Python 2.7 - Selenium无头浏览器测试使用Xvfb无法工作

6

请注意,我已经查看了以下来源:

easyprocess.EasyProcessCheckInstalledError: cmd=['Xvfb','-help'] OSError=[Errno 2] No such file or directory

如何在Xvfb中运行Selenium?

它们都没有解决我的问题。

我正在使用:

  • Python 2.7
  • selenium 3.0.2
  • xvfbwrapper 0.2.9
  • PyVirtualDisplay 0.2.1

运行在Mac OSX El Capitan (10.11.6 (15G1108))上。

我收到以下错误(与上面链接中的错误相同):

Traceback (most recent call last):
  File "/Users/user/Desktop/test.py", line 50, in <module>
    display = Display(visible=0, size=(800, 600))
  File "/Library/Python/2.7/site-packages/pyvirtualdisplay/display.py", line 34, in __init__
    self._obj = self.display_class(
  File "/Library/Python/2.7/site-packages/pyvirtualdisplay/display.py", line 52, in display_class
    cls.check_installed()
  File "/Library/Python/2.7/site-packages/pyvirtualdisplay/xvfb.py", line 38, in check_installed
    ubuntu_package=PACKAGE).check_installed()
  File "/Library/Python/2.7/site-packages/easyprocess/__init__.py", line 180, in check_installed
    raise EasyProcessCheckInstalledError(self)
EasyProcessCheckInstalledError: cmd=['Xvfb', '-help']
OSError=[Errno 2] No such file or directory
Program install error! 

我知道我也安装了Xvfb:

Xvfb -help
use: X [:<display>] [option]
-a #                   default pointer acceleration (factor)
-ac                    disable access control restrictions
-audit int             set audit trail level
-auth file             select authorization file
-br                    create root window with black background
+bs                    enable any backing store support
-bs                    disable any backing store support
-c                     turns off key-click
...
-pixdepths list-of-int support given pixmap depths
+/-render          turn on/off RENDER extension support(default on)
-linebias n            adjust thin line pixelization
-blackpixel n          pixel value for black
-whitepixel n          pixel value for white
-fbdir directory       put framebuffers in mmap'ed files in directory
-shmem                 put framebuffers in shared memory

如果 pip install 在不同的目录中查找 xvfb,希望能提供一些指导以重定向它的查找路径。
以下是我的代码:
from easyprocess import EasyProcess
from pyvirtualdisplay import Display
from selenium import webdriver

display = Display(visible=0, size=(800, 600))
display.start()

# now Firefox will run in a virtual display. 
# you will not see the browser.
browser = webdriver.Firefox()
browser.get('http://www.google.com')
print browser.title
browser.quit()

display.stop()

我已经进行了无数次pip安装和谷歌搜索,但目前为止没有什么帮助,最终目标是拥有一个无头自动化程序,能够处理下载,PhantomJS无法做到这一点。如有任何关于此主题的帮助将不胜感激。


你不能在OSX上使用这个。 - Corey Goldberg
@CoreyGoldberg 我能用什么?我已转向使用casperjs,但如果我想使用Python,在OSX上如何以无头模式运行它? - Q.H.
Chrome刚刚发布了59+版本的无头浏览器。你可以去看看。 - demouser123
1个回答

2
我不明白为什么你不能在OSX上使用这个,我过去曾经这样做过。但我有一个更好的解决方案。为什么不使用docker + VNC。安装您选择的任何浏览器并在后台运行所有内容。 优势: 1. 一切都在后台运行。 2. 使用VNC Web视图在浏览器中监视执行情况 3. 启动尽可能多的容器以进行并行执行。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接