How to enable plugin in Chrome Browser through capabilities using Web Driver? - google-chrome

While starting Chrome Browser through my script I want to enable the plugins automatically.
How can I do this using Chrome Options or is there any other way to solve this issue.

Try this Pythonic one for remote driver
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
chrome_options = Options()
chrome_options.add_argument('--always-authorize-plugins=true')
capabilities = chrome_options.to_capabilities()
driver = webdriver.Remote(
command_executor='http://hub_address/wd/hub',
desired_capabilities=capabilities)
and for local one
options = Options()
options.add_argument('--always-authorize-plugins=true')
driver = webdriver.Chrome(chrome_options=options)

Related

How to generate localStorage data for Google Chrome extension running in Selenium driver?

I need to run a test in Chrome driver which loads an extension which modifies HTTP headers. This extension stores its data in localStorage, per profile.
I tried starting chromedriver with user-data-dir set to the profile which already has this information manually inserted, but the profile data aren't loaded during test. What else do I need to configure to allow the Chrome extension to access localStorage associated with particular profile?
Here's some code to help illustrate the problem:
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
def load_message_monitoring():
chrome_options = Options()
chrome_options.add_argument(
'user-data-dir=/home/wvxvw/.config/google-chrome/Default'
)
chrome_options.add_argument(
'load-extension=/home/wvxvw/.config/google-chrome/Default/Extensions/idgpnmonknjnojddfkpgkljpfnnfcklj/2.1.2_0'
)
driver = webdriver.Chrome(
executable_path='/opt/webdriver/chromedriver',
chrome_options=chrome_options,
)
driver.get('http://localhost:3000/')
# This breaks because the extension doesn't rewrite headers
driver.quit()

Selenium not starting portable chrome but local installation

I have a problem with the Selenium web driver. What I'm trying to do is to start a "portable" chrome instead of my local installation, because it has different settings.
The problem is that the portable Chrome (from PortableApps) seems to only start when using GoogleChromePortable.exe. If I use the Chrome binary directly, it will start my local installation.
With Selenium it seems that no matter what Chrome path I pass to it (GoogleChromePortable.exe or binary path), it starts my local installation.
Here is my code:
String chromePath = "M:/my/path";
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
capabilities.setCapability("chrome.binary", chromePath);
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
Any ideas how to be able to start my portable chrome?
Thanks
For anyone else stumbling upon this problem, here is how I managed to get the portable Chrome starting:
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setBinary(binaryPath);
driver = new ChromeDriver(chromeOptions);
I'm using Python 3.7 on Windows 10 and got Chrome Portable from PortableApps.com.
comments by #mario.schlipf and #SeJaPy were helpful, but I noticed that in the newer Webdriver releases, the setbinary method has been replaced by binary_location
This is how it actually worked for me:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chromedriverpath='M:/my/chromedriver.exe'
chromePath = 'M:/my/App/Chrome-bin/chrome.exe' # <== IMPORTANT! See note below.
chromeoptions = Options()
chromeoptions.add_argument('--incognito')
chromeoptions.binary_location = chromePath
browser = webdriver.Chrome(executable_path=chromedriverpath, options=chromeoptions)
NOTE:
The chromePath variable must point to the Chrome executable in the portabilized environment.
In packages obtained from PortableApps.com, you have two executables: a GoogleChromePortable.exe in the install (actually, unpack) directory and a chrome.exe in [installdirectory]/App/Chrome-bin, the first being "just" a launcher which provides the portabilized app with a consistent environment.
As I could observe, chromedriver needs to directly interact with the "real" Chrome executable, otherwise the script will launch the browser (via the launcher) but will eventually crash with error message:
unknown error: DevTools Active Port file doesn't exist
and no browser session will be returned as a result.
This may seem obvious to many people... but it was not to me, so I decided to put this note in order to make some clarity for the less clever guys (myself included) :).
String chromePath = "M:/my/googlechromeporatble.exe path";
String chromedriverpath="M:/my/chromedriver.exe path";
ChromeOptions options = new ChromeOptions();
options.setBinary(chromepath);
System.setProperty("webdriver.chrome.driver",chromedriverpath);
driver = new ChromeDriver(options);
This will invoke portable chrome rather than local installation.
First set google chrome portable path and then invoke chromeriver.exe
Depending on the settings you have in ChromePortable, maybe you could default ChromeDriver with Capabilities & ChromeOptions?
I'm thinking especially on custom profile. If you somehow could get that from your ChromePortable and load it with default ChromeDriver?
EDIT: Maybe this could help

Is there any difference between mobileEmulation and setting the user agent in Chrome?

