如何修复"无法连接到Mir" python-selenium中的错误?

时间:2018-02-13 11:13:18

标签: python selenium

我正准备开发一些selenium测试(使用无头firefox运行),当我通过此命令获取webdriver时,server.log中出现以下错误:

driver = webdriver.Firefox(firefox_profile = profile, log_path = logfile)

这是错误:

1518520162032   geckodriver::marionette INFO    Starting browser /usr/bin/firefox with args ["-marionette"]
Failed to connect to Mir: Failed to connect to server socket: No such file or directory
Unable to init server: Could not connect: Connection refused
Error: cannot open display: :1137

我不记得有什么改变。它之前只运行了一百次。如何解决这个错误???

  • python 2.7.12
  • Firefox:Mozilla Firefox 57.0.3
  • geckodriver:0.19.1
  • selenium:3.8.0

1 个答案:

答案 0 :(得分:0)

错误说明了一切:

Failed to connect to Mir: Failed to connect to server socket: No such file or directory
Unable to init server: Could not connect: Connection refused
Error: cannot open display: :1137

这实质上意味着未正确设置DISPLAY属性。

解决方案

在.bashrc中添加以下配置:

export DISPLAY=:0

Unntu 16.04上的Gnome,正确的值是 DISPLAY=:1 (不是DISPLAY=:0

问题可能是由于/ etc / sudoers中的env_keep变量。所以一个永久的解决方案是使用gedit或使用sudo visudo在/ etc / sudoers的末尾添加以下行:

Defaults env_keep+="DISPLAY"
  

注意:有关详细讨论,请参阅Failed to connect to Mir: Failed to connect to server socket: No such file or directory