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

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

问题描述

我正在使用硒测试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.

我也尝试过将硒导航到chrome扩展程序设置页面,但是硒似乎无法驱动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)

现在,我们必须导航到上面的url,然后单击以隐身方式允许切换.

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()");

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

Continue Reading, if you want to know how and why

根本原因:

作为chrome浏览器增强功能的一部分,google将所有chrome选项都移到了shadow dom中.因此,您不能像硒find_element方法那样以隐身方式访问 allow 切换元素,该方法将指向页面的原始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元素,我们必须找到连接阴影dom的shadow host.这是基于shadowHost获取影子根的简单方法.

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()之类的操作.

And perform the operations as usual like .click(), .getText().

shadowTreeElement.click()

当您只有一个级别的影子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天全站免登陆