如何使用 Webdriver C# 中的 chromeoptions 启用每个进程的站点标志



在Chrome中,有一个严格的网站隔离设置,可以在 chrome://flags/#enable-site-per-process 页面中手动启用。我尝试在使用 WebDriver C# 绑定测试 Chrome 时启用此标志,但它没有启用。

该开关列在 https://peter.sh/experiments/chromium-command-line-switches/中,因此我尝试将其添加为ChromeOptions中的参数,但这没有任何效果。

ChromeOptions options = new ChromeOptions();
options.AddArgument("--site-per-process");
IWebDriver driver = new ChromeDriver(@"c:browserdrivers",options);
driver.Navigate().GoToUrl("www.google.com");
driver.Quit();

我还尝试根据 https://chromium.googlesource.com/chromium/src/+/master/chrome/common/pref_names.cc 中列出的设置将其设置为首选项

options.AddUserProfilePreference("site_isolation.site_per_processs", true);

但这也没有用。

有谁知道如何打开它?

我们正在使用 PS 脚本部署此更改,以在 %AppDataLocal%\Google\Chrome\User Data 上编辑 Windows 上的本地状态文件

脚本有点基础,但会为我们完成工作。 最好使用 GPO 选项,但不幸的是,这仅适用于 Chrome 63 及更高版本。


$USRPROF = Get-Childitem env:APPDATA | %{ $_.Value }
$ChromeLocalState = (Get-item $USRPROF).parent.FullName + "LocalGoogleChromeUser DataLocal State"
#original (Get-Content $ChromeLocalState) | Foreach-Object {$_ -replace '{"browser":{"last_redirect_origin"', '{"browser":{"enabled_labs_experiments":["enable-site-per-process"],"last_redirect_origin"'} | Set-Content $ChromeLocalState
#add site-per-process setting. Ok if it doubles the same nested statement. Chrome will parse it out.
(Get-Content $ChromeLocalState) | Foreach-Object {$_ -replace 'last_redirect_origin', 'enabled_labs_experiments":["enable-site-per-process"],"last_redirect_origin'} | Set-Content $ChromeLocalState

基于Fiddles代码,这个版本解决了我在测试中发现的几个问题。 必须以登录用户身份运行。请参阅末尾的注释。

"本地状态"json 文件包含启用此实验设置的"启用每个进程站点"配置项的每用户设置

使用本地系统环境变量获取用户 C:\用户\用户 ID\应用数据\本地文件夹

$USRPROF = Get-Childitem env:LOCALAPPDATA 

该文件夹下是'GoogleChromeUser Data'文件夹,其中包含我们需要修改的'Local State'文件

$ChromeLocalState =  -Join($USRPROF.Value.ToString(),'','GoogleChromeUser DataLocal State')

停止铬

Stop-Process -Name chrome -Force -ErrorAction SilentlyContinue

添加"每个进程的站点"设置。好的,如果它加倍相同的嵌套语句。Chrome 会解析它。

(Get-Content $ChromeLocalState) | Foreach-Object {$_ -replace 'last_redirect_origin', 'enabled_labs_experiments":["enable-site-per-process"],"last_redirect_origin'} | Set-Content $ChromeLocalState

查找安装了哪个版本的 Chrome 并启动它

Switch($TRUE) {
{Test-Path 'C:Program Files (x86)GoogleChromeApplicationchrome.exe'} {Start-Processs -FilePath 'C:Program Files (x86)GoogleChromeApplicationchrome.exe' }
{Test-Path 'C:Program FilesGoogleChromeApplicationchrome.exe'}       {Start-Processs -FilePath 'C:Program FilesGoogleChromeApplicationchrome.exe' }
}

测试期间的观察结果

如果此脚本启动时 chrome 正在运行,则会生成"本地状态"文件中的值,但不会生效。 如果 Chrome 随后关闭,"本地状态"文件将被内存中的信息覆盖,并且设置已删除!!

因此,为了获得最好的工作机会,脚本已被修改为 1.停止铬 2. 更改设置 3. 重新启动浏览器

重新启动 chrome 是可选的,由于 Chrome 的非标准终止,系统会要求用户恢复打开的标签页。

进一步的观察是,即使chrome在后台,本地状态文件也在不断被修改 这是首先停止铬的另一个原因。

最新更新