如何接受在Selenium中安装扩展时显示的弹出窗口? [英] How to accept the popup presented when installing extension in Selenium?

查看:708
本文介绍了如何接受在Selenium中安装扩展时显示的弹出窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用硒进行一些浏览器自动化。我需要在浏览器中为我的工作安装扩展。我是这样做的:

 从selenium导入selenium 
从selenium.webdriver导入webdriver
。 chrome.options import Options
executable_path =/ usr / bin / chromedriver
options = Options()
options.add_extension('/ home / TheRookie / Downloads / extensionSamples / abhcfceiempjmchhhdhbnkbimnfpckgl.crx' )
browser = webdriver.Chrome(executable_path = executable_path,chrome_options = options)

浏览器是开始罚款,但我会弹出提示,以确认我想添加扩展如下:




在弹出窗口后,Python很快返回,出​​现以下异常:


selenium.common.exceptions.WebDriverException:消息:u'unknown
错误:未能等待扩展背景页面加载:
chrome-extension:// abhcfceiempj mchhhdhbnkbimnfpckgl / toolbar.html\\\
from
未知错误:无法找到页面:
chrome-extension://abhcfceiempjmchhhdhbnkbimnfpckgl/toolbar.html\
(驱动程序信息:chromedriver = 2.12 .301324
(de8ab311bc9374d0ade71f7c167bad61848c7c48),platform = Linux
3.13.0-39-generic x86_64)'

我尝试使用以下代码作为常规JavaScript警报处理弹出窗口:

  alert = browser.switch_to_alert()
alert.accept()

然而,这并没有帮助。任何人都可以告诉我如何安装这个扩展没有弹出或接受弹出窗口的方式?任何帮助将不胜感激。感谢!

解决方案

通常,您无法使用 Selenium 测试Chrome扩展的内联安装,因为该安装对话框。有几个例子显示了如何在Selenium之外使用外部工具来解决这个问题,但这些并不是很便携(即特定于平台),并且依赖于Chrome的用户界面状态,这不能保证一致。



但这并不意味着您无法测试内联安装。如果您替换为 chrome.webstore.install (rel =nofollow> chrome.webstore.install API(但没有对话框),那么对于所有意图和目的,最终结果是相同的。
$ b

表现如同 chrome.webstore.install 由两部分组成:




  • 在错误报告和回调调用中具有相同的行为。

  • 安装了扩展程序。 ul>

    我在Github上设置了这样一个例子,其中包括助手扩展/ app的源代码和一些使用Selenium的例子( Python Java )。我建议阅读自述文件和源代码以更好地理解发生了什么: https://github.com/Rob--W/testing-chrome.webstore.install



    该示例不需要经过测试的扩展名在Chrome网上商店中可用。它甚至没有连接到Chrome网上商店。特别是,它不会检查运行测试的网站是否被列为经过验证的网站,请这是内联安装工作所必需的


    I am using selenium for some browser automation. I need to install an extension in the browser for my work. I am doing it as follows:

    import selenium
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    executable_path = "/usr/bin/chromedriver"
    options = Options()
    options.add_extension('/home/TheRookie/Downloads/extensionSamples/abhcfceiempjmchhhdhbnkbimnfpckgl.crx')
    browser = webdriver.Chrome(executable_path=executable_path, chrome_options=options)
    

    The browser is starting fine but I am prompted with a pop-up to confirm that I want to add the extension as follows:

    and after I get this pop-up, Python soon returns with the following exception:

    selenium.common.exceptions.WebDriverException: Message: u'unknown error: failed to wait for extension background page to load: chrome-extension://abhcfceiempjmchhhdhbnkbimnfpckgl/toolbar.html\nfrom unknown error: page could not be found: chrome-extension://abhcfceiempjmchhhdhbnkbimnfpckgl/toolbar.html\n (Driver info: chromedriver=2.12.301324 (de8ab311bc9374d0ade71f7c167bad61848c7c48),platform=Linux 3.13.0-39-generic x86_64)'

    I tried handling the popup as a regular JavaScript alert using the following code:

    alert = browser.switch_to_alert()
    alert.accept()
    

    However, this doesn't help. Could anyone please tell me how do I install this extension without the popup or a way to accept the popup? Any help would be greatly appreciated. Thanks!

    解决方案

    Usually, you cannot test inline installation of a Chrome extension with just Selenium, because of that installation dialog. There are a few examples in the wild that show how to use external tools outside Selenium to solve this problem, but these are not very portable (i.e. platform-specific) and rely on a state of Chrome's UI, which is not guaranteed to be consistent.

    But that does not mean that you cannot test inline installation. If you replace chrome.webstore.install with a substitute that behaves like the chrome.webstore.install API (but without the dialog), then the end-result is the same for all intents and purposes.

    "Behaves like chrome.webstore.install" consists of two things:

    • Same behavior in error reporting and callback invocation.
    • An extension is installed.

    I have just set up such an example on Github, which includes the source code of the helper extension/app and a few examples using Selenium (Python, Java). I suggest to read the README and the source code to get a better understanding of what happens: https://github.com/Rob--W/testing-chrome.webstore.install.

    The sample does not require the tested extension to be available in the Chrome Web store. It does not even connect to the Chrome Web store. In particular, it does not check whether the site where the test runs is listed as a verified website, which is required for inline installation to work.

    这篇关于如何接受在Selenium中安装扩展时显示的弹出窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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