I want to simulate mobile devices for testing and automation purposes. Using the Chromedriver and Selenium I have found 2 ways to do this. I can either set the user agent (example from here):
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--user-agent=Mozilla/5.0 (iPad; CPU OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3')
driver = webdriver.Chrome(chrome_options=options)
Or I can enable mobile emulation (example from here):
from selenium import webdriver
mobile_emulation = { "deviceName": "Google Nexus 5" }
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)
driver = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub', desired_capabilities = chrome_options.to_capabilities())
What's the difference?
User agent string: you are essentially lying to the web server about your browser.
Emulation mode: you are essentially lying to yourself about your browser.

Selenium - Chrome is not starting when I try to load an extension

Since Firefox does not support the GWT DevMode any more, I want to switch to Chrome for developing selenium tests for my application.
However, it fails to load the GWT-dev-plugin extension. The test does not return from
driver = new ChromeDriver(capabilities);
Without configuring the extension, in works in the sense that a Chrome instance is at least started, however, without GWT plugin.
This is the way I configure the webDriver:
package the GWT plugin of your Chrome installation (described here http://www.mythoughts.co.in/2013/09/seleinum-webdriver-loading-chrome.html#.U0ZTFFfKGno)
use the path of the plugin .crx package as pluginPath in the following code
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File(pluginPath));
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
File driverExecutable = new File("test-selenium/chromedriver.exe");
System.setProperty("webdriver.chrome.driver", driverExecutable.getAbsolutePath());
driver = new ChromeDriver(capabilities);
The only logging is a WARNING concerning the plugin key:
[14.899][WARNING]: Public key in crx header is different from key in manifest
key from header: ....
generated extension id from header key: godampgacipeiboepncogmjpfbgcpfba
generated extension id from manifest key: jpjpnpmbddbjkfaccnmhnkdgjideieim

How can I launch Chrome with an unpacked extension?

I'm using Selenium WebDriver to test a Google Chrome extension I'm developing. I noticed that ChromeDriver can be customised to add extensions to the instance of Chrome that it launches. This can be achieved using the AddExtension and AddExtensions methods of the ChromeOptions class.
The documentation for these methods indicates that they require extensions to be provided as crx files. Since I'm developing the extension, I don't have a crx file. I would like to be able to load the unpacked extension, but I couldn't find a method to do this.
I tried putting the extension files in a zip file and specifying this for the AddExtension method, but this caused an exception to occur since it wasn't a crx file. I also tried passing in the directory containing the unpacked files, but this produced a FileNotFoundException.
How can I do this?
I was able to achieve this by using the AddArgument method to directly pass the information to Chrome. Here's what it looks like in C#:
options = new ChromeOptions();
options.AddArgument("--load-extension=" + unpackedExtensionPath);
For packed extensions (a .crx file)
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("/path/to/extension.crx"));
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
ChromeDriver driver = new ChromeDriver(capabilities);
For unpacked extensions (a local folder)
ChromeOptions options = new ChromeOptions();
options.addArguments("load-extension=/path/to/extension");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
ChromeDriver driver = new ChromeDriver(capabilities);
source
It may be late but for future users:
https://sites.google.com/a/chromium.org/chromedriver/extensions
The unpacked extension error popped up for me and I requested for removing the restrictions in chrome which was enforced as organizational policy. Once the restrictions were removed, I am able to run the program with out any errors. ChromeBrowser-GPO-Deny - this was the one which was removed. You can check in Settings - Extensions - Check on Developer mode and see if the load unpacked extensions is checked once the restrictions are removed. You should be good then. All the above will work only when the chrome is not restricted.
In Python3 it can be done like this:
from selenium.webdriver import Chrome, ChromeOptions
options = ChromeOptions()
options.add_argument("load-extension=/path/to/unpacked_ext")
driver = Chrome("/path/to/chromedriver", options=options)
# (optional) Look at the uploaded extension
driver.get("chrome://extensions")
Here is apython example using webdriver_manager
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
options = webdriver.ChromeOptions()
# loading the extension Edit-This-Cookie
options.add_argument("--load-extension=./Edit-This-Cookie")
driver = webdriver.Chrome(ChromeDriverManager().install(), chrome_options=options)
driver.get("https://google.com")
The Selenium documentation has an example like this for a packed ( not unpacked) extension:
capabilities.setCapability("platform", "Windows 8" );
capabilities.setCapability("version", "10");
capabilities.setCapability("name", testname);
capabilities.setCapability("screen-resolution", "1280x1024");
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("/path/to/extension.crx"));
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
if (isLocal) driver = new ChromeDriver(capabilities);