![](/img/trans.png)
[英]unable to select an element using xpath in selenium and python
[英]unable to select an element using xpath using selenium
我正在嘗試使用 selenium 使流程自動化,並且已經能夠打開網頁並單擊鏈接,但是我偶然發現了一個表格,其中需要單擊一個鏈接,但我無法 select 該鏈接並出現錯誤. 需要幫助 select 該特定元素
現在這就是我所做的
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()
您可以在圖片中看到我正在嘗試訪問 findhtml.org 鏈接。
我得到的錯誤是
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)
首先你必須切換到iframe
例子:
frame = browser.find_elements_by_xpath('//iframe[contains(@src, \'hbx.media.net\')]')
browser.switch_to.frame(frame)
現在你可以點擊
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:
注意:請將以下導入添加到您的解決方案中
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
要單擊特定鏈接,請嘗試以下代碼。
引發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()
請注意該元素不在任何iframe
內
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.