Xvfb + firefox:如何知道firefox完成渲染?
我试图通过使用 X 服务器在无头 (Xvfb) X11 中运行 Firefox 来获取网页缩略图。
我正在寻找一种方法来知道 Firefox 何时完成渲染,然后我可以使用图像程序来抓取窗口。
问题:我无法确定 Firefox 何时完成渲染。我所知道的是,一旦完成渲染,状态栏应该有一个“完成”的输出(IE,如 Firefox 中的 GUI 所示)。
谁能知道我可以用什么来确定 Firefox 是否完成渲染页面?
我所做的只是睡 40 秒,但这可能并不总是有效。
环境: Centos 5.7、Xvfb、Firefox 3.6.23
Shell 命令:
Xvfb: 1 -screen 0 1024x768x24 &
export DISPLAY=localhost:1.0
DISPLAY=localhost:1.0 firefox http://www.example.com -width 1024 -height 768
sleep 40 **[ NEED some thing to tell me if Firefox finishes rendering ]**
DISPLAY=localhost:1.0 import -window root example.com.png
pkill firefox
I'm trying to grab web thumbnails by using X server to run Firefox in headless (Xvfb) X11.
I am looking for a way to know when Firefox FINISHED rendering and then I can use image programs to grab the windows.
Problem: I can NOT determine when Firefox finishes rendering. All I know is that the status bar should have an output of "Done" once it finishes rendering (IE, as shown in Firefox in GUI).
Can anyone know what can I use to determine whether if Firefox finishes rendering a page or not?
What I did is just sleep 40 seconds but this may not always work.
Environment:
Centos 5.7, Xvfb, Firefox 3.6.23
Shell commands:
Xvfb: 1 -screen 0 1024x768x24 &
export DISPLAY=localhost:1.0
DISPLAY=localhost:1.0 firefox http://www.example.com -width 1024 -height 768
sleep 40 **[ NEED some thing to tell me if Firefox finishes rendering ]**
DISPLAY=localhost:1.0 import -window root example.com.png
pkill firefox
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请注意,我认为使用
FF 4.x
及更高版本无法轻松完成此操作。这是由于这些版本的“特性”。我建议您如果可以的话,使用其他浏览器,例如 Opera。我自己还没有测试过,但我认为它没有 FF 中注意到的所有缺陷。见下文。在您的情况下,以下解决方案可能是有效的:
编写一些 Greasemonkey 脚本,在屏幕上的某处添加绝对定位的矩形。
当页面加载完成时,让这个矩形以 3 个周期闪烁:红色、蓝色、透明
编写一个脚本,等待至少 2 个这样的周期完成(因此它会看到红色、蓝色、某些东西、红色、蓝色),然后执行透明阶段中具有正确计时的屏幕截图。
这应该可以通过 X11 和 VNC 等实现。抱歉,我没有更好的解决方案,也没有可以在这里展示的代码。
就我而言,我会使用我为解决类似问题而编写的
EasyRFB
来解决它。然而,我还不能向您推荐这一点,除非您是非常绝望的天才,能够轻松阅读和理解可怕的未记录的代码片段;)顺便说一句,感谢您注意到一个能够截图任意网页的包的想法,这为
EasyRFB
提供了一个出色的用例。我自己以前从来没有想过这个问题。查看 GitHub,我注意到, 那里 一些基于 Selenium 或 WebKit 进行网页截图的解决方案。这些当然适合宣传照片,但它们显然无法涵盖用户如何查看网页的一般用例。
也许能够自动从 Windows 95 IE 4、FF 1 或带有 Chimera 等的 Debian Potato 看到的任意页面进行屏幕截图会很有趣。我会研究一下这个;)
请注意,< 上还有另一篇文章来自我的 href="https://stackoverflow.com/questions/5381152/p2p-rdp-with-python">Stackoverflow 。
有一个我称之为
EasyRFB
的东西,它是我用Python
(核心)、PHP
(小型网络助手)AJAX< 编写的。 /code> 和
bash
(控制脚本)。它已使用XVnc
进行了测试,但应适用于任何VNC
。它以某种方式能够做你想做的事,但要注意,它是开发代码,没有文档记录,复杂且写得很糟糕,并且可能有错误。您可以在那里找到它:它是出于以下目的而编写的:
下写成快速、高效、易于使用、也不适合其他人(还)。对我来说,它以某种方式起作用。
它的作用:
VNC
服务器提供AJAX
Web 前端。.jpg
保持更新到屏幕内容。edit.html
进行 Web 编辑 - 但是此编辑器是基本的,不言自明的,根本没有文档记录VNC
.sh
脚本可以帮助您了解如何这已经完成了,但是我我还无法发布真正有用的东西,抱歉没有任何记录,我无法解释如何使用它。要么你发现自己,要么你就迷失了。对不起。最好别看它,它可能会伤害你。
现在来看看 FF 在执行所有这些操作时注意到的问题:
较新的 FF 版本无法正确更新屏幕。我身边已知的最后一个正常工作的版本是 FF 3.6 系列,后来有时会出现一些奇怪的行为。
例如:
因此,即使看着旋转器变成灰色,包括(!)通知“onload()”的幼稚 Greasemonkey 解决方案,在
FF 4.x
及更高版本中都会失败,因为屏幕的某些部分可能仍然不可用。当您检测到它已准备好时进行更新。也许它只与
XVnc
有关,也许我的脚本中有一个错误触发了这一切,但对于FF 3.x
一切都是正确的,我认为,恢复到旧版本将立即解决所有看到的问题。 (但我已经针对所有这些问题制定了解决方法。但抱歉,我无法发布该脚本。)情况变得更糟。
FF 4.x
及更高版本显然会稍微延迟事件处理。因此,将鼠标跳出滚动条通常会使其突出显示,我认为事件不会到达滚动条。在普通桌面上,您不会跳动鼠标,但我的解决方案可以做到这一点。修复方法是生成额外的、多余的鼠标移动事件,让 FF 做正确的事情。
我还注意到,我真的不知道是否是由于 FF 或 FF 中显示的网页,有时事情没有到达应有的位置。不过,我之前从未在
FF 3.x
中观察到这一点。解决这个问题的技巧是重新加载页面。这可能与增量屏幕更新有关,但我还没有深入研究这一点,因为重试(自动单击重新加载按钮)对我有用。Note that I don't think this can be done with
FF 4.x
and above easily. This is due to "features" of these versions. My recommendation to you is to use some different browser if you can, like Opera. I did not test it myself yet, but I think it does not have all those flaws noticed with FF. See below.In your case following solution may be efficient:
Write some Greasemonkey script which adds an absolute positioned rectangle somewhere on the screen.
Let this rectangle flash in a 3 cycle when the page has finished loading: red, blue, transparent
Write a script which waits at least 2 such cycles to complete (so it sees red, blue, something, red, blue) and then do a screenshot with the correct timing in the transparent phase.
This should be doable with X11 and VNC and similar. Sorry that I don't have a better solution nor code I can present here.
I for my part would solve it using
EasyRFB
which I wrote to solve similar things. However I cannot recommend that to you, yet, except if you are very desparate Genius able to read and understand horrible undocumented code fragments with ease ;)BTW, thanks for noting the idea of a package which is able to screenshot arbitrary web pages, which makes an excellent use case for
EasyRFB
. Never thought about this myself before.Looking at GitHub I noticed, that there are some solutions for taking web screenshots based on Selenium or WebKit. These are certainly good for promotional shots, but they apparently cannot cover the general usecase on how users see web pages.
Perhaps it would be interesting to be able to automate screenshots from arbitrary pages as seen by a Windows 95 IE 4, FF 1 or Debian Potato with Chimera etc. I'll have a look into this ;)
Note that there is another posting on Stackoverflow from me.
There is something I call
EasyRFB
which I wrote in a mix ofPython
(core),PHP
(small web helpers)AJAX
andbash
(control scripts). It was tested withXVnc
but shall work with anyVNC
. It somehow is able to do what you want, but beware, it is development code, undocumented, complex and horribly written and may be faulty. You can find it there:It was written for following purposes:
It was not written to be fast nor efficient nor easy to use nor for others (yet). It works, for me, somehow.
What it does:
AJAX
Web frontend toVNC
servers, somehow..jpg
updated to the contents of the screen.edit.html
- however this editor is basic, not self-explanatory and not documented at allVNC
.sh
scripts which could help to understand how this is done, but I am not able to publish the really helpful stuff yet, sorryNothing is documented and I am not able to explain how to use it. Either you find out yourself or you are lost. Sorry. Better don't even look at it, it might hurt you.
And now to the problems noticed with FF while doing all this:
Newer FF versions fail to properly update the screen. The last version known working correctly at my side was the
FF 3.6
series, all later sometimes develop some strange behavior.For example:
So even looking at the spinner to become gray including(!) naive Greasemonkey solutions which notify about "onload()" both fail with
FF 4.x
and above, because parts of the screen may still not be updated when you detect that it is ready.Maybe it has only to do with
XVnc
, perhaps there is a bug in my scripts which triggers all this, but withFF 3.x
everything was right and I think, reverting to an old version would immediately solve all those problems seen. (But I already have workarounds in place for all those trouble. But I cannot publish that scripts, sorry.)It gets worse.
FF 4.x
and above apparently delay event processing a bit. So jumping the mouse out of the scrollbar often keeps it highlighted, I think the event does not reach the scrollbar.On a normal desktop you do not jump your mouse, but my solution is able to do so. The fix is to generate additional, superfluous mouse movement events to have FF do the right thing.
Also I noticed, I really have no idea if it is due to FF or the web pages shown in FF, that sometimes things do not land where they are supposed to be. I never observed this before with
FF 3.x
though. The trick which solves this is to reload the page. It might have to do with incremental screen updates, but I did not dive into this yet as the retry (automated clicking on the reload button) works for me.我想到了三种方法。
window.onload
并触发图像采集器。window.onload
并触发警报的 Greasemonkey 脚本。等待此类警报,然后运行图像采集器。Three methods off the top of my head.
window.onload
and fires the image grabber.window.onload
and fires an alert. Wait for such alert, then run your image grabber.