无法从硒python中的div元素中找到表格元素 [英] Cannot find table element from div element in selenium python

查看:61
本文介绍了无法从硒python中的div元素中找到表格元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 seleniumdiv 元素中选择一个 table 值,

当我检查元素时,我可以看到该元素包含 table 但在查看源代码时我找不到它(这是在尝试通过 selenium 访问时发生的).

检查期间:

下面是我试过的代码,

totals = driver.find_element_by_id("totals")#获取合计div元素.只有一个label=totals.find_elements_by_class_name("hasLabel")#应该得到表行,但是是空的打印(len(标签))#returned 0

但是,它是空的(正如我在源代码中看到的那样).我需要将这些值导出到文件中作为自动化的一部分

还有其他方法可以提取这些应有的值吗?.

解决方案

您的观察似乎近乎完美.根据您共享的 During Inspect 快照中的 HTML,突出显示了文本为 Taxes Due 的元素.

<小时>

在您通过 :

  • 使用XPATH:

    print(len(WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//table/tbody//tr[@class='hasLabel'][./td[@class='label']]")))))

  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.common.by import By从 selenium.webdriver.support 导入 expected_conditions 作为 EC

I am trying to select a table values from div element using selenium,

When I inspect the element, I could see the element contains the table but I cannot find it while viewing the source code (which is happening while trying to access through selenium).

During Inspect:

Below is the code I tried,

totals = driver.find_element_by_id("totals")#gets the totals div element. Only one is there
labels=totals.find_elements_by_class_name("hasLabel")#should get the table rows, but it was empty
print(len(labels))#returned 0

But, It was empty (as I saw in the source code). I need to export those values in a file as part of automation

Is there any other way I can extract those due values?.

解决方案

Your observation seems to be near perfect. As per the HTML within During Inspect snapshot which you have shared, The element with text as Taxes Due is highlighted.


While within the While viewing the source code snapshot which you have extracted through Selenium, possibly the Page Source was pulled out even before the child elements have completely rendered within the DOM Tree:

Hence the child elements within <div id='totals'></div> are not present withi the Page Source.


The relevant HTML in text format would have help to construct a canonical answer. However, to get the total number of <tr> elements you need to induce WebDriverWait for the visibility_of_all_elements_located() and you can use either of the following Locator Strategies:

  • Using XPATH:

    print(len(WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//table/tbody//tr[@class='hasLabel'][./td[@class='label']]")))))
    

  • 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
    

这篇关于无法从硒python中的div元素中找到表格元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