没有这样的元素错误,但是该元素可以通过XPATH表达式位于DOM中

发布于 2025-01-18 02:58:43 字数 353 浏览 0 评论 0原文

我有一个python脚本登录到我们公司的网站,然后获取每一排数据。它记录了我,找到了带有行值的表所在的表格,从那里我尝试在iframe表中找到一个行并附加的是一张图片:

“

突出显示正确的xpath表达式,但我需要所有31个值。

除此之外,我所有的代码都可以正常工作:

elem5 = browser.find_element_by_xpath("//td/label[contains(@id, 'driver')][1]")

I have a python script logging into our company website to then get each row of data. It logs me in, locates the iframe where the table with row values is located, from there I try to locate a row in the iframe table and attached is a picture:

xpathexpression

Highlighting the correct xpath expression but I need all 31 values.

All my code works properly except this:

elem5 = browser.find_element_by_xpath("//td/label[contains(@id, 'driver')][1]")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余罪 2025-01-25 02:58:43

find_element_by _* 返回单个webelement,在您看的地方对于所有 31 元素。因此,您需要 find_elements* 以下定位策略

  • 使用 css_selector

      elements = driver.find_elements(by.css_selector,“ td> label [id^='driver']”)
     

  • 使用 xpath

      elements = driver.find_elements(by.xpath,“ // td/label [starting-with(@id,'driver')]”)
     

要找到所有所需的元素,理想情况下,您需要诱导 webdriverwait =“ https://stackoverflow.com/a/64770041/7429447”> visibility_of_all_elements_located() ,您可以使用以下任何一个定位器策略

  • 使用 css_selector

      elements = webdriverwait(驱动程序,20).until(ec.visibility_of_all_elements_located((((by.css_selector),“ td> label [id^='驱动程序']”)
     
  • 使用 xpath

      elements = webdriverwait(驱动程序,20).until(ec.visibility_of_all_elt_elements_located(((by.xpath,“ // xpath,” // td/label [starting-with(@id,'driver')]))))))))))))))))))))
     
  • 注意:您必须添加以下导入:

     来自selenium.webdriver.support.ui导入webdriverwait
    从selenium.webdriver.common.通过进口
    从selenium.webdriver.support进口预期_conditions作为ec
     

find_element_by_* returns a single webelement, where as you are looking for all the 31 elements. So you need find_elements* and you can use either of the following Locator Strategies:

  • Using css_selector:

    elements = driver.find_elements(By.CSS_SELECTOR, "td > label[id^='driver']")
    
  • Using xpath:

    elements = driver.find_elements(By.XPATH, "//td/label[starts-with(@id, 'driver')]")
    

To find all the desired elements ideally you need to induce WebDriverWait for visibility_of_all_elements_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    elements = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "td > label[id^='driver']")))
    
  • Using XPATH:

    elements = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//td/label[starts-with(@id, 'driver')]")))
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文