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

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

问题描述

我将 selenium 用于一些浏览器自动化.我需要在浏览器中为我的工作安装一个扩展程序.我是这样做的:

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:

在我收到此弹出窗口后,Python 很快返回,但出现以下异常:

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

selenium.common.exceptions.WebDriverException:消息:你未知错误:无法等待扩展后台页面加载:chrome-extension://abhcfceiempjmchhhdhbnkbimnfpckgl/toolbar.html 来自未知错误:找不到页面:chrome-extension://abhcfceiempjmchhhdhbnkbimnfpckgl/toolbar.html (驱动程序信息:chromedriver=2.12.301324(de8ab311bc9374d0ade71f7c167bad61848c7c48),platform=Linux3.13.0-39-通用 x86_64)'

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

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

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!

推荐答案

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

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.

但这并不意味着您不能测试内联安装.如果您将 chrome.webstore.install 替换为类似于 chrome.webstore.install API(但没有对话框),那么最终结果对于所有意图和目的都是相同的.

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.

行为类似于 chrome.webstore.install"包括两件事:

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

  • 错误报告和回调调用的行为相同.
  • 安装了扩展程序.

我刚刚在Github上搭建了这样一个例子,里面包含了helper extension/app的源码和几个使用Selenium的例子(Python, Java).我建议阅读自述文件和源代码以更好地了解发生的情况:https://github.com/Rob--W/testing-chrome.webstore.install.

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.

该示例不需要在 Chrome 网上应用店中提供经过测试的扩展程序.它甚至无法连接到 Chrome 网上应用店.特别是,它不会检查运行测试的站点是否被列为经过验证的网站,这是内联安装工作所必需的.

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天全站免登陆