Open Profile in Chrome Selenium - python

I want to open profile in selenium (for login in whatsapp for once) like the below code but when run my code chrome open a second tab with this url:http://%3D%20c/Users/Ali/AppData/Local/Google/Chrome/User%20Data/Default
from selenium import webdriver
from selenium.webdriver import ChromeOptions
PATH = "chromedriver.exe"
url = "https://web.whatsapp.com/"
CH_Option = ChromeOptions()
CH_Option.add_argument("user-data-dir = C:/Users/Ali/AppData/Local/Google/Chrome/User Data/Default")
Driver = webdriver.Chrome(PATH,options=CH_Option)
Driver.get(url)
......ScreenShot.....

To fix this, change the add_argument line to (removing the spaces in the string):
CH_Option.add_argument("user-data-dir=C:/Users/Ali/AppData/Local/Google/Chrome/User Data/Default")

Try to change / in your path to \\
CH_Option.add_argument("user-data-dir=C:\\Users\\Ali\\AppData\\Local\\Google\\Chrome\\User Data\\Default")
Try to create a Guest user profile in your chrome driver and change the path to
CH_Option.add_argument("user-data-dir=C:\\Users\\Ali\\AppData\\Local\\Google\\Chrome\\User Data\\Guest Profile")

Related

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

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.

set "user-data-dir" in selenium python for chrome driver in Ubuntu [duplicate]

