如何正确设置Windows7,使之与Firefox一起使用Selenium[TDD with Python]?

2024-10-03 11:23:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我的系统(windows7pro64位,python3.5通过Anaconda)设置为通过selenium使用Firefox,遵循Python的测试驱动开发这本书。Python仍然抛出错误WebDriverException: 'geckodriver.exe' executable needs to be in PATH.,即使我已经将我的系统路径设置为指向geckodriver的文件夹(并重新启动了3次)。在

如果我将python/selenium指向geckodriver.exe的确切位置,就会得到以下错误

OSError: [WinError 216] This version of %1 is not compatible with the
version of Windows you're running. Check your computer's system information
to see whether you need a x86 (32-bit) or x64 (64-bit) version of the program,
and then contact the software publisher

现在我不确定错误版本%1是否与firefox版本错误(我尝试过64位和32位)、geckodriver、selenium或其他完全不同的版本有关。在


Tags: oftheto版本youversion系统selenium
2条回答

请确保您已经下载了GeckoDriver for win-64 bit,因为您有64位计算机。现在复制粘贴下载的GeckoDriver可执行文件在'Script'文件夹中(这个文件夹位于系统中安装python的根文件夹中)。现在在环境变量中设置python根文件夹和“Script”文件夹的路径

C:\..Python; //path of python root folder 
C:\..Python\Scripts; //path of python 'Script' folder

enter image description here

别忘了重新启动系统以使更改生效&请尝试以下示例代码

^{pr2}$
  1. Selenium用于引用名为wires.exegithub-geckodriver issue 90)的驱动程序。从Selenium3起,该驱动程序已替换为geckodriver.exe。通过运行pip install "selenium>=3.0.0"安装/升级到最新的selenium
  2. Download the latest geckodriver适用于您的平台:截至本文撰写之时,64位的{}或32位的{}。在您的例子中,version %1错误与错误的geckodriver版本有关。将此zip解压缩到C:\Users\YourUserName\Downloads\selenium_driver
  3. 安装Firefox Extended Support Release,将自定义安装路径设置为C:\Program Files\Mozilla FirefoxESR(如果是64位的话)或{}(如果是32位的话)。在

如果setting the Windows PATH到{}似乎不起作用(这样selenium可以找到geckdriver.exe),则可以改为在Python脚本中指定其目录,如下所示:

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
gecko = r'C:\Users\YourUserName\Downloads\selenium_driver\geckodriver.exe'
ffox_binary = FirefoxBinary(r'C:\Program Files\Mozilla FirefoxESR\firefox.exe') #for 64 bit installation
#ffox_binary = FirefoxBinary(r'C:\Program Files (x86)\Mozilla FirefoxESR\firefox.exe') #for 32 bit installation
browser = webdriver.Firefox(firefox_binary=ffox_binary, executable_path=gecko)  
browser.get('http://localhost:8000')

相关问题 更多 >