【发布时间】:2021-04-05 19:31:51
【问题描述】:
我无法使用 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'),运气不错。
任何关于如何改进和修复代码的建议将不胜感激。
我的最终目标是浏览赔率页面上的每个子选项卡,并为每个博彩公司收集所有赔率。
【问题讨论】:
标签: python selenium selenium-webdriver web-scraping xpath