机器人框架(Python)-处理具有动态内容的页面上的延迟加载 [英] Robot framework, Python - handle lazy load on page with dynamic content

查看:92
本文介绍了机器人框架(Python)-处理具有动态内容的页面上的延迟加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面,其中包含动态生成的表格(行是动态生成的).在我的测试中,我不知道要期待多少行.我只是从所有行创建一个字典,然后将其与另一个字典进行比较. 但是该表具有延迟加载的功能,因此当行数较高时,应该乍一看"的某些行不可见,但需要向下滚动才能获取它们.因此这些行未包含在我的字典中,然后失败了... 但是同样,我不知道什么时候滚动(如果表中的行数少,就没有滚动的地方),以及在哪里滚动(我不期望任何特定的元素),期望滚动多少行等等.

I have a page with dynamically generated table (rows are dynamically generated). In my test I do NOT know how many rows to expect. I just create a dictionary from all the rows and then compare to another dictionary. But the table has lazy load, so when the number of rows is higher, some of the rows which should be there are not visible "on the first sight" but it needs to be scrolled down to get them. So those rows are not included into my dictionary and then it fails... But again, I do not know when to scroll (if the table has small amount of rows, there is nowhere to scroll) and where to scroll (I do not expect any particular element), how many rows to expect etc.

有人知道如何处理这种情况吗?因为我不知道 :-(谢谢!

Does anyone has an idea how to handle situation like this? Because I don't. :-( Thank you!

推荐答案

您可以尝试解决以下问题:

You can try to solve this problem as below:

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.common.exceptions import TimeoutException

# Get the current number of rows
current_rows_number = len(driver.find_elements_by_xpath('//tr'))
while True:
    # Scroll down to make new XHR (request more table rows)
    driver.find_element_by_tag_name('body').send_keys(Keys.END)
    try:
        # Wait until number of rows increased       
        wait(driver, 5).until(lambda: len(driver.find_elements_by_xpath('//tr')) > current_rows_number)
        # Update variable with current rows number
        current_rows_number = len(driver.find_elements_by_xpath('//tr'))
    # If number of rows remains the same after 5 seconds passed, break the loop
    # as there no more rows to receive
    except TimeoutException:
        break

# Now you can scrape the entire table

P.S.不幸的是,我对RobotFramework不熟悉,因此上面的代码位于纯Selenium + Python上.我希望它可以很容易地解释:)

P.S. Unfortunately, I'm not familiar with RobotFramework, so above code is on pure Selenium + Python. I hope it can be easily interpreted :)

这篇关于机器人框架(Python)-处理具有动态内容的页面上的延迟加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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