Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何正确设置Windows7以在Firefox中使用Selenium[TDD with Python]?_Python_Selenium - Fatal编程技术网

如何正确设置Windows7以在Firefox中使用Selenium[TDD with Python]?

如何正确设置Windows7以在Firefox中使用Selenium[TDD with Python]?,python,selenium,Python,Selenium,我正在让我的系统(Windows7Pro 64位,Python 3.5到Anaconda)设置通过selenium使用Firefox,以遵循Python测试驱动开发这本书。Python不断抛出错误WebDriverException:“geckodriver.exe”可执行文件需要位于路径中。即使我已将系统路径设置为指向geckodriver的文件夹(并重新启动了3次) 如果我将python/selenium指向geckodriver.exe的确切位置,我会得到以下错误 OSError: [Wi

我正在让我的系统(Windows7Pro 64位,Python 3.5到Anaconda)设置通过selenium使用Firefox,以遵循Python测试驱动开发这本书。Python不断抛出错误
WebDriverException:“geckodriver.exe”可执行文件需要位于路径中。
即使我已将系统路径设置为指向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或其他完全错误的版本有关

  • Selenium用于引用名为
    wires.exe
    ()的驱动程序。从
    Selenium3
    开始,该驱动程序已替换为
    geckodriver.exe
    。通过运行
    pip安装“selenium>=3.0.0”
  • 对于您的平台:在撰写本文时,64位的是
    geckodriver-v0.11.1-win64.zip
    ,32位的是
    geckodriver-v0.11.1-win32.zip
    。在您的情况下,
    版本%1
    错误与不正确的geckodriver版本有关。将此zip文件解压缩到
    C:\Users\YourUserName\Downloads\selenium\u driver
  • 如果是64位,则安装将自定义安装路径设置为
    C:\Program Files\Mozilla FirefoxESR
    ,如果是32位,则安装
    C:\Program Files(x86)\Mozilla FirefoxESR
  • 如果to
    C:\Users\YourUserName\Downloads\selenium\u driver
    似乎不起作用(因此
    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')
    

    确保您已经下载了64位计算机。现在将下载的GeckoDriver可执行文件复制粘贴到“Script”文件夹中(此文件夹位于系统中安装python的根文件夹中)。现在在环境变量中设置python根文件夹和“Script”文件夹的路径

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

    不要忘记重新启动系统,以使更改生效&请尝试此示例代码

    from selenium import webdriver
    driver = webdriver.Firefox()
    driver.get("http://google.com")