How to accept the popup presented when installing the extension in Selenium?

I use selenium for some browser automation. I need to install the extension in the browser for my work. I do 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 starts up fine, but I am prompted with a popup to confirm that I want to add the extension as follows:

enter image description here

and after I get this popup, Python will be back soon with the following exception:

selenium.common.exceptions.WebDriverException: Message: u'unknown Error: Could not wait for additional page to load: chrome extension: //abhcfceiempjmchhhdhbnkbimnfpckgl/toolbar.html \ Nfrom unknown error: page not found: chrome extension: // abhcfcehbbbmbmbmbmbm html \ n (Driver Information: chromedriver = 2.12.301324 (De8ab311bc9374d0ade71f7c167bad61848c7c48), platform = Linux 3.13.0-39-generic x86_64) '

I tried to treat the popup as a normal JavaScript alert using the following code:

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

However, this does not help. Can someone tell me how can I install this extension without a popup or a way to accept a popup? Any help would be greatly appreciated. Thanks!

0
source share
2 answers

Normally, you cannot test the built-in installation of the Chrome extension using only Selenium because of this installation dialog. There are several examples in the wild that show how to use external tools outside of Selenium to solve this problem, but they are not very portable (for example, for the platform) and rely on the state of the Chrome user interface, which is not guaranteed.

But this does not mean that you cannot test the built-in installation. If you replace chrome.webstore.install replacement that behaves like the chrome.webstore.install API (but without a dialog), the end result is the same for all goals and purposes.

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

  • This behavior is in the error message and callback.
  • Installed extension.

I just installed such an example on Github, which includes the source code for a helper extension / application and some examples using Selenium ( Python , Java ). I suggest reading README and the source code to better understand what is going on: https://github.com/Rob--W/testing-chrome.webstore.install .

The sample does not require the test extension to be available on the Chrome Web Store. It doesnโ€™t even connect to the Chrome Web Store. In particular, it does not check whether the site on which the test is running is listed as a verified website, which is required for the built-in installation to work .

+3
source

I had really big code that I would have to rewrite if I had to use Java. Fortunately, python has a library for automating GUI events called ldtp . I used this to automate clicking the Add button. I did something in the following lines:

 from ldtp import * from threading import Thread import selenium from selenium import webdriver from selenium.webdriver.chrome.options import Options def thread_function(): for i in range(5): if activatewindow('Confirm New Extension'): generatekeyevent('<left><space>') break time.sleep(1) def main(): executable_path = "/usr/bin/chromedriver" options = Options() options.add_extension('/home/TheRookie/Downloads/extensionSamples/abhcfceiempjmchhhdhbnkbimnfpckgl.crx') thread.start() browser = webdriver.Chrome(executable_path=executable_path, chrome_options=options) 

Hope this helps someone.

0
source

Source: https://habr.com/ru/post/1499509/


All Articles