通过 CLI 标志在隐身模式下启用 Chrome 扩展? [英] Enabling Chrome Extension in Incognito Mode via CLI flags?

查看:14
本文介绍了通过 CLI 标志在隐身模式下启用 Chrome 扩展?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 selenium 来测试 Chrome 扩展程序,并且部分扩展程序要求用户处于隐身模式.目前,除了添加参数 user-data-dir=/path/to/directory 外,我无法在启动时启用隐身模式下的扩展.

I'm using selenium to test a chrome extension and part of the extension requires the user to be in incognito mode. Currently, I've not been able to enable the extension to be allowed in incognito mode upon startup except by adding the argument user-data-dir=/path/to/directory.

这样做的问题是它从我的文件系统深处加载扩展名,而不是我可以检入 git 的方式.

The problem with this is that it loads the extension from the depths of my file system, rather than in a way I can check into git.

我也尝试将 selenium 导航到 chrome 扩展设置页面,但 selenium 似乎无法驱动 chrome:// 页面.

I've also tried navigating selenium to the chrome extensions settings page but it seems that selenium can't drive chrome:// pages.

关于如何在 chrome 驱动程序启动时在 chrome 扩展上启用隐身模式有什么想法吗?

Any ideas on to how to enable incognito on the chrome extension on boot of the chrome driver?

推荐答案

这是适用于最新版本 Chrome 74 的解决方案.

Here is the solution that will work with the latest version of Chrome 74.

  1. 导航到 chrome://extensions
  2. 点击详细信息按钮选择您想要的扩展
  1. Navigate to chrome://extensions
  2. Click on Details button for your desired extension

  1. 复制网址(其中包含您的扩展程序 id)

现在我们必须导航到上面的网址,然后点击允许隐身切换.

Now we have to navigate to the above url and then click on the allow in incognito toggle.

Java:

driver.get("chrome://extensions/?id=bhghoamapcdpbohphigoooaddinpkbai");
JavascriptExecutor js = (JavascriptExecutor) driver; 
js.executeScript("document.querySelector('extensions-manager').shadowRoot.querySelector('#viewManager > extensions-detail-view.active').shadowRoot.querySelector('div#container.page-container > div.page-content > div#options-section extensions-toggle-row#allow-incognito').shadowRoot.querySelector('label#label input').click()");

Python:

driver.get("chrome://extensions/?id=bhghoamapcdpbohphigoooaddinpkbai")
driver.execute_script("return document.querySelector('extensions-manager').shadowRoot.querySelector('#viewManager > extensions-detail-view.active').shadowRoot.querySelector('div#container.page-container > div.page-content > div#options-section extensions-toggle-row#allow-incognito').shadowRoot.querySelector('label#label input').click()");

继续阅读,如果你想知道如何以及为什么

根本原因:

作为 chrome 浏览器增强功能的一部分,谷歌将所有 chrome 选项移到 shadow dom.因此,您无法以 selenium find_element 方法访问 allow in incognito 切换元素,该方法将指向页面的原始 dom.所以我们必须切换到shadow dom并访问shadow tree中的元素.

As part of enhancements to the chrome browser, google moved all the chrome option in to shadow dom. So you can not access allow in incognito toggle element as selenium find_element method which will point to the original dom of the page. So we have to switch to the shadow dom and access the elements in the shadow tree.

详情:

影子 DOM:

注意:我们将参考图片中显示的术语.所以请通过图片更好地理解.

解决方案:

为了使用shadow element,首先我们必须找到shadow dom所附加的shadow host.下面是基于shadowHost获取shadow root的简单方法.

In order to work with shadow element first we have to find the shadow host to which the shadow dom is attached. Here is the simple method to get the shadow root based on the shadowHost.

private static WebElement getShadowRoot(WebDriver driver,WebElement shadowHost) {
    JavascriptExecutor js = (JavascriptExecutor) driver;
    return (WebElement) js.executeScript("return arguments[0].shadowRoot", shadowHost);
}

然后您可以使用 shadowRoot 元素访问阴影树元素.

And then you can access the shadow tree element using the shadowRoot Element.

// get the shadowHost in the original dom using findElement
WebElement shadowHost = driver.findElement(By.cssSelector("shadowHost_CSS"));
// get the shadow root
WebElement shadowRoot = getShadowRoot(driver,shadowHost);
// access shadow tree element
WebElement shadowTreeElement = shadowRoot.findElement(By.cssSelector("shadow_tree_element_css"));

为了简化上述所有步骤,创建了以下方法.

In order to simplify all the above steps created the below method.

public static WebElement getShadowElement(WebDriver driver,WebElement shadowHost, String cssOfShadowElement) {
    WebElement shardowRoot = getShadowRoot(driver, shadowHost);
    return shardowRoot.findElement(By.cssSelector(cssOfShadowElement));
}

现在您可以通过单个方法调用获取 shadowTree 元素

Now you can get the shadowTree Element with single method call

WebElement shadowHost = driver.findElement(By.cssSelector("shadowHost_CSS_Goes_here));
WebElement shadowTreeElement = getShadowElement(driver,shadowHost,"shadow_tree_element_css");

并照常执行.click().getText()等操作.

shadowTreeElement.click()

当您只有一层 shadow DOM 时,这看起来很简单.但是在这里,在这种情况下,我们有多个级别的 shadow dom.所以我们必须通过到达每个影子主机和根来访问元素.

This Looks simple when you have only one level of shadow DOM. But here, in this case we have multiple levels of shadow doms. So we have to access the element by reaching each shadow host and root.

以下是使用上述方法(getShadowElement 和 getShadowRoot)的代码片段

Below is the snippet using the methods that mentioned above (getShadowElement and getShadowRoot)

// Locate shadowHost on the current dom
WebElement shadowHostL1 = driver.findElement(By.cssSelector("extensions-manager"));

// now locate the shadowElement by traversing all shadow levels
WebElement shadowElementL1 = getShadowElement(driver, shadowHostL1, "#viewManager > extensions-detail-view.active");
WebElement shadowElementL2 = getShadowElement(driver, shadowElementL1,"div#container.page-container > div.page-content > div#options-section extensions-toggle-row#allow-incognito");
WebElement allowToggle = shadowElementL2.findElement(By.cssSelector("label#label input"));
allowToggle.click();

您可以在答案开头提到的单个js调用中完成上述所有步骤(添加在下面只是为了减少混淆).

You can achieve all the above steps in single js call as at mentioned at the beginning of the answer (added below just to reduce the confusion).

WebElement allowToggle = (WebElement) js.executeScript("return document.querySelector('extensions-manager').shadowRoot.querySelector('#viewManager > extensions-detail-view.active').shadowRoot.querySelector('div#container.page-container > div.page-content > div#options-section extensions-toggle-row#allow-incognito').shadowRoot.querySelector('label#label input')");

这篇关于通过 CLI 标志在隐身模式下启用 Chrome 扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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