使用 Selenium 截屏:WaitForPageToLoad 等待时间不够长

发布于 2024-09-08 18:53:23 字数 559 浏览 3 评论 0原文

我正在尝试从具有多个浏览器的网页获取屏幕截图。只是尝试使用 Selenium RC,我写了这样的代码:

var sel = new DefaultSelenium(server, 4444, target, url);
sel.Start();
sel.Open(url);
sel.WaitForPageToLoad("30000");
var imageString = sel.CaptureScreenshotToString();

这基本上可以工作,但在大多数情况下,屏幕截图是一个空白的浏览器窗口,因为页面尚未准备好显示。如果我在 WaitForPageToLoad 之后添加睡眠,那么它会起作用,但这会减慢快速浏览器的速度和/或对于较慢的浏览器(或在负载下)来说可能会很短。

对此的典型解决方案似乎是等待某个元素的存在。然而,这意味着作为一个简单的通用解决方案,可以使用尽可能多的浏览器获取本地网页的屏幕截图(以测试布局),并且我不想输入某些元素名称或其他内容。这是一个简单的工具,您只需输入 Selenium 服务器 URL 和您想要测试的 URL,然后即可获取屏幕截图。

有什么建议吗?

I'm trying to get screenshots from a web page with multiple browsers. Just experimenting with Selenium RC, I wrote code like this:

var sel = new DefaultSelenium(server, 4444, target, url);
sel.Start();
sel.Open(url);
sel.WaitForPageToLoad("30000");
var imageString = sel.CaptureScreenshotToString();

This basically works, but in most cases the screenshot is of a blank browser window, because the page is not yet ready for display. It kind of works if I add a sleep just after the WaitForPageToLoad, but that slows down the fast browsers and/or may be to short for the slower browsers (or under load).

A typical solution for this seems to be to wait for the presence of a certain element. However, this is meant as a simple generic solution to get a screenshot of a local web page with as many browsers as possible (to test the layout) and I don't want to have to enter certain element names or whatever. It's a simple tool where you just enter the Selenium Server URL and the URL you want to test, and get the screenshots back.

Any advice?

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

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

发布评论

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

评论(2

扶醉桌前 2024-09-15 18:53:23

我使用 Selenium-RC 来捕获等待时间不同的远程页面的屏幕截图。在这种情况下,检查页面标题并使用 time.sleep(n 秒) 通常适合我。

I use Selenium-RC to capture screenshots of remote pages where the waiting time is variant. In such cases, checking the title of the page and using time.sleep(n seconds) usually does it for me.

淡忘如思 2024-09-15 18:53:23

也许您可以利用浏览器状态栏来验证该页面是否已完全加载。我认为这是最好的解决方案。

May be you can make use of Browser status bar to verify whether that page is loaded fully or not. I think this is the best solution.

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