[英]Selenium - Unable to click element on page from Python
我無法使用 Selenium 單擊下一頁上的“賠率”選項卡: https://www.flashscore.dk/kamp/zFfSWY7h/#kampreferat
目前我的代碼如下:
from selenium import webdriver # General webscraping
from selenium.webdriver.common.by import By # Specification of method for locating elements
from selenium.webdriver.support.ui import WebDriverWait # Waiting for element to load
from selenium.webdriver.support import expected_conditions as EC # Expected conditions (used for waits)
import time
driver = webdriver.Chrome(path) # Use the chrome webdriver
wait = WebDriverWait(driver,10) # Will wait for up to 10 seconds for an element/page to load
# URL of webpage to scrape from
web = 'https://www.flashscore.dk/kamp/zFfSWY7h/#kampreferat'
# Open webpage
driver = webdriver.Chrome(path)
driver.get(web)
# Go to Odds
odds_page = driver.find_element_by_xpath('//*[@id="a-match-odds-comparison"]')
odds_page.click()
我嘗試使用等待功能(WebDriverWait),但我似乎無法讓它工作,即使我讓頁面休眠 120 秒,它也會給我一個錯誤:
Exception has occurred: NoSuchElementException
Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="a-match-odds-comparison"]"}
我也嘗試過使用driver.find_element_by_id('[@id="a-match-odds-comparison')
也很幸運。
任何有關如何改進和修復代碼的建議將不勝感激。
我的最終目標是通過賠率頁面上的每個子選項卡 go 並為每個博彩公司刮取所有賠率。
試試這個。 它等待屏幕上的元素加載。 在您的情況下,它會等待“賠率”選項卡可見。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
driver = webdriver.Chrome(executable_path='path')
waitshort = WebDriverWait(driver,.5)
wait = WebDriverWait(driver, 20)
waitLonger = WebDriverWait(driver, 100)
visible = EC.visibility_of_element_located # Use the chrome webdriver # Will wait for up to 10 seconds for an element/page to load
driver.get('https://www.flashscore.dk/kamp/zFfSWY7h/#kampreferat')
odds = wait.until(visible((By.XPATH,'//*[@id="a-match-odds-comparison"]'))).click()
wait = WebDriverWait(driver, 10)
driver.get('https://www.flashscore.dk/kamp/zFfSWY7h/#kampreferat')
wait.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="a-match-odds-comparison"]'))).click()
只需等待元素可單擊並單擊它。
進口
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.