New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't set preferences with selenium-python #27

Closed
marco-c opened this Issue Nov 8, 2015 · 22 comments

Comments

Projects
None yet
9 participants
@marco-c
Member

marco-c commented Nov 8, 2015

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.keys import Keys

firefox_profile = webdriver.FirefoxProfile()
firefox_profile.set_preference("general.useragent.override", "Mozilla/5.0 (Android 4.4; Mobile; rv:41.0) Gecko/41.0 Firefox/41.0")

firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
firefox_capabilities['binary'] = 'tools/firefox/firefox-bin'

driver = webdriver.Firefox(firefox_profile=firefox_profile, capabilities=firefox_capabilities)

This works (i.e. the user agent is changed) if I remove the capabilities part.

@jgraham

This comment has been minimized.

Collaborator

jgraham commented Nov 8, 2015

This is an extension to the core specification that wires doesn't yet support. It seems reasonable to add though. @AutomatedTester

@AutomatedTester

This comment has been minimized.

Member

AutomatedTester commented Nov 10, 2015

@jgraham this is something that we will need to add.

What is currently done is a through capabilities

firefox_profile = webdriver.FirefoxProfile()
firefox_profile.set_preference("general.useragent.override", "Mozilla/5.0 (Android 4.4; Mobile; rv:41.0) Gecko/41.0 Firefox/41.0")
firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
firefox_capabilities['binary'] = 'tools/firefox/firefox-bin'
# send over profile. It is a 
firefox_capabilities['firefox_profile'] = firefox_profile.encoded

We zip up the profile directory and then base64 encode it. You can see what the python code does at https://github.com/SeleniumHQ/selenium/blob/master/py/selenium/webdriver/firefox/firefox_profile.py#L162

@EricRahm

This comment has been minimized.

EricRahm commented Jan 22, 2016

@AutomatedTester So just to be clear, that's not a workaround correct? AFAICT the profile is hardcoded in src/marionette.rs.

@jgraham

This comment has been minimized.

Collaborator

jgraham commented Jan 23, 2016

Yeah, this extension isn't implemented yet.

@jgraham

This comment has been minimized.

Collaborator

jgraham commented Mar 11, 2016

https://github.com/jgraham/wires/tree/profile is an in-progress implementation of this. I don't expect it to be fully FirefoxDriver compatible yet, but I would appreciate feedback on whether it works for your use case.

@marco-c

This comment has been minimized.

Member

marco-c commented Mar 30, 2016

https://github.com/jgraham/wires/tree/profile is an in-progress implementation of this. I don't expect it to be fully FirefoxDriver compatible yet, but I would appreciate feedback on whether it works for your use case.

