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

问题描述 投票:1回答:1

我的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改变。

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
python selenium selenium-webdriver css-selectors
1个回答
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)

那会

© www.soinside.com 2019 - 2024. All rights reserved.