[英]unable to select an element using xpath using selenium
I'm trying to automate a process using selenium and have been able to open a webpage and click on links, however I stumbled upon a table in which a link needs to be clicked but I'm unable to select that link and getting an error.我正在尝试使用 selenium 使流程自动化,并且已经能够打开网页并单击链接,但是我偶然发现了一个表格,其中需要单击一个链接,但我无法 select 该链接并出现错误. need help to select that particular element
需要帮助 select 该特定元素
right now this is what I've done现在这就是我所做的
elem2=browser.find_elements_by_xpath('/html/body/div[3]/table/tbody/tr[1]/td[2]/div[2]/table/tbody/tr[7]/td[3]/a::text')
elem2.click()
you can see in the picture that I'm trying to access the findhtml.org link.您可以在图片中看到我正在尝试访问 findhtml.org 链接。
the error that I get is我得到的错误是
InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression /html/body/div[3]/table/tbody/tr[1]/td[2]/div[2]/table/tbody/tr[7]/td[3]/a::text because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '/html/body/div[3]/table/tbody/tr[1]/td[2]/div[2]/table/tbody/tr[7]/td[3]/a::text' is not a valid XPath expression.
(Session info: chrome=81.0.4044.113)
First you have to switch to the iframe
首先你必须切换到
iframe
Example:例子:
frame = browser.find_elements_by_xpath('//iframe[contains(@src, \'hbx.media.net\')]')
browser.switch_to.frame(frame)
Now you can click现在你可以点击
link = browser.find_elements_by_xpath('//a[contains(@href, \'http://www.findhtml.org\')]')
link.click()
browser.get('https://publicrecords.netronline.com/state/IL/county/dupage')
wait = WebDriverWait(browser, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, "//td[contains(text(),'DuPage Supervisor of Assessments')]//following-sibling::td[2]//a"))).click()
output: output:
Note: please add below imports to your solution注意:请将以下导入添加到您的解决方案中
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
To click on the specific link try below code.要单击特定链接,请尝试以下代码。
Induce WebDriverWait
() and presence_of_element_located
() and following xpath.引发
WebDriverWait
() 和presence_of_element_located
() 以及后续 xpath。
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://publicrecords.netronline.com/state/IL/county/dupage")
element=WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,"//a[@href='http://www.dupageco.org/PropertyInfo/PropertyLookUp.aspx' and contains(.,'Go to Data')]")))
element.location_once_scrolled_into_view
element.click()
Please note the element is not inside any iframe
请注意该元素不在任何
iframe
内
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.