简体   繁体   English

使用python从硒铬webdriver启动时如何在隐身模式下添加扩展名

[英]How to add extension in incognito mode when launched from selenium chrome webdriver with python

We are doing some testing using selenium python webdriver where we need to open one url in incognito mode and enable one extension already installed and then do some actions. 我们正在使用selenium python webdriver做一些测试,我们需要以隐身模式打开一个url,并启用一个已安装的扩展,然后执行一些操作。

My findings: 我的发现:

  • loading of extension in incognito mode not working 在隐身模式下无法加载扩展程序
  • extension getting loaded when icgnito mode turned off icgnito模式关闭时加载扩展程序

Verified so many post on stack overflow, nothing worked. 验证了很多关于堆栈溢出的帖子,没有任何效果。 tried below code" 尝试以下代码”

path = os.path.dirname(r"C:\Users\ab\AppData\Local\Google\Chrome\User Data\Default\Extensions\jfpmbokkdeapjommajdfmmheiiakdlgo\0.1.7_0\manifest.json")
options = webdriver.ChromeOptions()
options.add_argument('--incognito')
options.add_argument("--load-extension={path}")
driver = webdriver.Chrome(chrome_options=options, executable_path='C:\chromedriver_win32\chromedriver.exe')
driver.maximize_window()
driver.get(xxxxxxxx)

which throwing error cannot load manifest.json either missing or not readable. 哪些抛出错误无法加载manifest.json丢失或不可读。 However i have made sure the path is correct. 但是我已经确定路径是正确的。

any suggestion please how to load extension while opening chrome driver in incognito mode ? 有什么建议请在隐身模式下打开chrome驱动程序时如何加载扩展程序?

Rather you loading the required cookies/extension as part of your chrome options, other option is using the chrome profile. 而是将所需的Cookie /扩展名作为chrome选项的一部分加载,而其他选项是使用chrome配置文件。 Check my answer in this post 这篇文章中检查我的答案

To more on the profiles and how they work refer here 有关配置文件及其工作方式的更多信息,请参见此处

Here is the logic to turn on the extension in the incognito mode. 这是在隐身模式下打开扩展名的逻辑。

Python: 蟒蛇:

# driver.get("chrome://extensions/?id=extion_name_goes_here"); # <=== general snippet see below example
# driver.get("chrome://extensions/?id=jfpmbokkdeapjommajdfmmheiiakdlgo") 

# select allow in incognito mode checkbox
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()");

Refer to my answer in this post for more information on the js used above. 请参阅我的答案在这个岗位上上面使用的JS更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM