How to Add Chrome Extension at run time using Selenium?

We have discussed a lot about Desired capabilities, ChromeOptions and technique to set proxy in Selenium by using Chrome Options class. We knew that ChromeOptions is one of the upgraded class for Java to set the capabilities Since DesiredCapabilities have been deprecated in Java. Today we are going to discuss the technique to add chrome extension in the browser at runtime using Selenium WebDriver. We use Chrome Options here too to add the chrome extensions.

Let’s look forward.

How did I come across this scenario to add the chrome extension during test execution?

I got an assignment from one of the banking clients who is mainly based in North America. Their insurance portal is restricted in our country; hence, I was unable to access their website to capture the web elements to write the test cases. Therefore, I thought to use Virtual Proxy Network (VPN) to bypass the connection, hence, I downloaded one chrome extension and captured the web elements, thus, by doing so,  I finished writing the test cases.

Now I had to execute the test cases to make sure that there are no breakpoints. We already know that WebDriver creates a new session each time it triggers the new browser. Hence, I didn’t find that Chrome extensions for VPN during test execution. After research, I found a document on official chromium site where the declaration was given. You can refer them for more info, here are their links available:

The scenario could be anything; You can use this implementation whenever required. I just explained How I took advantage of the availability of insertion of chrome extension in Selenium WebDriver.

How to add chrome extension at runtime using Selenium WebDriver?

Following piece of code helps us to add the chrome extensions at runtime using Selenium WebDriver.

ChromeOptions option = new ChromeOptions();
option.addExtensions(new File("Path to the crx file"));

Steps to implement insertion of chrome extension at runtime using Selenium WebDriver

Below mentioned steps elaborate the insertion of chrome extensions using Selenium.

Step# 1: Get the .crx file of the extension

First, you need to download the .crx file of the extension. Go to  https://crxextractor.com/ then enter the link of the chrome extension in the download box then click on Get .CRX button. See image below:

Get crx chrome extension

Step# 2: Give the path of the crx file to the addExtensions() method

Following sample program adds an extension at runtime.

package Test;

import java.io.File;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;

public class AddChromeExtension {

	public static void main(String[] args) {
		System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");
		
		ChromeOptions option = new ChromeOptions();
		
		option.addExtensions(new File("C:\\Users\\Avinash\\Downloads\\extension_3_5_0_0.crx"));
	
		 
		WebDriver  driver  = new ChromeDriver(option);
		
		
		 driver.get("https://www.inviul.com/");
	  	  
	  	  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		  
	  	  driver.manage().window().maximize();
	  	  
	  	  System.out.println(driver.getTitle());
	 

	}

}

See below image, chrome extension has been added.

Chrome extension added

This was all about the insertion of chrome extension at runtime. If you have any queries, suggestions or feedback do post in the comment form below and don’t miss to join our Facebook group for quick updates.

Suggested Readings

Join Inviul fb group

4 Comments

Leave a Reply