如何确定是否为 Selenium + Python 加载了某些 HTML 元素? [英] How can I make sure if some HTML elements are loaded for Selenium + Python?

查看:39
本文介绍了如何确定是否为 Selenium + Python 加载了某些 HTML 元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从这个链接,我假设 DOM 应该首先在 RAM 中作为一个整体加载.

From this link, I assume the DOM should be loaded as a whole at first in RAM.

DOM 如何工作/加载?(在 HTML 中)

但后来我在 Selenium 中测试时出现超时异常.似乎甚至引发了超时异常,已经可以找到一些元素,因此它不是一个空对象.

But then I test in Selenium with a timeout exception. It seems even the timeout exception is raised, some elements can already be found, so it is not an empty object.

但我想知道,如何确保某些元素已经加载?例如.在 HTML 示例中,如何确保所有 元素都已加载?鉴于我实际上不知道 元素的数量.

But I am wondering, how can I make sure some elements are already loaded? E.g. the HTML example, how can I make sure all <th> elements are loaded? Given the fact that I actually do not know the number of the <th> elements.

代码试用:

driver = webdriver.Chrome()
driver.set_page_load_timeout(10)
try:
    driver.get(url)
    print('load success!')
except TimeoutException:
    print(self.page_source)

示例 HTML:

<table width="910" border="0" cellpadding="3" cellspacing="0" id="fth1_" class="fth1_" style="display: none; position: fixed; top: 29px; left: 99px;">
     <thead style="background-color: rgb(233, 233, 233);">
        <tr align="center">
           <th id="f13" style="width: 121px;"><a href="t/?i=614&amp;o=1">Symbol</a></th>
           <th id="f13" style="width: 267px;"><a href="t/?i=614&amp;o=2">Name</a></th>
        </tr>
     </thead>
</table>

推荐答案

根据您的代码试验,您正在使用 ChromeDriverChrome 浏览器自动化em> 步骤.由于您已经配置了 set_page_load_timeout(10)timeout 异常被引发,因为页面在通过 set_page_load_timeout() 配置的时间范围内没有完全加载.但是当您调用 print(self.page_source) 时,部分渲染的元素在 HTML DOM 被检索.

As per your code trials you are using ChromeDriver and Chrome Browser to Automate the steps. As you have configured set_page_load_timeout(10) the timeout exception is raised as the page havn't completely loaded within the timeframe configured through set_page_load_timeout(). But as you have invoked print(self.page_source) the elements rendered within the partially rendered HTML DOM are retrieved.

关于您的个人查询:

  • 如何确保某些元素已经加载?:理想的测试用例应该有一个明确的步骤,例如验证元素的存在,验证元素的可见性或验证元素的交互性(单击时).从这个角度来看,验证元素已经加载可能不包括所需的元素.因此,您需要将搜索条件缩小到某些确定的内容,例如

  • How can I make sure some elements are already loaded? : An ideal testcase would have a definite step e.g. to validate the presence of an element, to validate the visibility of an element or to validate the interactability (while clicking) of element. From this perspective verifying the elements are already loaded may not include the desired element. Hence instead of such a broader search criteria you need to narrow down your search criteria to some thing definite e.g.

  • 页面标题
  • 页面标题
  • 存在警报
  • 元素的属性
  • 元素的存在
  • 一组元素的存在
  • 元素的可见性
  • 一组元素的可见性
  • 元素的可点击性
  • 元素的陈旧性
  • FrameToBeAvailableAndSwitchToIt

WebDriverWaitexpected_conditions.

Implementing these narrowed down search criteria can save a lot of execution time with the help of WebDriverWait inconjunction with expected_conditions.

  • 如何确保所有元素都已加载? :同样,我们的测试应该只关注我们需要与之交互的一个或多个元素,而忽略验证状态/我们不感兴趣的其他元素的条件.

  • How can I make sure all elements are loaded? : Again, our tests should be focused only on the element/elements with which we need to interact with and leaveout verifying the status/condition of other elements which are not of our interest.

现在,按照上面提到的两点,这是 3 个最常用的用例:

Now, following the two points mentioned above these are the 3 most commonly used usecases :

  • presence_of_element_located :检查元素是否存在于页面的 DOM 上的期望.这并不一定意味着该元素是可见的.
  • visibility_of_element_located :检查元素是否存在于页面的 DOM 上并且可见的期望.可见性意味着元素不仅被显示,而且高度和宽度都大于 0.
  • element_to_be_clickable :检查元素是否可见并已启用以便您可以单击它的期望.
  • presence_of_element_located : An expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible.
  • visibility_of_element_located : An expectation for checking that an element is present on the DOM of a page and visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0.
  • element_to_be_clickable : An Expectation for checking an element is visible and enabled such that you can click it.

根据您提到的用例,您可以使 List 中的所有 元素在 DOM 树,同时等待一段可配置的时间,引发 WebDriverWait 如下:

As per the usecase you mentioned you can make a List of all the <th> elements visible within the DOM Tree whilst waiting for a configurable amount of time inducing WebDriverWait as follows :

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

headerList = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//table[@class='fth1_' and @id='fth1_']/thead/tr//th")))

注意:此插图中使用的 xpath 是一个示例 xpath,仅用于演示目的.

Note : The xpath used in this illustration is a sample xpath used only for demonstration purpose.

这篇关于如何确定是否为 Selenium + Python 加载了某些 HTML 元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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