2017-06-05 34 views
0

我目前正在閱讀使用Python自動化煩人的東西,並試圖學習如何使用硒。目前,我只是試圖打開一個網頁,甚至無法讓它工作。我知道有更簡單的方法來使用python啓動網頁,但我的目標是稍後使用網頁內容,所以這就是我使用硒的原因。使用硒啓動Firefox的網頁

from selenium import webdriver 
driver = webdriver.Firefox() 
driver.get("http://www.python.org") 

當我運行這段代碼,但它成功地啓動Firefox,但它不打開網頁我指定。這個錯誤也被返回。

Traceback (most recent call last): 
    File "/Users/lbor/Desktop/se.py", line 2, in <module> 
    driver = webdriver.Firefox() 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/firefox/webdriver.py", line 80, in __init__ 
    self.binary, timeout) 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/firefox/extension_connection.py", line 52, in __init__ 
    self.binary.launch_browser(self.profile, timeout=timeout) 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/firefox/firefox_binary.py", line 68, in launch_browser 
    self._wait_until_connectable(timeout=timeout) 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/firefox/firefox_binary.py", line 108, in _wait_until_connectable 
    % (self.profile.path)) 
selenium.common.exceptions.WebDriverException: Message: Can't load the profile. Profile Dir: /var/folders/4c/gsw7v5b118j406zxj3lxctt40000gn/T/tmp_dgwff4s If you specified a log_file in the FirefoxBinary constructor, check it for details. 

我不明白這個問題是什麼或如何去解決它。我運行OS 10.12.5,Python 3.6,Selenium 2.53.6,Firefox 53.0.3。至於geckodriver,我不知道那是什麼或如何安裝它。

回答

0

您可以安裝geckodriver這裏,通過選擇適合您的計算機的相應版本:

https://github.com/mozilla/geckodriver/releases 

然後,在你的代碼,您通過計算機上geckodriver的位置到Firefox方法的完整路徑:

browser = webdriver.Firefox(executable_path="/Users/username/Location/geckodriver") 

browser.get("https://google.com") #this will load the the google homepage. 
#you can specify any page you want here. 
+0

我只是試過這個,但它仍然只是啓動Firefox,但不是慾望網頁 – Lmorj

+1

請看我最近的編輯。 – Ajax1234

+0

非常感謝您的幫助Ajax,我知道我是一個noob,但你的解釋確實有幫助。我解決了它。 – Lmorj

1

您可以從here下載geckodriver。

之後,你需要加載它,使用:

geckodriver = os.path.dirname(os.path.realpath(__file__)) + "/geckodriver" 

drv = webdriver.Firefox(geckodriver) 
+0

我剛試過這,但它仍然只是啓動Firefox,但不是慾望網頁 – Lmorj

+0

你下載了哪個geckodriver版本? –

+0

我正在使用v0.16.1。 – Lmorj