这是我尝试与whoscored.com交互的代码。我以前用过selenium,我知道你必须点击“同意”来使用cookie,然后你才能在网站上做任何其他事情。但是,在这种情况下,selenium似乎无法单击“同意”按钮。是我的代码还是网站出了问题?
我得到的错误是
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element
我的代码:
from selenium import webdriver
import time
driver = webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe")
driver.get("https://www.whoscored.com/Regions/252/Tournaments/2/Seasons/6829/Stages/15151/PlayerStatistics/England-Premier-League-2017-2018")
time.sleep(10)
driver.find_element_by_xpath("/html/body/div[@id='qc-cmp2-container']/div[@id='qc-cmp2-main']/div[@class='sc-dnqmqq dkDDgT qc-cmp-cleanslate']/div[@id='qc-cmp2-ui']/div[@class='qc-cmp2-footer qc-cmp2-footer-overlay qc-cmp2-footer-scrolled']/div[@class='qc-cmp2-summary-buttons']/button[@class='sc-bwzfXH kTIxrL']").click()
发布于 2020-09-19 19:29:51
使用更简单的xpath查找适用于我
driver.find_element_by_xpath("//button[contains(., 'Agree')]").click()
//button
查找所有作为按钮标记的节点,[contains(., 'Agree']
查找内部文本包含子字符串“[contains(., 'Agree']
”的所有节点。
发布于 2020-09-19 22:11:08
我有一个这样的问题,我发现这个代码总是比'click()‘方法工作得更好,即使元素是不可交互的。看看这个
yourElement = driver.find_elements_by_xpath(".//button[@class='sc-bwzfXH kTIxrL']")
driver.execute_script("arguments[0].click();", "yourElement")
希望它能帮到你
https://stackoverflow.com/questions/63968096
复制相似问题