python - 带有 Selenium 的 Firefox 空白网络浏览器

标签 python python-3.x selenium firefox

当我使用 python firefox webdriver 调用 firefox webbrowser 时,firefox 打开时显示空白页面(导航栏中没有任何内容),等待并关闭。

The python consol give me this error : Traceback (most recent call last):

File "firefox_selenium2.py", line 4, in <module>
  driver = webdriver.Firefox()   
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/firefox/webdriver.py", line 80, in __init__
  self.binary, timeout)   
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/firefox/extension_connection.py", line 52, in __init__
  self.binary.launch_browser(self.profile, timeout=timeout)   
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 68, in launch_browser
  self._wait_until_connectable(timeout=timeout)   
File "/usr/local/lib/python3.5/dist-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: /tmp/tmpngm7g76x 
If you specified a log_file in the FirefoxBinary constructor, check it for details.

我的代码是来自 python selenium read_the_doc 的示例:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.clear()
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.close()

任何帮助将不胜感激

附言:Firefox 版本 49 Selenium 版本 2.53.6 python 3.5

最佳答案

根据这篇文章 https://github.com/SeleniumHQ/selenium/issues/2739#issuecomment-249479530 这是你需要使用一种叫做 Gecko Driver 的东西,可以在这里找到 https://github.com/mozilla/geckodriver .其他人也成功地回到了 Firefox 的早期版本(48 之前)。我也遇到了这个问题,实际上并不了解如何解决这两个问题,而且进展缓慢。

嗨,Dennis,我会一步步发布我的解决方案,现在我已经开始工作了。

逐步解决方案

问题是 Selenium 和 Firefox 不再相互支持。我实际上不明白为什么,但希望有人能比我更详细地评论和解释。有两种可能的解决方案。一个是安装一个叫做 Geckodriver 的东西。我安装了它,但很难将它添加到我的 PATH 中,并且通常发现自己很沮丧。

相反 我选择了一条更简单的路线。 首先我用命令卸载了 firefox

sudo apt-get purge firefox

然后我下载了Firefox 47.0.1从这里(我选择了美国英文版)。然后我将它从我的下载文件夹移动到我的主文件夹。然后我使用这个命令提取它。

tar xjf firefox-47.0.1.tar.bz2

您的 Firefox 号码可能与我的不同。然后我进入那个目录

cd firefox    

这让我进入了那个目录。然后剩下的就是运行命令了

sudo apt install firefox 

之后我再次使用了 Selenium 版本。很高兴我回到了编写代码而不是配置东西!

关于python - 带有 Selenium 的 Firefox 空白网络浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39711674/

相关文章:

python3.5 --version 命令未找到 CentOS 7

python - 错误 : "Undefined symbol" when trying to import pyarrow/feather

python - 如何使用 python 复制和提取 .gz 文件

python - "The handshake operation timed out"与 urllib,适用于请求

相同unicode的python不同长度

python - 在 Python 中通过 strptime 解析不带前导零的时间

python - 使用 python 逐文本比较两个不同的文件

selenium - 如何通过Selenium打开Chrome浏览器控制台?

python - 获取 chrome 性能和跟踪日志

c# - Selenium 在特定的 div 标记后单击其他 div(在 Xpath 中指定)