硒中的webdriver.Firefox()和webdriver.Firefox(<path =“ =” gecko =“” executable =“”>)有什么区别?

时间:2018-09-06 06:18:24

标签: firefox selenium-webdriver web-scraping webdriver geckodriver

我浏览了Selenium 3以获得python文档,但仍然无法理解这两个不同的驱动程序调用之间的区别。

webdriver.Firefox() 

webdriver.Firefox(<path to gecko executable >) 

何时使用硒?

由于我正在从事网络抓取项目,因此对我有很大帮助。

2 个答案:

答案 0 :(得分:0)

如果定义驱动程序的路径,则Functional call在路径中查找文件并相应地工作。第二个功能driver.Firefox() / driver.Chrome()在系统中查找现有软件。如果浏览器不存在,则会出现以下错误。

 WebDriverException: Message: 'geckodriver' executable needs to be in PATH. 

我的观点是始终使用绝对路径定义驱动程序。

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
binary = FirefoxBinary('path/to/installed firefox binary')
driver = webdriver.Firefox(firefox_binary=binary)

答案 1 :(得分:0)

webdriver.Firefox()

根据selenium.webdriver.firefox.webdriver中的文档,以下行是默认构造函数

driver = webdriver.Firefox()

在使用默认构造函数时,脚本/程序希望底层的 OS PATH 变量包含绝对路径 > GeckoDriver ,它将启动Firefox浏览器的新本地会话


webdriver.Firefox(executable_path = r'C:\ path \ to \ geckodriver.exe')

同样,根据selenium.webdriver.firefox.webdriver的文档, webdriver.Firefox()的完整/完整签名如下:

class selenium.webdriver.firefox.webdriver.WebDriver(firefox_profile=None, firefox_binary=None, timeout=30, capabilities=None, proxy=None, executable_path='geckodriver', options=None, service_log_path='geckodriver.log', firefox_options=None, service_args=None, desired_capabilities=None, log_path=None)

这可以达到以下目的:

  • 启动Firefox的新本地会话。
  • 基于各种关键字参数的组合和特殊性,将构建功能字典,并将其传递到远端。
  • 赋予此构造函数的关键字参数是帮助程序,可以更轻松地允许使用不同的选项来自定义Firefox WebDriver会话。它们被映射到功能字典,该字典将传递到远端。
  • 由于某些选项(例如firefox_profile和options.profile)是互斥的,因此优先级取决于设置的具体程度。功能是最不具体的关键字参数,其后是选项,其后是firefox_binary和firefox_profile。
  • 实际上,这意味着如果同时设置了firefox_profile和options.profile,则所选的配置文件实例将始终来自最特定的变量。在这种情况下,将是firefox_profile。这将导致options.profile被忽略,因为它被认为是比顶层firefox_profile关键字参数更具体的设置。同样,如果您指定了一个功能[“ moz:firefoxOptions”] [“ profile”] Base64字符串,则其排名将低于options.profile。

因此,如果您的 Test Suite 包含具有多个版本的 GeckoDriver 选项 Firefox的 testcases 配置文件功能,在初始化新的 WebDriver 实例和 Web浏览会话时,您始终可以特别提及它们。

作为示例,如果将 geckodriver.exe v0.21.0放在C:\\geckodriver_0_21_0\\中,则可以提及以下内容:

# Windows OS style
driver = webdriver.Firefox(executable_path=r'C:\geckodriver_0_21_0\geckodriver.exe')
# Linux OS style
driver = webdriver.Firefox(executable_path='path/to/geckodriver')