尽管在chrome检查中使用了正确的css元素,但没有抓取正确的css

时间:2017-12-20 05:54:03

标签: python selenium selenium-webdriver css-selectors

我的css元素在Python selenium中给出完全相同的css元素,当它在图像中看起来不同时。

如何在图像中获得css选择器?我尝试过不同的CSS,但到目前为止它仍然是第一个元素,而不是第二个元素。

Css one: match-pop-market> #options_table> tbody> tr#mtb_row> TD:第n个孩子(1)

Css二: match-pop-market> #options_table> tbody> tr#mta_row> TD:第n个孩子(1)

注意a和b改变。

enter image description here

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

1 个答案:

答案 0 :(得分:0)

您的 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)

那就是