我的css元素在Python selenium中给出完全相同的css元素,当它在图像中看起来不同时。
如何在图像中获得css选择器?我尝试过不同的CSS,但到目前为止它一直是第一个元素而不是第二个元素。
Css one:match-pop-market> #options_table> tbody> tr#mtb_row> td:nth-child(1)
Css二:match-pop-market> #options_table> tbody> tr#mta_row> td:nth-child(1)
注意a和b改变。
driver = webdriver.Chrome()
driver.set_window_size(1024, 600)
driver.maximize_window()
driver.get('https://www.palmerbet.com/sports/soccer')
time.sleep(4)
langs = driver.find_elements_by_css_selector("match-pop-market > #options_table > tbody > tr#mta_row > td:nth-child(1)")
langs_text = []
for lang in langs:
print(lang.text)
langs_text.append(lang.text)
print('second css')
langs2 = driver.find_elements_by_css_selector("match-pop-market > #options_table > tbody > tr#mtb_row > td:nth-child(1)")
langs_text2 = []
for lang in langs:
print(lang.text)
langs_text2.append(lang.text)
driver.close()
确切的输出:
Arsenal
Everton
Brighton & Hove Albion
Manchester City
Southampton
Stoke City
Swansea City
West Ham
Burnley
Leicester City
Tottenham Hotspur
AFC Bournemouth
Chelsea
Huddersfield Town
Manchester United
Watford
West Bromwich Albion
Liverpool
Newcastle United
Crystal Palace
AFC Bournemouth
Chelsea
Huddersfield Town
Liverpool
Newcastle United
Watford
Manchester United
Crystal Palace
West Bromwich Albion
第二个css
Arsenal
Everton
Brighton & Hove Albion
Manchester City
Southampton
Stoke City
Swansea City
West Ham
Burnley
Leicester City
Tottenham Hotspur
AFC Bournemouth
Chelsea
Huddersfield Town
Manchester United
Watford
West Bromwich Albion
Liverpool
Newcastle United
Crystal Palace
AFC Bournemouth
Chelsea
Huddersfield Town
Liverpool
Newcastle United
Watford
Manchester United
Crystal Palace
West Bromwich Albion
你的css_selector
和你的Script
是完美的。
也许,在你的第二个for()
循环中,你可能想在langs
中将langs2
替换为for lang in langs:
。所以你的代码块看起来像:
print('second css')
langs2 = driver.find_elements_by_css_selector("match-pop-market > #options_table > tbody > tr#mtb_row > td:nth-child(1)")
langs_text2 = []
for lang in langs2:
print(lang.text)
langs_text2.append(lang.text)
那会