【问题标题】:Launching a webpage with Firefox using selenium [duplicate]使用 selenium 使用 Firefox 启动网页 [重复]
【发布时间】:2017-11-06 12:52:11
【问题描述】:

我目前正在阅读 Automate the Boring Stuff with Python 并尝试学习如何使用 Selenium。现在,我只是想打开一个网页,甚至无法让它工作。我知道使用 python 启动网页有更简单的方法,但我的目标是稍后处理网页内容,这就是我使用 selenium 的原因。

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,我不知道那是什么,也不知道怎么安装。

【问题讨论】:

    标签: python selenium firefox


    【解决方案1】:

    您可以在此处安装 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.
    

    【讨论】:

      【解决方案2】:

      您可以从here下载geckodriver。

      之后,你需要加载它,使用:

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

      【讨论】:

      • 我刚试过这个,但它仍然只是启动 Firefox 而不是所需的网页
      • 您下载了哪个 geckodriver 版本?
      • 我使用的是 v0.16.1。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-21
      • 2015-02-10
      • 2015-02-28
      • 1970-01-01
      • 2017-07-30
      • 2011-11-20
      相关资源
      最近更新 更多