4

我正在使用 Xvfb 在无头 Ubuntu 服务器上运行 Selenium,如此所述

我正在启动 Xvfb:

#!/bin/bash
disp=${1:-"99"}
/usr/bin/Xvfb :$disp -ac 2>&1 | tee /var/log/run-xvfb.log

我正在启动 Selenium-Server:

#!/bin/bash
disp=${1:-"0"}
export DISPLAY=":$disp" && java -jar selenium-server-standalone-2.21.0.jar 2>&1 | tee /var/log/run-selenium-server.log

我的启动代码如下所示:

from selenium import selenium
sel = selenium('localhost', 4444, '*firefox', 'http://www.google.com')
sel.start() # This takes forever!!!
<do stuff>

我发现它通常有效,但sel.start()可能需要 15 分钟或更长时间才能完成。奇怪的是,日志文件从未被写入,所以我不知道是否发生了任何错误。它似乎只是“挂起”。

当我在本地机器上运行相同的代码时,这也是 Ubuntu,但具有正常的桌面 GUI 设置,不到一分钟,所以我知道服务器上出现了可怕的错误。如何诊断问题所在并改善 Selenium 糟糕的性能?

4

1 回答 1

0

Your question does not contain enough details to give you a good answer but I can give you some tips that might help. Without logs this is an incredible hard problem to debug.

By default Selenium tries to emulate a real user as close as possible. This means that it will wait until pages are fully loaded before doing anything on them. A slow third-party script can cause long hangups.

If found that by default the Selenium BaseTest(unittest.TestCase) also contained this line:

self.driver.implicitly_wait(20)

Removing this line should speedup every test quite a bit. This line could be ignored by your normal Selenium setup (because it uses a different initialization).

For further speedups I recommend you take a look at PhantomJS. Even with optimization the Firefox test is going to take a few minutes.

于 2014-11-18T14:18:12.707 回答