Search code examples
pythonseleniumfirefoxuser-agentfirefox-profile

How to change firefox profile preference after defining webdriver using Selenium and Python


How to change profile preference after defining driver?

profile = webdriver.FirefoxProfile()

driver = webdriver.Firefox(firefox_profile=profile)

After some code need to set useragent

profile.set_preference("general.useragent.override", ua)

How to set it without defining new driver?


Solution

  • As per the current implementation of Selenium once you configure the GeckoDriver with specific Capabilities and initialize the session to open a Browsing Context, you cannot change the capabilities runtime. Even if you are able to retrieve the runtime capabilities still you won't be able to change them back.

    So, in-order to change the Firefox User Preference you have to initiate a new WebDriver session.

    Note:However, you can change the for Firefox on each run and you can find a relevant discussion in How to change user agent for Firefox webdriver in Python?


    Reference

    Here is @JimEvans clear and concise comment (as of Oct 24 '13 at 13:02) related to proxy settings capability:

    When you set a proxy for any given driver, it is set only at the time WebDriver session is created; it cannot be changed at runtime. Even if you get the capabilities of the created session, you won't be able to change it. So the answer is, no, you must start a new session if you want to use different proxy settings.


    Outro

    You can find a couple of relevant detailed discussions in: