我正在嘗試使用 [website][1] 中的 selenium 提取資料。但我無法訪問我想點擊的按鈕。我試過很多方法都沒有用。我不知道為什么會這樣。
這是代碼。
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
uj5u.com熱心網友回復:
您錯過了等待/延遲。
您應該在訪問 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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/405015.html
標籤:
