如何在 python selenium 中设置 Chrome 实验选项 same-site-by-default-cookie [英] How to set Chrome experimental option same-site-by-default-cookie in python selenium

查看:23
本文介绍了如何在 python selenium 中设置 Chrome 实验选项 same-site-by-default-cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这应该可行:

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

启用为未来的 chrome 版本安排的相同站点 cookie 限制.不是,有错误:

to enable samesite cookies restrictions scheduled for future chrome version. It is not, there is error:

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 手动更改选项并查看它是否有效.但是我想自动化它并运行测试脚本来查看它.

I can change option manually using chrome://flags and see it is working. However I would like to automate it and just run testing script to see it.

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

There is java code here: https://groups.google.com/forum/#!topic/chromedriver-users/cI8hj7eihRo which could do it, however I'm not sure, how to transfer it to python.

是否有任何参考资料可以帮助我设置此选项或其他选项?

Is there any reference available, which would help me to set this option or different options?

推荐答案

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

Tested on Chrome : Version 79.0.3945.130 (Official Build) (64-bit)

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

In Python you can use below code

    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 客户端将发送如下功能

Python selenium client will send the capabilities as below

[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/

To check if its actually worked or not . Go to chrome://flags/

这篇关于如何在 python selenium 中设置 Chrome 实验选项 same-site-by-default-cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