Chrome Webdriver doesn't load the default profile - python

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.

Related

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?

Can I get the gecko driver to launch in an existing Firefox window?

I'm trying to use Selenium in Python to fill out a form, which is why I need it to run in an existing Firefox window where I'm logged in due to the sensitive login data. But I can't seem to find a way to prevent the driver launching in a new window where I am logged out. Is there a way to do this?
I've tried using options to set preference:
`from selenium.webdriver.firefox.options import Options
options = Options()
options.set_preference("browser.tabs.loadInBackground", False)
driver = webdriver.Firefox(options=options)`
I also tried using the -no-remote command on Firefox in cmd before running the script, but it didn't work either.
From chrome driver you can achieve this.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("user-data-dir=C:/Users/Designer1/AppData/Local/Google/Chrome/User Data/Profile 1")
driver = webdriver.Chrome(chrome_options=options,executable_path="C:\webdrivers\chromedriver.exe")

How to open a already logged-in chrome browser in selenium 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

Selenium python - How to load Default Chrome Profile

I need to load my own Default chrome profile where all my saved login info is, instead of selenium logging me into my websites.
I have taken this code from stackoverflow, cannot find the exact user. My issue with this code is selenium opens chrome but does not load the correct profile. Every time I open run the code it creates a new "scoped_dir" folder and runs the profile "default" from there chrome version
(C:\Users\farja\AppData\Local\Temp\scoped_dir[bunch of numbers]\Default).
I have tried closing chrome and then running my code which doesn't work
I am thinking there is a big flaw in my code but do not know what it is or how to find it. A relevant answer for 2022 would be very very much appreciated as I have literally been stuck on this for a week now and have tried multiple answers on stackoverflow, the web and youtube but nearly all give me a deprecation error.
Thank you for taking the time to read.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
ser = Service(r'C:\Users\farja\Documents\Instagram Programmes\Scheduler 2\chromedriver')
op = webdriver.ChromeOptions()
s = webdriver.Chrome(service=ser, options=op)
op.add_argument(r"--user-data-dir=C:\\Users\\farja\\AppData\\Local\\Google\\Chrome\\User Data")
op.add_argument("--profile-directory=Default")
s.get("chrome://version/")
As your usecase is to load the Default Chrome Profile you can use the argument user-data-dir and remove the argument --profile-directory as follows:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
op = webdriver.ChromeOptions()
op.add_argument(r"--user-data-dir=C:\\Users\\farja\\AppData\\Local\\Google\\Chrome\\User Data\\Default")
ser = Service(r'C:\Users\farja\Documents\Instagram Programmes\Scheduler 2\chromedriver')
s = webdriver.Chrome(service=ser, options=op)
s.get("chrome://version/")
References
You can find a couple of relevant detailed discussions in:
How to open a Chrome Profile through Python
How to open a Chrome Profile through --user-data-dir argument of Selenium

How to initiate a Chromium based Vivaldi browser session using Selenium and Python

I am trying to use the vivaldi browser with Selenium. It is a chromium browser that runs very similar to chrome. I have Selenium working with Firefox (geckodriver), and Google Chrome(chromedriver), but I can't seem to find a way with Vivaldi. Any help would be appreciated.
If the vivaldi binary by default is located at C:\Users\levir\AppData\Local\Vivaldi\Application\vivaldi.exe you can use the following solution:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("start-maximized")
options.binary_location=r'C:\Users\levir\AppData\Local\Vivaldi\Application\vivaldi.exe'
driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', options=options)
driver.get('http://google.com/')
For future reference:
to make Vivaldi work with selenium you need to make sure of three things :
The correct version of ChromeDriver
Set selenium's driver to use Vivaldi's binary via webdriver.ChromeOptions()
Make sure you're getting the correct url (don't forget "https://")
All of the above is explained step by step with screenshots in this blog post
The key executable_path will be deprecated in the upcoming releases of Selenium.
This post has the solution. I'm posting a copy of said solution with the path to Vivaldi, where the username is fetched by the script so you don't have to hard code it.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
import os
current_user = os.getlogin()
s = Service(rf"C:\Users\{current_user}\AppData\Local\Vivaldi\Application\vivaldi.exe")
driver = webdriver.Chrome(service=s)
driver.get("http://duckduckgo.com") # or your website of choice
You can use ChromeOptions and supply binary.
from selenium.webdriver.chrome.options import Options
opt = Options()
opt.binary_location = chromium_path//path to chromium binary
driver = webdriver.Chrome(options=opt, executable_path="path_to_chromedriver")

Categories

Resources