【问题标题】:Selenium - Unable to click element on page from PythonSelenium - 无法从 Python 中单击页面上的元素
【发布时间】: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


    【解决方案1】:

    试试这个。它等待屏幕上的元素加载。在您的情况下,它会等待“赔率”选项卡可见。

    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()
    

    【讨论】:

      【解决方案2】:
      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-13
        • 2018-05-26
        • 2019-04-13
        相关资源
        最近更新 更多