Xvfb + firefox:如何知道firefox完成渲染?

发布于 2024-12-07 09:02:17 字数 687 浏览 0 评论 0原文

我试图通过使用 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 技术交流群。

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

发布评论

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

评论(2

硪扪都還晓 2024-12-14 09:02:18

请注意,我认为使用 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。它以某种方式能够做你想做的事,但要注意,它是开发代码,没有文档记录,复杂且写得很糟糕,并且可能有错误。您可以在那里找到它:

它是出于以下目的而编写的:

  • 即使在互联网连接极其缓慢且极其不可靠的情况下,也能够从移动设备控制远程 GUI
  • 完整的 UI 自动化/用命令行替换鼠标
  • 自动 QA 测试,基于用户所看到的内容与浏览器或某些 Windows 间谍程序所看到的内容的对比,在输出也不需要像素颜色完美的情况

下写成快速、高效、易于使用、也不适合其他人(还)。对我来说,它以某种方式起作用。

它的作用:

  • 以 Web 为中心,并以某种方式为 VNC 服务器提供 AJAX Web 前端。
  • 将高度压缩的 .jpg 保持更新到屏幕内容。
  • 可以进行无损屏幕截图
  • 能够根据屏幕内容模糊匹配模板
  • 可以使用 edit.html 进行 Web 编辑 - 但是此编辑器是基本的,不言自明的,根本没有文档记录
  • 可以编写 Shell 脚本,等待这些模板匹配并将按键、鼠标移动和单击等命令发送到 VNC
  • 有一些 .sh 脚本可以帮助您了解如何这已经完成了,但是我我还无法发布真正有用的东西,抱歉

没有任何记录,我无法解释如何使用它。要么你发现自己,要么你就迷失了。对不起。最好别看它,它可能会伤害你。


现在来看看 FF 在执行所有这些操作时注意到的问题:

较新的 FF 版本无法正确更新屏幕。我身边已知的最后一个正常工作的版本是 FF 3.6 系列,后来有时会出现一些奇怪的行为。

例如:

  • 较新的版本现在会在更新屏幕内容之前更新状态。
  • 此外,它们对 CPU 和内存的需求如此之高,以至于完成更新屏幕的最后一步可能需要一些时间。在慢速硬件上(例如 NSLU2,从未测试过),我认为在 Spinners 之后甚至可能需要几分钟等已经处于“完成”状态。

因此,即使看着旋转器变成灰色,包括(!)通知“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 of Python (core), PHP (small web helpers) AJAX and bash (control scripts). It was tested with XVnc but shall work with any VNC. 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:

  • Being able to control a remote GUI from a Mobile even in a situation where the Internet connection is extremely slow and extremely unreliable
  • Complete UI automation / replace mouse by commandline
  • Automated QA tests, based on what the user sees in contrast to what the browser or some windows spy program sees, in cases where the output needs not to be pixel-color-perfect as well

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:

  • is Web centric and gives a AJAX Web frontend to VNC servers, somehow.
  • keeps a highly compressed .jpg updated to the contents of the screen.
  • can take lossless screenshots
  • is able to fuzzy-match templates against the contents of the screen
  • templates can be Web-edited with edit.html - however this editor is basic, not self-explanatory and not documented at all
  • Shell scripts can be written which can wait for these templates to match and send commands like keypresses, mousemoves and clicks to VNC
  • There are some .sh scripts which could help to understand how this is done, but I am not able to publish the really helpful stuff yet, sorry

Nothing 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:

  • Newer versions now update the status before they update the screen contents.
  • Also they have such a high CPU and memory demand, that finishing the last step on updating the screen can take some time. On slow hardware (like the NSLU2, never tested) I think it might even take minutes after the Spinners etc. are already in the "finished" state.

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 with FF 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.

哆啦不做梦 2024-12-14 09:02:17

我想到了三种方法。

  1. 编写您自己的扩展来挂钩 window.onload 并触发图像采集器。
  2. 编写一个连接到 window.onload 并触发警报的 Greasemonkey 脚本。等待此类警报,然后运行图像采集器。
  3. 定期抓取“停止”按钮的图像(应位于相对于窗口的已知坐标)并分析其颜色。如果禁用,则渲染完成。

Three methods off the top of my head.

  1. Write your own extension that hooks to window.onload and fires the image grabber.
  2. Write a greasemonkey script that hooks to window.onload and fires an alert. Wait for such alert, then run your image grabber.
  3. Periodically grab the image of the Stop button (should be at known coordinates relative to the window) and analyze its color. If it's disabled, the rendering is finished.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文