2016-11-07 61 views
2

我已经在centos上安装了Firefox和Selenium。我使用Xvfb和pyvirtualdisplay来打开浏览器。Permission denied:'geckodriver.log'在python中运行selenium webdriver时

当我尝试运行硒webdriver的,我能打开一个新的显示器,但只要我做

browser = webdriver.Firefox()

我得到的错误:

File "<stdin>", line 1, in <module> 
    File "/usr/lib/python2.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 134, in __init__ 
    self.service = Service(executable_path, log_path=log_path) 
    File "/usr/lib/python2.7/site-packages/selenium/webdriver/firefox/service.py", line 45, in __init__ 
    log_file = open(log_path, "a+") 
IOError: [Errno 13] Permission denied: 'geckodriver.log' 

任何线索在这里发生了什么问题?

编辑:克服权限错误后,我越来越

Message: 'geckodriver' executable needs to be in PATH

+1

运行该脚本的用户是否有权在脚本的路径中创建文件'geckodriver.log'? –

+0

好吧,你显然不应该写入这个日志文件,因为它已经被运行在PC上的另一个进程打开了(让我猜测它是你自己的使用硒的Python程序)。 –

+0

@IvanChaer:我以超级用户的身份登录,并且使用“sudo”安装了Selenium。我正在python shell中运行命令,并且要求作为webdriver的许可的脚本。 –

回答

2

显然,这可以来自你的Firefox和您的硒之间的不兼容。尝试pip install --upgrade selenium,如果错误仍然存​​在,请尝试downloading a different version of Firefoxgecko driver

关于消息:

'geckodriver' executable needs to be in PATH 

您可以设置驱动器的路径上的脚本:

ff_profile_dir = "/usr/local/selenium/webdriver/firefox" 
ff_profile = selenium.webdriver.FirefoxProfile(profile_directory=ff_profile_dir) 
driver = selenium.webdriver.Firefox(ff_profile) 

或者,根据this answer,你都可以在Unix系统上运行,在bash兼容的外壳:

export PATH=$PATH:/path/to/directory/of/executable/downloaded/in/previous/step 

On Windows you will need to update the Path system variable to add the full directory path to the executable geckodriver manually or command line(don't forget to restart your system after adding executable geckodriver into system PATH to take effect). The principle is the same as on Unix.

+0

谢谢。如前所述,我在PATH中加入了geckodriver。然后,我做'browser = webdriver.Firefox()',我得到了 'selenium.common.exceptions.WebDriverException:消息:连接被拒绝' –

+0

将geckodriver移动到/ usr/local/bin中。非常感谢! –

1

我遇到了同样的问题。我试着用Ivan Chaer的回答,但它没有奏效。我试了很多其他的东西,直到我终于遇到了一个工作解决方案。我一直试图运行Selenium的时候,我一直在使用交互式shell。当我尝试将代码放在脚本中,然后运行它时,一切正常。

然后我注意到一个名为“geckodriver.log”的文件已经在脚本的同一个目录中创建。这给了我一个主意。我在C:\ Program Files \ Python36中创建了一个“geckodriver.log”文件,然后在Firefox中使用Selenium不再抛出任何错误。

0

以下解决方案适用于我。在我的情况下,我从Windows Notepad++应用程序中启动我的Python脚本。我的geckodriver.exe位于PATH中,恰巧位于我的C:\Python 27文件夹中。还提供了完整的Firefox路径。硒,geckodriver和Firefox都是最新版本。

它变成了geckodriver.log文件正在这里创造:

C:\Program Files (x86)\Notepad++\geckodriver.log 

我发现这个通过运行Notepad++以管理员身份。然后就可以在需要的地方创建文件。然后我找到该文件并将文件权限更改为Windows上正常用户帐户的完全访问权限(它被设置为只读)。

做完这些之后,我能够正常运行Notepad ++,错误消失了。

相关问题