Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Chrome Profile in Selenium Webdriver Python 3

So whenever I try to use my Chrome settings (the settings I use in the default browser) by adding

options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\Users\... (my webdriver path)")
driver = webdriver.Chrome(executable_path="myPath", options=options)

it shows me the error code

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes n 16-17: truncated \UXXXXXXXX escape

in my bash. I don't know what that means and I'd be happy for any kind of help I can get. Thanks in advance!

like image 652
mxrlvn Avatar asked Sep 18 '18 20:09

mxrlvn


People also ask

How do I use an existing Chrome profile in Selenium Python?

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.

How do I use an existing Chrome profile in Selenium?

We can open Chrome default profile with Selenium. To get the Chrome profile path, we need to input chrome://version/ in the Chrome browser and then press enter. We need to use the ChromeOptions class to open the default Chrome profile. We need to use the add_argument method to specify the path of the Chrome profile.

How do I use ChromeOptions?

Create an object of DesiredCapabilities Chrome class and merge the Desired Capabilities class object with Chrome Options class object using merge method. Create an object of Chrome Driver class and pass the Chrome Options Selenium object as an argument.


2 Answers

The accepted answer is wrong. This is the official and correct way to do it:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = webdriver.ChromeOptions()
options.add_argument(r"--user-data-dir=C:\path\to\chrome\user\data") #e.g. C:\Users\You\AppData\Local\Google\Chrome\User Data
options.add_argument(r'--profile-directory=YourProfileDir') #e.g. Profile 3
driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=options)
driver.get("https://www.google.co.in")

To find the profile folder on Windows right-click the desktop shortcut of the Chrome profile you want to use and go to properties -> shortcut and you will find it in the "target" text box.

like image 194
uzumaki Avatar answered Sep 21 '22 21:09

uzumaki


To get the path, follow the steps below.

In the search bar type the following and press enter

enter image description here

This will then show all the metadata. There find the path to the profile

enter image description here

like image 34
AzyCrw4282 Avatar answered Sep 18 '22 21:09

AzyCrw4282