How to open a already logged-in chrome browser in selenium python - python

I want to open a browser that is logged in to my gmail account like my default browser in selenium. Is there a way to do this?
edit:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
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)
this method does not work for me.

Chrome Options
Chrome options are very particular on how you call them. Syntax needs to be followed with perfection. Correct your below syntax errors by changing your old code:
options.add_argument("user-data-dir=C:\\Path")
To the following code:
userdatadir = 'C:/Users/<user>/AppData/Local/Google/Chrome/User Data'
chromeOptions.add_argument(f"--user-data-dir={userdatadir}")

You can use your profile that you use to open browser:
from selenium import webdriver
profile = webdriver.FirefoxProfile('/home/user/.mozilla/firefox/xxxx-release/') # this work for linux, if you use windows, you can find this file in local AppData
browser = webdriver.Firefox(executable_path='./geckodriver', firefox_profile =profile)
This will use your standard profile that you use with your standard browser, including logins, cookies etc.
Edit:
I used firefox here, but the same principle works with chrome as well.

options = webdriver.ChromeOptions()
options.add_argument(f"user-data-dir={CHROME_USER_DIR}")
options.add_argument("profile-directory=Default")
This will open the Default Profile in Chrome.
The user-data-dir looks something like C:\Users\<username>\AppData\Local\Google\Chrome\User Data
To open a different profile, replace the Default with your profile directory

Related

Why driver.get doesn't work in Python Selenium when using Profile

What I need
I need to run Selenium with Chrome Profile
Here is my code:
from selenium import webdriver
import time
options = webdriver.ChromeOptions()
options.add_argument("--user-data-dir=/home/atom/.config/google-chrome") #Path to your chrome profile
options.add_argument('--profile-directory=Profile 1')
driver = webdriver.Chrome(options=options)
driver.get('https://youtube.com')
time.sleep(5)
Problem
The code opens Google Chrome instead of Chromedriver. Profile is loaded. But driver.get doesn't work
Question
How can I use Selenium with Chrome profile?
What chromedriver does is briefly described here. When you call get on the webdriver, you are asking for the site to be opened on the chrome browser. If you want to use brave browser or another browser implementation using the chromium engine, you may specify the location of the browser's binary using:
ChromeOptions options = new ChromeOptions();
options.setBinary("/path/to/other/chrome/binary");
You may load a specific profile by using:
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=/path/to/your/custom/profile");
Specifying no profile (not including this option should load the default profile).
A complete list of supported options may be found here.

How can open my local Chrome (no chromedriver.exe) and navigate with Selenium

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
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.
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

Selenium either Custom Profile or get() method not working

I was trying to control an already open instance of Chrome, but when that didn't work, I started to try to make a new instance of Chrome on my profile. I found two stackoverflow posts detailing the problem (How to use Chrome Profile in Selenium Webdriver Python 3 and Selenium: get() not working with custom google profile) but neither solution worked. I have two code blocks, both of which has its own problem.
The first thing I tried was this:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_argument(r"--user-data-dir=C:\\Users\\EliBa\\AppData\\Local\\Google\\Chrome\\User Data")
options.add_argument(r'--profile-directory=Default')
driver = webdriver.Chrome(executable_path=r'C:\\Selenium Drivers\\chromedriver.exe', chrome_options=options)
driver.get("https://google.com")
While this opened my profile, it did not open google like I asked it to. The second thing I tried was this:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("user-data-dir=C:\\Users\\EliBa\\AppData\\Local\\Google\\Chrome\\User Data\\Default")
driver = webdriver.Chrome(executable_path='C:\\Selenium Drivers\\chromedriver.exe', options=options)
driver.get("https://www.google.com")
While this opened Google, it did not open my custom profile. Does anyone know what I should be putting so that the custom profile opens AND it loads the site I ask it to?

how to run Selenium ChromeDriver with cookies from actual Chrome installation?

I want to use Selenium(python) to log in to several sites and I need to get all my Chrome cookies,
How should I do this?
This is a duplicate question, anyways:
Do the following:
# Remember to import
from selenium.webdriver.chrome.options import Options
options = Options()
# Your user agent
options.add_argument("YOUR_USER_AGENT")
# Path to chrome application
options.add_argument("CHROME_APPLICATION_PATH")
# Path to chrome driver
driver = webdriver.Chrome(executable_path="CHROME_DRIVER_PATH", options=options)
You will need to do some research about your own system and see what exactly is your user-agent (there are a million site that tell you)
As well as finding the correct path for your chrome application.
Then, proceed normally, this will execute chrome as if you opened it yourself.

Chrome Webdriver doesn't load the default profile

The Selenium Chrome Webdriver does not load my default Chrome profile.
I already tried many other Stack Overflow solutions including changing the path (and using the local Chrome App, which gives a 'permission denied' Error).
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_argument("--user-data-dir=~/Library/Application Support/Google/Chrome/Default")
options.add_argument("--profile-directory=Default")
driver = webdriver.Chrome()
driver.get("https://tagmanager.google.com/#/home")
assert "Google Analytics" in driver.title
account_element = driver.find_element_by_css_selector("div.account.card div#149385038 table")
accountPublicId = driver.find_element_by_css_selector("div#149385038 table td.account__public-id")
The result is still the same; it loads only a 'naked' Chrome Webdriver instead of loading the local default profile (I use everyday for work).
Update:
I don't know how or why but now, when I quit Chrome and start Chrome through the Python Script, Google Chrome starts with my profile but does not use the cookies I have in that profile.
I will see if I can add the cookies "manually" with an options.add_arguments.
Try this:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\ProfilePath") ##profile path
w = webdriver.Chrome(executable_path="C:\\chromedriver.exe", chrome_options=options)
you may try this
options.add_argument("user-data-dir=C:\\Test")
did not add anything to the
options arguments (looking via debugger it was an empty list)
options.arguments.append("user-data-dir=C:/test/") managed to update the list of arguments
and it worked.

Categories

Resources