1

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:

enter image description here

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!

TheRookierLearner
  • 3,643
  • 8
  • 35
  • 53
  • You cannot click on that button. Try Java instead of the Python bindings. I've implemented the Java implementation and that one automatically installs the extension without requiring a confirmation. – Rob W Nov 12 '14 at 22:32
  • Actually, it automatically installs some extensions but for other extensions it requires confirmation. – TheRookierLearner Nov 12 '14 at 22:33
  • https://developer.chrome.com/extensions/permission_warnings - warnings only come up for some – Xan Nov 12 '14 at 23:25
  • @RobW - Probably its [not possible in Java too](http://stackoverflow.com/questions/18499997/selenium-webdriverjs-testing-chrome-extension-installation) – TheRookierLearner Nov 13 '14 at 09:51
  • 2
    @TheRookierLearner The dialog only comes up if you install the extension in the browser (e.g. using [`chrome.webstore.install`](https://developer.chrome.com/extensions/webstore#method-install). If you load the extension via `addExensions`, then it will load without any additional confirmation dialogs. I'm sure on that, I frequently use Selenium to test one of my Chrome extensions. Here's sample code (Java): http://stackoverflow.com/a/17117849 – Rob W Nov 13 '14 at 09:55
  • @RobW Could it be triggered by installing CRX as opposed to unpacked? – Xan Nov 13 '14 at 20:32
  • @RobW - I am using `add_extension` which is a counterpart of `addExtensions` in Java (at least, its supposed to be) so I don't know what's causing this. May be its a Python specific problem. – TheRookierLearner Nov 13 '14 at 20:37
  • @Xan - I really doubt that. I do install some extensions that are CRX. I had previously install CRX extensions which did not cause the popup to occur. – TheRookierLearner Nov 13 '14 at 20:40
  • @RobW - unless there is a rigorous mathematical proof akin to Fermat's final problem, where it was shown that a solution to a^n + b^n = c^n does not exist for any whole n > 2, I would hardly call your (or anyone else's for that matter) finite experience (comprising a couple, a hundred or even a thousand data points, all the same) qualifies as mathematical 'rigour'. I say this with confidence is all it takes is a rebel like *moi* to show one example of its existence. Here is is (installed via add Extensions/options, either way: https://1drv.ms/x/s!AsxYl9DXJ0j9iHa5Mbv26mEzs3vR?e=LCAgi1 – JB-007 Mar 06 '23 at 07:12

2 Answers2

3

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.

Rob W
  • 341,306
  • 83
  • 791
  • 678
  • Uses `developerPrivate`... Steals whitelisted ID.. HAAAAAAX! ;) – Xan Feb 22 '15 at 23:48
  • Does your solution work with `webdriver.Remote`, and can you give an example how? I'm using `webdriver.Remote` to connect to a remote Chrome browser. And how do I install the helper extensions on the remote browser? – Uri Feb 23 '15 at 10:30
  • And is it possible to install an extension from the Chrome Web Store? Our website installs our extension from the Chrome Web Store when the user clicks on a button. – Uri Feb 23 '15 at 10:39
  • @Uri For remote to work, you have to use `add_extension` with a zip file, as seen here: https://github.com/Rob--W/testing-chrome.webstore.install/commit/5a914e2db1c36c9d4dadc6c52d40d093a55c65c8. The helper app/extension load just fine, but IF your extension contains a background page, then it does not work, because ChromeDriver waits until the background page has loaded. A way to solve this issue is to copy the zip/crx files to a fixed location at the remote host and specify the paths in `load-extension=path/to/helper,path/to/helper-app,path/to/extension/etc`. – Rob W Feb 23 '15 at 10:44
  • @Uri This UI-less installation trick only works because the extension you're testing is already pre-installed (though disabled). Installing the latest version from the CWS is not possible without UI. Technically, it is possible to achieve the goal by (automatically) downloading and unpacking the CRX before testing, and adding the logical checks that are also used by the Chrome web store (e.g. site verification), but I didn't do that as I did not want to spend more time on writing this demo. – Rob W Feb 23 '15 at 10:46
  • @RobW is this possible now with selenium? – ChanChow Mar 03 '16 at 17:28
  • @AllIsWell Not sure, I don't think so. – Rob W Mar 03 '16 at 23:25
0

I had some really big code which I would have to re-write if I had to use Java. Luckily, python has a library for automating GUI events called ldtp. I used that to automate the clicking on the "Add" button. I did something on 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 it helps somebody.

TheRookierLearner
  • 3,643
  • 8
  • 35
  • 53
  • 2
    Does this code work? How do I install ldtp? You use `thread` but you import `Thread`, Python is case sensitive. When I try to use your code I see that `thread` is undefined. – Uri Feb 22 '15 at 14:22