简体   繁体   English

Selenium - 无法从 Python 中单击页面上的元素

[英]Selenium - Unable to click element on page from Python

I am unable to click on the 'Odds' tab on the following page using Selenium: https://www.flashscore.dk/kamp/zFfSWY7h/#kampreferat我无法使用 Selenium 单击下一页上的“赔率”选项卡: https://www.flashscore.dk/kamp/zFfSWY7h/#kampreferat

Currently my code is the followig:目前我的代码如下:

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

I have tried to use the wait-function (WebDriverWait) but I cannot seem to make it work and even if i let the page sleep for 120 seconds it will give me an error:我尝试使用等待功能(WebDriverWait),但我似乎无法让它工作,即使我让页面休眠 120 秒,它也会给我一个错误:

Exception has occurred: NoSuchElementException
Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="a-match-odds-comparison"]"}

I have also tried to use driver.find_element_by_id('[@id="a-match-odds-comparison') also with any luck.我也尝试过使用driver.find_element_by_id('[@id="a-match-odds-comparison')也很幸运。

Any advice on how to improve and fix the code would be greatly appreciated.任何有关如何改进和修复代码的建议将不胜感激。

My final goal is to go through each sub-tab on the odds page and scrape alle the odds for each bookmaker.我的最终目标是通过赔率页面上的每个子选项卡 go 并为每个博彩公司刮取所有赔率。

Give this a try.试试这个。 It waits for the element on the screen to load.它等待屏幕上的元素加载。 In your case it waits for the "odds" tab to be visible.在您的情况下,它会等待“赔率”选项卡可见。

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

Just wait for the element to be clickable and click it.只需等待元素可单击并单击它。

Import进口

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM