问题
I looked through the selenium 3 for python documentation but still couldn’t get the difference between these two different driver calls.
webdriver.Firefox()
and
webdriver.Firefox(<path to gecko executable >)
when using Selenium?
Since I’m working on a web scrapping project this could help me a lot .
回答1:
If you define the path for the driver, Functional call look for the file in the path and works accordingly. Second function driver.Firefox() / driver.Chrome()
looks for existing software in the system. if the browser is not present you'll be getting following error.
WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
My take is always use absolute path for the driver definition.
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)
回答2:
webdriver.Firefox()
As per the documentation in selenium.webdriver.firefox.webdriver the following line is the default constructor.
driver = webdriver.Firefox()
While you use the default constructor your script/program expects that the underlying OS PATH variable to contain the absolute path of the GeckoDriver which will start a new local session of Firefox Browser
webdriver.Firefox(executable_path=r'C:\path\to\geckodriver.exe')
Again, as per the documentation of selenium.webdriver.firefox.webdriver the complete/full signature of webdriver.Firefox() is as follows:
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)
This achieves the following:
- Starts a new local session of Firefox.
- Based on the combination and specificity of the various keyword arguments, a capabilities dictionary will be constructed that is passed to the remote end.
- The keyword arguments given to this constructor are helpers to more easily allow Firefox WebDriver sessions to be customised with different options. They are mapped on to a capabilities dictionary that is passed on to the remote end.
- As some of the options, such as firefox_profile and options.profile are mutually exclusive, precedence is given from how specific the setting is. capabilities is the least specific keyword argument, followed by options, followed by firefox_binary and firefox_profile.
- In practice this means that if firefox_profile and options.profile are both set, the selected profile instance will always come from the most specific variable. In this case that would be firefox_profile. This will result in options.profile to be ignored because it is considered a less specific setting than the top-level firefox_profile keyword argument. Similarily, if you had specified a capabilities[“moz:firefoxOptions”][“profile”] Base64 string, this would rank below options.profile.
So incase your Test Suite includes testcases with multiple version of GeckoDriver, options, Firefox Profiles and Capabilities, you can always specifically mention them while initializing the new WebDriver instance and Web Browsing session.
As an example, if you place geckodriver.exe v0.21.0 within C:\\geckodriver_0_21_0\\
you can mention as follows:
# 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')
来源:https://stackoverflow.com/questions/52197632/what-is-the-difference-between-webdriver-firefox-and-webdriver-firefoxpath-t