I'd like to launch Chrome with its default profile using Python's webdriver so that cookies and site preferences persist across sessions.
How can I do that?
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.
This solved my problem. (remove Default at the end)
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--user-data-dir=/home/username/.config/google-chrome")
cls.driver = webdriver.Chrome(options=options,
executable_path="./../ext/chromedriver")
Chrome_Options ist deprecated. Use options instead
I solved my problem with answer of "Yoannes Geissler".
In my case My profile was named "Profile 2"
My Code :
options = webdriver.ChromeOptions()
options.add_argument('--user-data-dir=C:/Users/GOD/AppData/Local/Google/Chrome/User Data')
options.add_argument('--profile-directory=Profile 2')
wd = webdriver.Chrome(options=options)
The below line solved my problem:
options.add_argument('--profile-directory=Profile 2')
Just to share what worked for me. Using default's profile was complicated, chrome keeps crashing.
from pathlib import Path
from selenium import webdriver
driver_path = Path("{}/driver/chromedriver75.exe".format(PATH_TO_FOLDER))
user_data_dir = Path("{}/driver/User Data".format(PATH_TO_FOLDER))
options = webdriver.ChromeOptions()
# TELL WHERE IS THE DATA DIR
options.add_argument("--user-data-dir={}".format(user_data_dir))
# USE THIS IF YOU NEED TO HAVE MULTIPLE PROFILES
options.add_argument('--profile-directory=Default')
driver = webdriver.Chrome(executable_path=driver_path, options=options)
driver.get("https://google.com/")
By doing this Chrome will create the folder User Data and keep all the data in it where I want and it's easy to just move your project to another machine.
This answer is pretty simple and self-explained.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
exec_path_chrome = "path/to/Google Chrome" #Do not use this path that is extracted from "chrome://version/"
exec_path_driver = "path/to/chromedriver"
ch_options = Options() #Chrome Options
ch_options.add_argument("user-data-dir = /path/to/Chrome Profile") #Extract this path from "chrome://version/"
driver = webdriver.Chrome(executable_path = exec_path_driver, options = ch_options) #Chrome_Options is deprecated. So we use options instead.
driver.get("https://stackoverflow.com/a/57894065/4061346")
As #MadRabbit said type chrome://version/ into the address bar to find the path to your chrome profile data.
It appears like this in Windows C:\Users\pc\AppData\Local\Google\Chrome\User Data\Default
It appears like this in Mac /Users/user/Library/Application Support/Google/Chrome/Default
So all you have to do is to erase the last portion Default from the profile path.
Note: Make sure you don't run more than one session at the same time to avoid problems.
This is what I did.
import chromedriver_binary
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
chrome_options.add_argument(r"--user-data-dir=User Data Directory")
chrome_options.add_argument(r"--profile-directory=Profile name")
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches",["enable
logging"])
chrome_options.add_experimental_option("excludeSwitches", ["enable
automation"])
chrome_options.add_argument("start-maximized")
self.driver = webdriver.Chrome(chrome_options=chrome_options)
self.wait = WebDriverWait(self.driver, 20)
Here we do not need to give the chrome driver path.

How to load default profile in Chrome using Python Selenium Webdriver?

I'd like to launch Chrome with its default profile using Python's webdriver so that cookies and site preferences persist across sessions.
How can I do that?
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.
This solved my problem. (remove Default at the end)
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--user-data-dir=/home/username/.config/google-chrome")
cls.driver = webdriver.Chrome(options=options,
executable_path="./../ext/chromedriver")
Chrome_Options ist deprecated. Use options instead
I solved my problem with answer of "Yoannes Geissler".
In my case My profile was named "Profile 2"
My Code :
options = webdriver.ChromeOptions()
options.add_argument('--user-data-dir=C:/Users/GOD/AppData/Local/Google/Chrome/User Data')
options.add_argument('--profile-directory=Profile 2')
wd = webdriver.Chrome(options=options)
The below line solved my problem:
options.add_argument('--profile-directory=Profile 2')
Just to share what worked for me. Using default's profile was complicated, chrome keeps crashing.
from pathlib import Path
from selenium import webdriver
driver_path = Path("{}/driver/chromedriver75.exe".format(PATH_TO_FOLDER))
user_data_dir = Path("{}/driver/User Data".format(PATH_TO_FOLDER))
options = webdriver.ChromeOptions()
# TELL WHERE IS THE DATA DIR
options.add_argument("--user-data-dir={}".format(user_data_dir))
# USE THIS IF YOU NEED TO HAVE MULTIPLE PROFILES
options.add_argument('--profile-directory=Default')
driver = webdriver.Chrome(executable_path=driver_path, options=options)
driver.get("https://google.com/")
By doing this Chrome will create the folder User Data and keep all the data in it where I want and it's easy to just move your project to another machine.
This answer is pretty simple and self-explained.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
exec_path_chrome = "path/to/Google Chrome" #Do not use this path that is extracted from "chrome://version/"
exec_path_driver = "path/to/chromedriver"
ch_options = Options() #Chrome Options
ch_options.add_argument("user-data-dir = /path/to/Chrome Profile") #Extract this path from "chrome://version/"
driver = webdriver.Chrome(executable_path = exec_path_driver, options = ch_options) #Chrome_Options is deprecated. So we use options instead.
driver.get("https://stackoverflow.com/a/57894065/4061346")
As #MadRabbit said type chrome://version/ into the address bar to find the path to your chrome profile data.
It appears like this in Windows C:\Users\pc\AppData\Local\Google\Chrome\User Data\Default
It appears like this in Mac /Users/user/Library/Application Support/Google/Chrome/Default
So all you have to do is to erase the last portion Default from the profile path.
Note: Make sure you don't run more than one session at the same time to avoid problems.
This is what I did.
import chromedriver_binary
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
chrome_options.add_argument(r"--user-data-dir=User Data Directory")
chrome_options.add_argument(r"--profile-directory=Profile name")
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches",["enable
logging"])
chrome_options.add_experimental_option("excludeSwitches", ["enable
automation"])
chrome_options.add_argument("start-maximized")
self.driver = webdriver.Chrome(chrome_options=chrome_options)
self.wait = WebDriverWait(self.driver, 20)
Here we do not need to give the chrome driver path.

Getting Chrome to launch via Selenium

I am having issues getting an instance of a Chrome browser from selenium in python. I'm using Windows 8. I have downloaded the chromedriver binary and added it to my path but I get the following error in Python:
selenium.common.exceptions.WebDriverException: Message: 'ChromeDriver executable needs to be available in the path.
This error occurs for the following line:
driver = webdriver.Chrome(executable_path='path\to\chromedriver_win32_2.0')
Two ways to set it, you somehow mixed up.
Put the chromedriver.exe's path into PATH (on Windows), so your PATH setting is correct, but you need to call the default constructor.
driver = webdriver.Chrome()
Specify the path in webdriver.Chrome(executable_path='some path'). Here you need the full path to the executable, not the directory.
webdriver.Chrome(executable_path=r'C:\Users\HaranKumar\Downloads\chromedriver_win32_2.0\chromedriver.exe')
Choose either one you want.
Assuming that your path is correct, make sure that you include the chromedriver itself: chromedriver.exe
I used the following and it worked! Thanks!
driver = webdriver.Chrome(executable_path=r'C:\chromedriver.exe')
#put your own path between the ''
Even if you have chromedriver.exe in the PATH, its necessary to have chromedriver.exe in the folder where your executable scripts are present(atleast so is the case when it comes to python scripts)
for python(selenium) you will need:
from selenium import webdriver
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
then put your chromedriver.exe path in. the "r" is just to prevent it from detecting the \ and causing errors in python
PATH = r"C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
options = webdriver.ChromeOptions()
you can now order the driver to get websites
driver.get('http://www.google.com')
Update 2021
For Python
I got Selenium to open my default Chrome browser with my profile using this:
options.add_argument("--user-data-dir=C:\\Users\\Sams\\AppData\\Local\\Google\\Chrome\\User Data")
options.add_argument('--profile-directory=Default')
Go to: chrome://version/
Look for your "Profile Path"
profile path image
Copy your Profile Path and replace "options.add_argument("--user-data-dir={YOUR PROFILE PATH}")"
The second argument may also look different. Mine so happens to be "Default". For other it may be "Profile 1" or "Profile X" X being an incrementing number.
Close all your Chrome browsers before running. Because the Chrome Driver cannot run an automated browser alongside the rest of your other tabs.
Here is my entire Selenium Config. Hopefully it helps someone.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.remote.webdriver import WebDriver
options = Options()
options.add_argument("--window-size=1920,1080")
options.add_argument("--log-level=3")
options.add_experimental_option('excludeSwitches', ['enable-logging'])
# The 2 arguments below will use your main browser.
options.add_argument("--user-data-dir=C:\\Users\\Sams\\AppData\\Local\\Google\\Chrome\\User Data") # profile path (C)
options.add_argument('--profile-directory=Default')
options.headless = False # To show Chrome or not to show?
PATH = (r"C:\Users\Sams\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\selenium\webdriver\chrome\chromedriver.exe")
CHROMEDRIVER_PATH = PATH
driver = webdriver.Chrome(CHROMEDRIVER_PATH, options=options)
I recently ran into this error and I found three main solutions for this chromedriver not in path error
download a library that does it - webdriver_manager or chromedriver_autoinstaller both work
add it to path in your program
add chromedriver executable to your system PATH
Update 2016
The following solution works for me, with WebDriver 3.0.1, Chrome Driver 2.25.426923, Window 7
System.setProperty("webdriver.chrome.driver","D:\\workspace\\chromedriver.exe");
WebDriver driver;
driver = new ChromeDriver();
*Note:
Chrome Driver
See also: http://www.frontendtest.org/blog/path-executable-chrome/

Categories

Resources