如何仅等待 DOM 使用 Selenium 加载(无图像)

发布于 2024-12-11 19:34:33 字数 943 浏览 0 评论 0原文

页面上有一长串图像。应用程序中有一个 AJAX“加载更多图像”按钮(位于页面底部),当用户向下滚动页面时会自动激活该按钮。 我想用 Selenium 将页面滚动到最后,并确保页面底部没有“加载更多图像”按钮。 该脚本(可能有更优雅的解决方案,但这不是重点)

browser.execute_script("""
            setInterval(function() {
                window.scrollBy(0,50000);
            }, 1000);
           ;""")
invisibleShowMoreCounter = 0;
while True:
        if "" ==  browser.find_element_by_id("show_more").text:
            invisibleShowMoreCounter = invisibleShowMoreCounter + 1
            if (invisibleShowMoreCounter  > 3): break
        time.sleep(1)

它将页面滚动到底部,然后检查 3 秒内是否没有“show_more”元素(但不确定是否需要此循环)。 所以问题是它成功地向下滚动页面,但在此之后,它停留在 browser.find_element_by_id("show_more") 上,直到页面完全加载(包括图像),这需要很多时间。所以 DOM 就在那里,但是因为有很多图像,所以加载所有图像大约需要几分钟的时间。 我不能 100% 确定这是因为页面未完全加载,但看起来很可能(因此它进入循环的第一次迭代,然后停留在 find_element_by_id 上几分钟,然后进一步并成功完成。 问题是是否可以命令 find* 方法不等待所有图像完成下载,以便测试不会花费几分钟才能通过?

设置 browser.implicitly_wait(1) (这似乎与此相关)似乎没有帮助。

There is a long list of the images on the page. There is an AJAX "Load more images" button in the application (at the bottom of the page) which is activated automatically when user scrolling page down.
I want to scroll the page to the very end with Selenium and make sure that there is no button "Load more images" at the bottom of the page.
The script (there could be more elegant solution but that's not a point)

browser.execute_script("""
            setInterval(function() {
                window.scrollBy(0,50000);
            }, 1000);
           ;""")
invisibleShowMoreCounter = 0;
while True:
        if "" ==  browser.find_element_by_id("show_more").text:
            invisibleShowMoreCounter = invisibleShowMoreCounter + 1
            if (invisibleShowMoreCounter  > 3): break
        time.sleep(1)

It scrolls the page to the bottom and then checks that there is no "show_more" element for 3 seconds (not sure if this cycle is needed at all though).
So the thing is that it successfully scrolls the page down but after this, it stuck on browser.find_element_by_id("show_more") until the page fully loaded (including images) which takes a lot of time. So DOM is there but because there are a lot of images it takes about few minutes to load all of them.
I'm not 100% sure that this is because page not fully loaded but it looks very likely (so it goes to the first iteration of the cycle and then just stay on find_element_by_id for few minutes and then go further and successfully finished.
The question is if it is possible to command find* method not to wait for all images to finish downloading so the test will not take few minutes to pass?

Setting browser.implicitly_wait(1) (which seems kind of relevant to this) does not seem to help.

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

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

发布评论

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

评论(1

总以为 2024-12-18 19:34:33

您可以等待浏览器/文档状态变为交互。那就是等待js条件

document.readyState='interactive'

You can wait for browser/document status to interactive. That is wait for js condition

document.readyState='interactive'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文