Loading an encoded profile works for me (although it isn't exactly the code from the first comment, which means some existing tests will need to be rewritten).

@jgraham

This comment has been minimized.

Collaborator

jgraham commented Mar 30, 2016

What change did you have to make relative to the code in the first comment? This might be my bug or something @AutomatedTester can fix.

@marco-c

This comment has been minimized.

Member

marco-c commented Mar 30, 2016

@jgraham I haven't tested your branch, I was only looking at the code. I thought your branch only added support for the firefox_profile capability.

@marco-c

This comment has been minimized.

Member

marco-c commented Mar 30, 2016

Ah, I guess the Python interface encodes the firefox_profile argument and adds it to the capabilities?

@jgraham

This comment has been minimized.

Collaborator

jgraham commented Mar 30, 2016

I think that's how it works, yes. But @AutomatedTester is the authority here.

@jgraham

This comment has been minimized.

Collaborator

jgraham commented Apr 21, 2016

This should be fixed now

@jgraham jgraham closed this Apr 21, 2016

@vladikoff

This comment has been minimized.

Member

vladikoff commented Apr 21, 2016

Awesome!!!

@ysreeram

This comment has been minimized.

ysreeram commented Jun 27, 2016

I just tested this feature and its not working for me

Using webdriver (python)

fp=FirefoxProfile()
fp.set_preference('media.navigator.permission.disabled', True)
fp.update_preferences()
caps = DesiredCapabilities.FIREFOX.copy()
caps["firefox_profile"] = fp.encoded
caps["marionette"] = True
webdriver.Firefox(capabilities=caps)

Now in the about:config I do not see the preference updates.

Note: I am starting geckodriver with --marionette-port <custom_port>

geckodriver version: 0.8.0
Webdriver: 2.53.5
firefox: beta, nightly

@ysreeram

This comment has been minimized.

ysreeram commented Jun 27, 2016

nevermind, problem is with the python webdriver. In order for the profile to work it is also required to send the same profile as argument to Firefox driver.

webdriver.Firefox(firefox_profile=fp, capabilities=caps)

it is a bug in the python implementation that it overwrites the caps["firefox_profile"] if firefox_profile arg is None.

@andreastt

This comment has been minimized.

Member

andreastt commented Jun 28, 2016

@ysreeram Yes, that looks fishy. Did you file a bug with the Selenium bindings?

@benklaasen

This comment has been minimized.

benklaasen commented Oct 14, 2016

Hi -

I'm attempting to set the browser User-Agent string to that of a mobile device. I can see that it's correctly set in the zipped-up and base64-encoded profile object included in the capabilities dictionary, and I'm providing the same modified profile object as the argument to firefox_profile when instantiating webdriver.Firefox , yet when I set a breakpoint in my test after Firefox has started and loaded the desired page, I can see that it's sending the unmodified User-Agent string. It's at this point that I'm able to examine the state of the profile objects.

Here's my code snippet to set up the Firefox driver:

@pytest.fixture(scope='module')
def selenium_firefox_driver_setup(request, browsermob_proxy_setup):

    useragent = "Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) " \
                "AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 " \
                "Mobile/13B143 Safari/601.1"
    profile = webdriver.FirefoxProfile()
    profile.set_preference("general.useragent.override", useragent)
    caps = DesiredCapabilities.FIREFOX
    caps["firefox_profile"] = profile.encoded
    caps["marionette"] = True
    caps["binary"] = str(subprocess.check_output(["which", "firefox"]),
                         "utf-8").strip()
    global driver
    driver = webdriver.Firefox(firefox_profile=profile,
                               proxy=bmp.selenium_proxy(),
                               capabilities=caps)

    def selenium_driver_teardown():
        driver.quit()

    request.addfinalizer(selenium_driver_teardown)

    return driver

I'm using the following components:

  • geckodriver 0.11.1
  • Python3 "selenium" library 2.53.6
  • Firefox 49.0

I'd love to know why this isn't working. Thanks for your time!

@andreastt

This comment has been minimized.

Member

andreastt commented Oct 15, 2016

@benklaasen

If you consult the changelog you will see that the accepted capabilities changed in 0.11. Specifically, the firefox_profile capability was replaced by a moz:firefoxOptions dictionary (akin to chromeOptions). You can see the README on how to use it.

The tl;dr is that you need to pass {"moz:firefoxOptions": {"profile": <base 64 encoded string>}} instead of {"firefox_profile": <base 64 encoded string>}.

I believe the latest Python bindings for Selenium 3 has an updated selenium.webdriver.firefox.Options class that makes it easier to construct this dictionary.

@benklaasen

This comment has been minimized.

benklaasen commented Oct 15, 2016

Hi Andreas -

Thanks very much for the prompt response and clear instructions!

regards
Ben

@swatisunimi

This comment has been minimized.

swatisunimi commented Oct 22, 2016

Hi,

I am facing one issue while implementing python with geckodriver. My snippet is wokring and able to locate element but when I am putting that snippet in my framework it is not able to locate any element.It just loads the page and stops and responds nothing.

  firefox_profile = webdriver.FirefoxProfile(os.path.join(os.path.split(currentpath)[0], file name))
  caps = desired_capabilities.DesiredCapabilities.FIREFOX.copy()
    caps['marionette'] = True
    caps['binary'] = 'C:\Program Files (x86)\Mozilla Firefox\\firefox.exe
    driver = webdriver.Firefox(firefox_profile=firefox_profile, capabilities=caps)

here caps= firefox_capbilities.
test environment
Selenium - 3.0.1
Firefox : 49.0.1
Python 27
frame work : pyunit testing framework.
Gecko latest version. v0.11.1

If any one can suggest here

@andreastt

This comment has been minimized.

Member

andreastt commented Oct 24, 2016

@swatisunimi The caps['binary'] capability you are using is not being picked up. See the README for correct use.

You can alternatively use the selenium.webdriver.firefox.Options helper class that populates the right keys of the capabilities dictionary and pass that in to the firefox_options keyword argument of webdriver.Firefox.

@swatisunimi

This comment has been minimized.

swatisunimi commented Nov 14, 2016

I couldn't get this in my working code and not able to do the same.

On Mon, Oct 24, 2016 at 7:04 PM, Andreas Tolfsen notifications@github.com
wrote:

@swatisunimi https://github.com/swatisunimi The caps['binary']
capability you are using is not being picked up. See the README for correct
use.

You can alternatively use the selenium.webdriver.firefox.Options helper
class that populates the right keys of the capabilities dictionary and pass
that in to the firefox_options keyword argument of webdriver.Firefox.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#27 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AUQVRYPAuT-_DiQvQtY0LywsKKkKawt_ks5q3LQDgaJpZM4GeENm
.

Thanks & Regards

Swati Chandra

@andreastt

This comment has been minimized.

Member

andreastt commented Nov 14, 2016

@swatisunimi That you “couldn’t get it working” is a totally unactionable bug report. Sorry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment