How to use a specific chrome profile in selenium?

Rest AssuredDynamic ProgrammingProgramming

We can use a specific Chrome profile in Selenium. This can be done with the help of the ChromeOptions class. We need to create an object of this class and then apply addArguments method on it.

The path of the specific Chrome profile that we want to use is passed as a parameter to this method. We can open Chrome’s default profile with Selenium. To get the Chrome profile path, we need to input chrome://version/ in the Chrome browser and then press enter.

Syntax

o = webdriver.ChromeOptions()
o.add_argument = {'user-data-dir':'/Users/Application/Chrome/Default'}

Example

Code Implementation

from selenium import webdriver
#object of ChromeOptions class
o = webdriver.ChromeOptions()
#adding specific Chrome Profile Path
o.add_arguments = {'user-data-dir':'<path of specific Chrome profile>'}
#set chromedriver.exe path
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe", options=o)
#maximize browser
driver.maximize_window()
#launch URL
driver.get("https://www.tutorialspoint.com/index.htm")
#get browser title
print(driver.title)
#close browser
driver.close()

Output

raja
Published on 18-Nov-2021 11:16:37
Advertisements