- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to programmatically configure Chrome extension through Selenium WebDriver?
We can programmatically configure Chrome extension through Selenium webdriver. We can have multiple extensions of the Chrome browser while we manually open the browser and work on it.
However, while the Chrome browser is opened through Selenium webdriver, those extensions which are available to the local browser will not be present. To configure an extension, we have to obtain the .crx extension file of the extension.
Then we have to add that extension to the browser which is launched by Selenium webdriver. To get all the extensions available to the browser enter
chrome://extensions on the browser.
To get add an extension for example: Momentum, visit the link −
https://chrome.google.com/webstore/category/extensions and enter Momentum in the search box. Once the search results are displayed, click on the relevant option.
After clicking on the Momentum extension, the details of the extension gets displayed. Copy the URL of the extension as highlighted in the below image.
Now, navigate to the link: https://chrome−extension−downloader.com/ and paste the URL we have copied within the Download extension field.
The .crx file of the extension gets downloaded to our system. We should then save it in a desired location.
To add this extension to the Chrome browser, once it is launched by Selenium webdriver, we have to use the ChromeOptions class. We shall create an object of this class and apply addExtensions method on it.
The path of the .crx file of the extension that we want to add is passed as a parameter to that method. Then use the DesiredCapabilities class to set this browser capability.
We shall apply the setCapability method on the object of DesiredCapabilities and pass ChromeOptions.CAPABILITY and object of the ChromeOptions class as parameters to that method. Finally, the object of DesiredCapabilities is passed as a parameter to the webdriver object.
Example
import org.openqa.selenium.WebDriver; import org.openqa.selenium.Capabilities; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.remote.CapabilityType; import org.openqa.selenium.remote.DesiredCapabilities; import java.io.File; public class AddExtensns{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); //ChromeOptions object ChromeOptions opt= new ChromeOptions(); //set path of .crx file of extension opt.addExtensions(new File("C:\Users\Momentum_v0.92.2.crx")); //DesiredCapabilities object DesiredCapabilities c = DesiredCapabilities.chrome(); // set ChromeOptions capability c.setCapability(ChromeOptions.CAPABILITY, opt); // pass capability to driver WebDriver driver = new ChromeDriver(c); driver.get("https://www.tutorialspoint.com/index.htm"); } }
- Related Articles
- Is there any way to load an extension in chrome browser using Selenium Webdriver?
- How to run Selenium WebDriver test cases in Chrome?
- How do I open Chrome in selenium WebDriver?
- How to mute all sounds in chrome webdriver with selenium?
- How does Selenium Webdriver handle SSL certificate in Chrome?
- Running Selenium WebDriver python bindings in chrome.
- How to use chrome webdriver in Selenium to download files in Python?
- Using the Selenium WebDriver - Unable to launch chrome browser on Mac
- How to send a report through email using Selenium Webdriver?
- How to open a link in new tab of chrome browser using Selenium WebDriver?
- How to set Google Chrome in WebDriver?
- How to launch Chrome Browser via Selenium?
- How to handle chrome notification in Selenium?
- How to Maximize window in chrome using webDriver (Python)?
- How to Download & Install Selenium WebDriver?
