How to use certain Chrome profile with selenium python?
First of all
update your ChromeDriver to the latest version
(This part is mandatory).
Then pass two arguments --profile-directory=Profile 1
and user-data-dir=C:\\Users\\user123\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 16
to the Chrome binary
options = Options()
options.add_argument('--profile-directory=Profile 16')
options.add_argument("user-data-dir=C:\\Users\\Hana\\AppData\\Local\\Google\\Chrome\\User Data\\") #Path to your chrome profile
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe", options=options)
Note that in user-data-dir
you should only pass the path of all profiles then the name of specific profile (Here, Profile 16) should be passed through --profile-directory
You can check out full source code named PythonChromeProfile in SeleniumWebDriver-Tiny_projects on Github, Which I tested successfuly. Also you can read about creating Chrome profiles and find the path to that profile Here
How to use Chrome Profile in Selenium Webdriver Python 3
As per your question and your code trials if you want to open a Chrome Browsing Session here are the following options:
To use the default Chrome Profile:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\\AtechM_03\\AppData\\Local\\Google\\Chrome\\User Data\\Default")
driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=options)
driver.get("https://www.google.co.in")Note: Your default chrome profile would contain a lot of bookmarks, extensions, theme, cookies etc. Selenium may fail to load it. So as per the best practices create a new chrome profile for your @Test and store/save/configure within the profile the required data.
To use the customized Chrome Profile:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("user-data-dir=C:\\Users\\AtechM_03\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 2")
driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=options)
driver.get("https://www.google.co.in")Here you will find a detailed discussion on How to open a Chrome Profile through Python
How to load default profile in Chrome using Python Selenium Webdriver?
This is what finally got it working for me.
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Path") #Path to your chrome profile
w = webdriver.Chrome(executable_path="C:\\Users\\chromedriver.exe", chrome_options=options)
To find path to your chrome profile data you need to type chrome://version/
into address bar . For ex. mine is displayed as C:\Users\pc\AppData\Local\Google\Chrome\User Data\Default
, to use it in the script I had to exclude \Default\
so we end up with only C:\Users\pc\AppData\Local\Google\Chrome\User Data
.
Also if you want to have separate profile just for selenium: replace the path with any other path and if it doesn't exist on start up chrome will create new profile and directory for it.
Unable to start Chrome browser with user profile in Selenium
Finally got this fixed. I was working on macOS and found out that i needed to quit the browser cleanly.
After closing the browser window, i had to right click on the icon in the dock and press quit. The selenium script would work perfectly fine after this.
open multiple chrome profile with selenium
The user-data directory get locked by the first instance and the second instance fails with exception
So the solution is to create a Userdata for each profile
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("--user-data-dir=C:\\anyPathWhereYouWantToSaveYourUserData\\profile1")
driver = webdriver.Chrome(executable_path=r'C:\Windows\System32\chromedriver.exe', options=options)
driver.get("https://www.instagram.com")
No worries you can access your new profiles now manually if you would like to just by creating shortcuts you just need to add to your target in your shortcut
"C:\Program Files\Google\Chrome\Application\chrome.exe" --user-data-dir="C:\anyPathWhereYouWantToSaveYourUserData\profile1"
Related Topics
How to Return 2 Values from a Java Method
Number of Days in Particular Month of Particular Year
Picking a Random Element from a Set
Why Is the Max Recursion Depth I Can Reach Non-Deterministic
Lambdas: Local Variables Need Final, Instance Variables Don'T
Rotating a Shape Vertically Around the X-Axis
Why Java Inner Classes Require "Final" Outer Instance Variables
What Is the Main Difference Between Inheritance and Polymorphism
Sockets: Discover Port Availability Using Java
Differencebetween Persist() and Merge() in JPA and Hibernate
Createprocess Error=206, the Filename or Extension Is Too Long When Running Main() Method
Safely Casting Long to Int in Java
How to Instantiate an Inner Class with Reflection in Java
What Does Value & 0Xff Do in Java
Content Is Not Allowed in Prolog Saxparserexception
Android: How to Stretch an Image to the Screen Width While Maintaining Aspect Ratio