2

I am running automated tests on a Selenium remote server in Python. The remote Selenium server runs inside of a docker container. I obtain my Selenium driver with the following code:

options = webdriver.ChromeOptions()
options.add_argument("--headless")
driver = webdriver.Remote(command_executor='http://127.0.0.1/wd/hub', options=options)
driver.get('http://127.0.0.1:8000')
# ...
driver.close()

I would like to pass two Chrome flags (#same-site-by-default-cookies and #cookies-without-same-site-must-be-secure) to the Selenium Chrome instance. After some research, it seems that I would pass these flags to a new Chrome process by running:

/usr/bin/google-chrome-stable --flag-switches-begin --disable-features=CookiesWithoutSameSiteMustBeSecure,SameSiteByDefaultCookies --flag-switches-end

I've tried adding these arguments to the Selenium ChromeOptions object like so:

options = webdriver.ChromeOptions()
options.add_argument("--headless")
options.add_argument("--flag-switches-begin")
options.add_argument("--disable-features=CookiesWithoutSameSiteMustBeSecure,SameSiteByDefaultCookies")
options.add_argument("--flag-switches-end")
# ... snip ...

Unfortunately, this doesn't appear to have any effect on the Selenium Chrome browser instance. How can I reconfigure the Selenium browser with these flags? Do I need to pass these arguments to the docker container somehow?

Benjamin
  • 546
  • 4
  • 12

2 Answers2

2

I think this line is not doing what you expect options.add_argument("--disable-features=CookiesWithoutSameSiteMustBeSecure,SameSiteByDefaultCookies")

CookiesWithoutSameSiteMustBeSecure vs. cookies-without-same-site-must-be-secure

Set your chrome options like this:

from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
# experimentalFlags = ['CookiesWithoutSameSiteMustBeSecure','SameSiteByDefaultCookies']
flags_i_want = ['cookies-without-same-site-must-be-secure@1', 'same-site-by-default-cookies@1']
chromeLocalStatePrefs = { 'browser.enabled_labs_experiments': flags_i_want}
chrome_options.add_experimental_option('localState',chromeLocalStatePrefs)
driver = webdriver.Chrome(executable_path=r'C:\\Path\\To\\chromedriver.exe', options=chrome_options)

driver.get("https://www.google.com")
Jortega
  • 3,616
  • 1
  • 18
  • 21
  • Thanks! Unfortunately, this doesn't seem to work either. Is it possible that it's not possible to set these experimental flags using a **Remote** Selenium instance? In your answer you switched to using a local instantiation of the chromedriver. – Benjamin Sep 11 '20 at 21:26
  • I also found this answer: https://stackoverflow.com/a/59834314/5161222. I tried appending `@2` to both of the flags but to no avail. – Benjamin Sep 11 '20 at 21:27
  • What do you mean by "doesn't seem to work"? You can print the chrome options to verify they are what you expect: `print(chrome_options.__dict__)`. What are you expecting to happen when set these options? – Jortega Sep 11 '20 at 21:37
  • Chrome's recent SameSite security changes (https://www.chromium.org/updates/same-site) are blocking my application's cookies from being set in the browser and they are causing my Selenium tests to fail. Manually disabling these Chrome flags (via chrome://flags) immediately fixes things in my browser. I want to apply this same "fix" to my tests, but my Selenium tests are still failing even after including this answer in my code. – Benjamin Sep 11 '20 at 21:47
  • Did some more reading and I think @1 means enabled & @2 means disabled. Updating the answer to use `@1`. – Jortega Sep 11 '20 at 21:55
  • I actually want to disable the flags, rather than enable them. – Benjamin Sep 11 '20 at 22:10
2

I got this working with Jortega's help. The following code disables the SameSite experimental Chrome flags on a remote Selenium server as desired:

options = webdriver.ChromeOptions()

# Disable experimental features:
chrome_local_state_prefs = {
    "browser": {
        "enabled_labs_experiments": [
            "cookies-without-same-site-must-be-secure@2",
            "same-site-by-default-cookies@2",
        ],
    }
}
options.add_experimental_option("localState", chrome_local_state_prefs)

driver = webdriver.Remote(command_executor='http://127.0.0.1/wd/hub', options=options)
driver.get('http://127.0.0.1:8000')
# ...
driver.close()
Benjamin
  • 546
  • 4
  • 12