python-3.x - 如何在 python selenium 中设置 Chrome 实验性选项 same-site-by-default-cookie

标签 python-3.x selenium google-chrome selenium-chromedriver samesite

我想这应该可行:

from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_experimental_option('same-site-by-default-cookies', 'true')
driver = webdriver.Chrome(chrome_options=options)

为 future 的 chrome 版本启用 samesite cookies 限制。不是,有错误:

selenium.common.exceptions.InvalidArgumentException: 
Message: invalid argument: cannot parse capability: goog:chromeOptions
from invalid argument: unrecognized chrome option: same-site-by-default-cookies

我可以使用 chrome://flags 手动更改选项并查看它是否正常工作。但是我想自动化它并运行测试脚本来查看它。

这里有java代码:https://groups.google.com/forum/#!topic/chromedriver-users/cI8hj7eihRo 这可以做到,但我不确定如何将它转移到 python。

是否有任何引用资料可以帮助我设置此选项或不同的选项?

最佳答案

在 Chrome 上测试:版本 79.0.3945.130(官方构建)(64 位)

在Python中你可以使用下面的代码

    chrome_options = webdriver.ChromeOptions()
    experimentalFlags = ['same-site-by-default-cookies@1','cookies-without-same-site-must-be-secure@1']
    chromeLocalStatePrefs = { 'browser.enabled_labs_experiments' : experimentalFlags}
    chrome_options.add_experimental_option('localState',chromeLocalStatePrefs)
    driver = webdriver.Chrome(options=chrome_options)
    driver.get("https://www.bing.com")

Python selenium 客户端将发送如下功能

[1579581631.792][INFO]: Starting ChromeDriver 79.0.3945.36 (3582db32b33893869b8c1339e8f4d9ed1816f143-refs/branch-heads/3945@{#614})
[1579581631.792][INFO]: Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
[1579581632.264][INFO]: [f6b8433509c420fd317902f72b1d102d] COMMAND InitSession {
   "capabilities": {
      "alwaysMatch": {
         "browserName": "chrome",
         "goog:chromeOptions": {
            "args": [  ],
            "extensions": [  ],
            "localState": {
               "browser.enabled_labs_experiments": [ "same-site-by-default-cookies@1", "cookies-without-same-site-must-be-secure@1" ]
            }
         },
         "platformName": "any"
      },
      "firstMatch": [ {

      } ]
   },
   "desiredCapabilities": {
      "browserName": "chrome",
      "goog:chromeOptions": {
         "args": [  ],
         "extensions": [  ],
         "localState": {
            "browser.enabled_labs_experiments": [ "same-site-by-default-cookies@1", "cookies-without-same-site-must-be-secure@1" ]
         }
      },
      "platform": "ANY",
      "version": ""
   }
}

检查它是否真的有效。转到 chrome://flags/

关于python-3.x - 如何在 python selenium 中设置 Chrome 实验性选项 same-site-by-default-cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59787776/

相关文章:

python - Pyside 与 python 3.6.4 不兼容吗?

python - df.first_valid_index() 返回一个引发 KeyError 的索引?

selenium - 如何在Robot Framework中基于元素的文本编写xpath?

python - 无法使用 selenium 输入凭据

javascript - 使用javascript获取伪元素的offsetLeft

Angular5 调试在 Chrome 中不起作用

python - python中alpha排序最快的列表

java - 在 Selenium WebDriver 中截屏时出错

google-chrome - Curl 有效但 Postman 无效

python-3.x - 如何修复 python3 中的 CVE-2019-19646 Sqlite 漏洞