ha.fi
ha.fi

Reputation: 21

How to add multiple experimental option in python selenium webdriver chrome?

I'd like to add two experimental_option to the selenium webdriver chrome options. This to run a test in an already open browser window AND with a mobile emulation active. I'm able to do them separately, but I'm not able to make them work at the same time. And now I'm asking myself if it's even possible, maybe not, but it's more likely that I don't know how to do it. Thanks in advance to those who want to give me some advice.

This takes the control of an already open browser window

opt = Options()
opt.add_experimental_option("debuggerAddress", "localhost:8989")
driver = webdriver.Chrome(executable_path = my_path, options = opt)
driver.get(URL)

This emulates a mobile, but in a new window

opt = Options()
opt.add_experimental_option("mobileEmulation", { "deviceName": "Pixel 2" })
driver = webdriver.Chrome(executable_path = mt_path, options = opt)
driver.get(URL)

Trying to make them work together I made a lot of attemps. Here some of them.

Adding twice the option results in an exception regardless of which one I put first:

opt.add_experimental_option("mobileEmulation", { "deviceName": "Pixel 2" })
opt.add_experimental_option("debuggerAddress", "localhost:8989")
#selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: cannot parse capability: goog:chromeOptions
#from invalid argument: unrecognized chrome option: mobileEmulation

Adding both as arguments results in a TypeError.

opt.add_experimental_option("mobileEmulation", { "deviceName": "Pixel 2"}, "debuggerAddress", "localhost:8989")
#'TypeError: add_experimental_option() takes 3 positional arguments but 5 were given'

Or I tryed this other way, but it only opens the URL in the already open window with no mobile emulation.

opt = Options()
opt.add_experimental_option("debuggerAddress", "localhost:8989")
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("mobileEmulation", { "deviceName": "Pixel 2" })
desired_capabilities = chrome_options.to_capabilities()
driver = webdriver.Chrome(executable_path = my_path, desired_capabilities = chrome_options.to_capabilities(), options = opt)
driver.get(URL)

So I have no more ideas, poor me, and I am looking for a suggestion from someone more experienced.

Upvotes: 2

Views: 6253

Answers (1)

WhiteHatGamer
WhiteHatGamer

Reputation: 21

This Copied from another thread.I Think it can be done by this

chromeOptions = webdriver.ChromeOptions() 
chromeOptions.add_experimental_option("prefs", 
{"profile.managed_default_content_settings.images": 2}) 
chromeOptions.add_argument("--no-sandbox") 
chromeOptions.add_argument("--disable-setuid-sandbox") 

chromeOptions.add_argument("--remote-debugging-port=9222")  # this

chromeOptions.add_argument("--disable-dev-shm-using") 
chromeOptions.add_argument("--disable-extensions") 
chromeOptions.add_argument("--disable-gpu") 
chromeOptions.add_argument("start-maximized") 
chromeOptions.add_argument("disable-infobars")
chromeOptions.add_argument(r"user-data-dir=.\cookies\\test") 

b = webdriver.Chrome(chrome_options=chromeOptions)`

Upvotes: 2

Related Questions