简体   繁体   中英

selenium: no such element: Unable to locate element by using xpath for espncricket

I'm having some difficulty, I want to find the Commentary button and then click it.

This is the website:

from selenium import webdriver

button1= driver.find_element_by_xpath("//*[@id='main-container']/div[1]/div/div/div[1]/div[2]/a[3]")

button1.click()

But some problems happen, how to slove this? I tried to select it by class name before, but class name is not unique and hard to locate.

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id='main-container']/div[1]/div/div/div[1]/div[2]/a[3]"} (Session info: chrome=92.0.4515.159)

okay try with below xpath :

//div[text()='Commentary']/..

in code:

button1 = driver.find_element_by_xpath("//div[text()='Commentary']/..")
button1.click()

or with explicit waits:

driver = webdriver.Chrome(driver_path)
driver.maximize_window()
driver.implicitly_wait(30)
driver.get("https://www.espncricinfo.com/series/carlton-series-2000-01-61080/australia-vs-west-indies-1st-match-65601/full-scorecard")
wait = WebDriverWait(driver, 10)

wait.until(EC.element_to_be_clickable((By.XPATH, "//div[text()='Commentary']/.."))).click()

Imports:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
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