简体   繁体   中英

How to find element by xpath in Selenium Python?

I am trying to use Selenium in Python to click on a link to a report on a web page. I have it working up to the point where it opens the page that the report is on, but I am having trouble actually clicking that specific report.

The page has a list of reports all with the same class. This is what I get when I inspect that specific report for example:

<a class="rpt" href="reportConfigRedirect.asp?action=filter&amp;rc_id=181786&amp;letter=">Run with new filters</a>

I have tried:

driver.find_element_by_xpath("xpath")

and this doesn't seem to work, it doesn't do anything once it gets to that page with the report.

Induce WebdriverWait and element_to_be_clickable .Use the following Xpath.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


element=WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//a[@class='rpt'][contains(.,'Run with new filters')]")))
element.click()

If unable to click using Webdriver try use javascript executor.

element=WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//a[@class='rpt'][contains(.,'Run with new filters')]")))
driver.execute_script("arguments[0].click();",element)

EDITED


element=WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//a[contains(.,'Run with new filters')]")))
driver.execute_script("arguments[0].click();",element)

OR

element=WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//a[contains(@hef,'reportConfigRedirect.asp?action=filter')][contains(.,'Run with new filters')]")))
driver.execute_script("arguments[0].click();",element)

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