简体   繁体   English

如何为 selenium 设置我的壁虎驱动程序?

[英]How can i set up my geckodriver for selenium?

Im trying to do the obeythetestinggoat tutorial and cant set my geckodriver,我正在尝试遵守测试山羊教程,但无法设置我的壁虎驱动程序,

Im working in Win10 64bits我在Win10 64位工作

my pip freeze shows:我的 pip 冻结显示:

Django==1.7,selenium==3.141.0,urllib3==1.25.7 Django==1.7,硒==3.141.0,urllib3==1.25.7

i download the geckodriver (geckodriver-v0.26.0-win64) when i try to get the geckodriver version (via $geckodriver --version ) stops and show me a error 'application error'当我尝试获取 geckodriver 版本(通过$geckodriver --version )停止并显示错误“应用程序错误”时,我下载了 geckodriver(geckodriver-v0.26.0-win64)

I think that the error are in the enviroment variables (i was trying to put the file in location where the variables are set (windows/system32 or python/scripts) but nothing works我认为错误出在环境变量中(我试图将文件放在设置变量的位置(windows/system32或python/scripts)但没有任何效果

i also trying this solution (put the file in some file where path are viable) in another computer and works.我也在另一台计算机上尝试了这个解决方案(将文件放在路径可行的某个文件中)并且可以工作。

I've solve it like this:我已经这样解决了:

  1. pip install webdriver-manager
  2. in Python i wrote following code and run it:在 Python 中,我编写了以下代码并运行它:

     from selenium import webdriver from webdriver_manager.firefox import GeckoDriverManager driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
  3. Now add the path below to your PATH variable (of course replace your_user_name with the one you have there:现在将下面的路径添加到您的 PATH 变量中(当然将your_user_name 替换为您那里的路径:

    c:\users\your_user_name\.wdm\drivers\geckodriver\v0.26.0\win64

    At this point geckodriver should be visible be Selenium from Python.此时 geckodriver 应该是可见的 Selenium 来自 Python。 Note: geckodriver does not show up when you run pip freeze注意:运行pip freeze时,geckodriver 不显示

STEP 1 > First You Need To Download The v0.26.0 version make sure you download for correct os if you are using window donwload for window and if you are using linux or other download fo them第 1 步 > 首先您需要下载 v0.26.0 版本,如果您使用 window 下载 window 和如果您使用 linux,请确保下载正确的操作系统

STEP2 > Put the geckodirver in your python directory STEP2 > 将 geckodirver 放到你的 python 目录下

Step3 > write this code Step3 > 编写这段代码

from selenium import webdriver

driver = webdriver.Firefox()
driver.get('your url')

This question have been cropping up pretty often recently.这个问题最近经常出现。 To burst the myth, you don't need to install geckodriver anyway.为了打破这个神话,你无论如何都不需要安装geckodriver

To set the you need to ensure a couple of things as follows:要设置 ,您需要确保以下几点:


Windows 10, 64 bits Windows 10, 64 位

Steps:脚步:

  • As the most recent GeckoDriver release being v0.26.0 , you can download either of the following binaries from Releases - mozilla/geckodriver page and save it within a sub-directory on your system eg within "C:\Utility\BrowserDrivers" :由于最新的GeckoDriver版本是v0.26.0 ,您可以从Releases - mozilla/geckodriver页面下载以下任一二进制文件,并将其保存在系统的子目录中,例如在"C:\Utility\BrowserDrivers"
    • geckodriver-v0.26.0-win64.zip : The 64-bit Windows OS compatible GeckoDriver geckodriver-v0.26.0-win64.zip : 64 位 Windows 操作系统兼容GeckoDriver
    • geckodriver-v0.26.0-win32.zip : The 32-bit Windows OS compatible GeckoDriver geckodriver-v0.26.0-win32.zip : 32 位 Windows 操作系统兼容GeckoDriver
  • Unzip the geckodriver-v0.26.0-winXY.zip file and extract the geckodriver.exe (eg "C:\Utility\BrowserDrivers\geckodriver.exe" )解压geckodriver-v0.26.0-winXY.zip文件并解压geckodriver.exe (eg "C:\Utility\BrowserDrivers\geckodriver.exe" )
  • Now you can open the command prompt, change the directory and issue the following command:现在您可以打开命令提示符,更改目录并发出以下命令:

     geckodriver --version
  • Output: Output:

geckodriver_cmd


Usage用法

Now you can use the GeckoDriver to initiate a Firefox Browsing session passing the absolute path of the GeckoDriver as an argument using the following lines of code:现在您可以使用GeckoDriver启动Firefox 浏览 session使用以下代码行将 GeckoDriver的绝对路径作为参数传递:

  • Python: Python:

     from selenium import webdriver driver = webdriver.Firefox(executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe', log_path='./Logs/geckodriver.log') driver.get('http://seleniumhq.org/') driver.quit()
  • Java: Java:

     import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class A_Firefox { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "C:/Utility/BrowserDrivers/geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://stackoverflow.com"); driver.quit(); } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM