What is the difference between webdriver.Firefox() and webdriver.Firefox() in selenium?

前端 未结 2 463
夕颜
夕颜 2021-01-27 07:53

I looked through the selenium 3 for python documentation but still couldn’t get the difference between these two different driver calls.

webdriver.Firefox() 
         


        
2条回答
  •  北恋
    北恋 (楼主)
    2021-01-27 08:33

    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')
    

提交回复
热议问题