如何在Selenium Python中按xpath查找元素?

2024-10-04 05:27:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Python中的Selenium来单击网页上的报告链接。我有它的工作点,它打开的页面,报告是在,但我有麻烦,实际上点击具体的报告。你知道吗

该页面有一个报表列表,所有报表都具有相同的类。这是我在检查特定报告时得到的结果,例如:

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

我试过:

driver.find_element_by_xpath("xpath")

而这似乎不起作用,一旦到了报告的那一页,它就什么也做不了。你知道吗


Tags: 网页列表报表链接报告seleniumaction页面
1条回答
网友
1楼 · 发布于 2024-10-04 05:27:54

归纳WebdriverWaitelement_to_be_clickable。你知道吗

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

如果无法使用Webdriver单击,请尝试使用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)

编辑


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)

或者

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)

相关问题 更多 >