使用 Selenium 截屏:WaitForPageToLoad 等待时间不够长
我正在尝试从具有多个浏览器的网页获取屏幕截图。只是尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我使用 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.
也许您可以利用浏览器状态栏来验证该页面是否已完全加载。我认为这是最好的解决方案。
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.