python - selenium:即使使用完整的 xpath 也找不到元素

标签 python selenium webdriverwait expected-condition

我正在尝试使用 selenium 从 [网站][1] 中提取数据。但我无法访问我想单击的按钮。我尝试了很多方法都没有用。我不知道为什么会发生这种情况。
这是代码。

from selenium import webdriver
from selenium.webdriver.chrome import service
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

option = webdriver.ChromeOptions()
option.add_argument("start-maximized")
option.add_experimental_option("excludeSwitches", ["enable-automation"])
option.add_experimental_option('useAutomationExtension', False)
option.add_argument("--disable-blink-features")
option.add_argument("--disable-gpu")
#option.add_argument("--headless")
option.add_argument('--disable-blink-features=AutomationControlled')
wd = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=option)


url = "https://greyhoundbet.racingpost.com/#result-meeting/track_id=4&r_date=2021-01-01&r_time=13:24"
wd.execute_cdp_cmd('Network.setUserAgentOverride', {"userAgent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36'})
wd.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
wd.get(url)

button = wd.find_elements(By.XPATH, "/html/body/div[3]/div[2]/div[2]/div[2]/div[1]/div/span")
#button = wd.find_elements(By.CLASSNAME, 'btnLeft btnBlueText')
#button = wd.find_element_by_id('resultsDateBtn')
#used above methods along with relative xpath

print(button)
for i in button:
    if i.get_attribute('id') == "resultsDateBtn":
        i.click()

wd.quit()

我总是得到空列表或找不到元素错误 [1]:https://greyhoundbet.racingpost.com/#results-list/r_date=2021-01-01

最佳答案

您错过了等待/延迟。
您应该在访问 Web 元素之前先加载页面。
最好的方法是使用预期条件,如下所示:

from selenium import webdriver
from selenium.webdriver.chrome import service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

option = webdriver.ChromeOptions()
option.add_argument("start-maximized")
option.add_experimental_option("excludeSwitches", ["enable-automation"])
option.add_experimental_option('useAutomationExtension', False)
option.add_argument("--disable-blink-features")
option.add_argument("--disable-gpu")
#option.add_argument("--headless")
option.add_argument('--disable-blink-features=AutomationControlled')
wd = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=option)
url = "https://greyhoundbet.racingpost.com/#results-list/r_date=2021-01-01"
wd.execute_cdp_cmd('Network.setUserAgentOverride', {"userAgent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36'})
wd.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
wd.get(url)
wait = WebDriverWait(wd, 20)
button = wait.until(EC.visibility_of_element_located((By.ID, "resultsDateBtn")))
#now you can click this button etc
button.click()

关于python - selenium:即使使用完整的 xpath 也找不到元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70583948/

相关文章:

python - 在变化时显示 kivy slider 值

python - 使用python将webm转换为mp3?

python - 使用 Selenium 在 Mac 上打开选项卡、关闭选项卡并退出 Firefox?

python - 如何通过Python使用Selenium从网页中提取文本$7.56

python - 使用python根据变量正确打印字符

python - 带有 smtp.gmail SMTPAuthenticationError 534 的 Django 电子邮件需要特定于应用程序的密码

selenium - 单击警报内的“确定”按钮(Selenium IDE)

linux - 错误未知错误 : missing or invalid 'entry.level' while executing Nigthwatch. js

javascript - 如何结合execute_scipt和WebdriverWait

java - 如何处理在新选项卡中自动打开的新窗口?