2016-11-07 244 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 ++,錯誤消失了。

相關問題