【问题标题】:unable to select an element using xpath using selenium无法使用 xpath 使用 selenium 选择元素
【发布时间】:2020-08-08 00:59:55
【问题描述】:

我正在尝试使用 selenium 自动化流程,并且能够打开网页并单击链接,但是我偶然发现了一个需要单击链接但我无法选择该链接的表格,并且收到错误。需要帮助来选择特定元素

现在这就是我所做的

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)

【问题讨论】:

    标签: python selenium xpath automation


    【解决方案1】:
    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()
    

    输出:

    注意:请在您的解决方案中添加以下导入

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

    【讨论】:

    • 得到错误NameError: name 'driver' is not defined
    • 检查更新的解决方案,使用浏览器代替驱动程序
    • TimeoutException: Message: 这是新错误.. 我等了 1 分钟,没有点击任何地方并收到超时错误
    • 你能分享你的网址吗?在切换或单击 td 元素时也出现此错误
    • @DipakBachhav : 没有可切换的 iframe。为什么您提供的信息有误?
    【解决方案2】:

    要点击特定链接,请尝试以下代码。

    诱导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

    【讨论】:

    • 但您能向我解释一下这些新导入的作用以及它们如何帮助点击链接吗?
    • @reduxHelpPlz :它等待元素在页面上可用并单击。它消除了有时会导致查找元素错误的同步问题。
    • 好的。所以在我使用 find_element_by_link_text 然后在该元素上添加 click() 之前,有时会显示中断或其他错误。所以这个方法效率更高,我应该用这个新的点击代码替换之前的点击事件?
    • 我的代码会做两件事。1) 这将解决同步问题。 2)由于您正在搜索表格底部的项目,因此您需要滚动到元素element.location_once_scrolled_into_view,然后单击。希望你现在理解了整个代码。
    【解决方案3】:

    首先你必须切换到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()
    

    【讨论】:

    • 在 hbx.net 的 'x' 下得到错误 SyntaxError: invalid syntax
    • 用 \ 转义 url 或添加双引号 "
    • 现在出现一个新错误NameError: name 'driver' is not defined
    • 对我来说应该是浏览器而不是驱动程序
    • 我也尝试做浏览器而不是驱动程序并收到此错误InvalidArgumentException: Message: invalid argument: invalid 'id'
    猜你喜欢
    • 1970-01-01
    • 2020-08-06
    • 1970-01-01
    • 1970-01-01
    • 2018-06-14
    • 2014-11-02
    • 1970-01-01
    • 2013-02-04
    • 1970-01-01
    相关资源
    最近更新 更多