简体   繁体   中英

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

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:

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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