我如何确定是否为Selenium + Python加载了一些HTML元素?(How can I make sure if some HTML elements are loaded for Selenium + Python?)

HTML/CSS IT屋
百度翻译此文   有道翻译此文
问 题

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

How DOM works/is loaded? (in HTML)

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.

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.

Code trial :

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

Sample 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>
解决方案

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.

Now about your individual queries :

  • 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.

    • Page Title
    • Page Heading
    • Presence of an Alert
    • Attribute of an element
    • Presence of an element
    • Presence of a group of elements
    • Visibility of an element
    • Visibility of a group of elements
    • Clickablity of an element
    • StalenessOf of an element
    • FrameToBeAvailableAndSwitchToIt

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.

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

    • 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.
  • 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")))
    

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

本文地址:IT屋 » How can I make sure if some HTML elements are loaded for Selenium + Python?

问 题

从这个链接中,我假设DOM应该首先在RAM中加载。

questions / 3423864 / how-dom-works-is-loaded-in-html“> DOM如何工作/加载? (在HTML中)



但是接着我在Selenium中测试超时异常。看起来即使超时异常提出,一些元素已经可以找到,所以它不是一个空的对象。

但我想知道,我如何确保一些元素已经加载?例如。 HTML例子,我如何确保所有的< th> 元素都被加载?鉴于我实际上不知道< th> 元素的数量。



代码试用:

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


$ b

示例HTML:

 < table width =“910” border =“0”cellpadding =“3”cellspacing =“0”id =“fth1_”class =“fth1_”style =“display:none; position:fixed; top:29px; left:99px;”> 
< tr align =“center”>
< th id =“f13”style =“width:121px;”>< a href =“t /?i = 614& amp; amp; /第>
< th id =“f13”style =“width:267px;”>< a href =“t /?i = 614& amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; /第>
< / tr>
< / thead>
< / table>

解决方案

ChromeDriver 和 Chrome浏览器自动执行步骤。由于您已配置 set_page_load_timeout(10),所以引发了 timeout 异常,因为页面没有在通过<$ c $配置的时间范围内完全加载C> set_page_load_timeout()。但是,正如您调用 print(self.page_source)部分呈现的元素 HTML DOM




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




    • 页面标题

    • 页面标题
    • 存在警报
    • 元素的属性


    • 一个元素的可见性
    • li>
    • 一组元素的可见性
    • 元素的可点击性

    • 元素的StalenessOf

    • FrameToBeAvailableAndSwitchToIt

  • >


实现这些缩小的搜索条件可以在帮助中节省很多执行时间 WebDriverWait 结合 expected_conditions




  • 如何确保所有元素都已加载? :同样,我们的测试应该只集中在我们需要与之交互的元素/元素上,并且不需要验证其他元素的状态/状态,而这些元素是我们感兴趣的。


  • 现在,遵循上面提到的两点,这些是最常用的3个用例


  • >
    根据您提到的 usecase ,您可以制作所有< th> 元素的 List DOM树 中可见,同时等待可配置的时间量, em> WebDriverWait :selenium.webdriver.support.ui中的

     从Selenium导入WebDriverWait 
    .webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    $ b headerList = WebDriverWait(driver,20).until(EC.visibility_of_all_elements_located((By.XPATH,“// table [@ class ='fth1_'and @ b = b





    $ b $
    $ b

    注意:本图中使用的 xpath 是使用的示例 xpath 仅用于示范目的。



    本文地址:IT屋 » 我如何确定是否为Selenium + Python加载了一些HTML元素?

官方微信
扫一扫关注IT屋
微信公众号搜索 “ IT屋 ” ,选择关注
与百万开发者在一起
}