I am stucking with a script to do the following
I want to open my local chrome in which I have several accounts logged in, so for this I do:
from selenium import webdriver
driver = webdriver.Chrome(executable_path="C:/Program Files (x86)/Google/Chrome/Application/chrome.exe")
but when I do that
driver.get(url)
doesn't send me to the URL I want.
On the other hand if I do it with
driver = webdriver.Chrome()
driver.get(URL)
Everything goes smoothly but it opens it with the chromedriver.exe and therefore the accounts are not logged in.
Any idea how to open the local chrome and then be able to browse?
Any solutions for the problem
CodePudding user response:
You need to load your user profile while initializing selenium webdriver. You can do it using
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Path to local user profile") #Path to your chrome profile
w = webdriver.Chrome(executable_path="C:\\Path to your local chrome.exe installation", chrome_options=options)
Typing chrome://version/
in chrome address bar will show you the path to user profile.
CodePudding user response:
In automation, when we use Selenium to interact with a web browser, we need a browser driver to communicate with the specific browser. ChromeDriver is the specific driver for Google Chrome. When you use webdriver.Chrome()
, it launches the Chrome browser using the ChromeDriver
executable. Chromedriver is a bridge between your Selenium script and the Chrome browser exactly as taxi driver is a bridge between you and taxi car.
Chrome uses profiles. By default in tests are not used local profiles of users. The new temporary profile is created instead. However, it is possible to point out the profile which should be used.
You need to pass additional argument user-data-dir
to the Chrome options when initialising the Chrome driver, in order to open the local default Chrome profile. If you need to point out specific profile then you should use profile-directory
.
Example:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument(r"--user-data-dir=C:/Users/[username]/AppData/Local/Google/Chrome/User Data")
options.add_argument(r'--profile-directory=YourProfileDir')
driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=options)
driver.get(URL)
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.
See also the following answers:
- https://stackoverflow.com/a/66179265/21085370
- https://stackoverflow.com/a/69251146/21085370
- https://stackoverflow.com/a/67389309/21085370
The official documentation is here