Chrome does not load profile - python

For some reason the program does not load the profile. The location of the profile is fine.
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\\Aron\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 2")
capabilities = DesiredCapabilities.CHROME.copy()
chromedriver = 'C:\\test\\chromedriver.exe'
# Opening the browser
driver = webdriver.Chrome(executable_path = chromedriver, chrome_options=options, desired_capabilities=capabilities)

Two steps:
1 - When using an empty folder for the profile Chrome creates a new profile in that folder. If it creates the profile you can suppose that it also loads it next time.
options = webdriver.ChromeOptions()
options.add_argument(r"user-data-dir=C:\test\Profile 2")
chromedriver = r'C:\test\chromedriver.exe'
driver = webdriver.Chrome(executable_path=chromedriver, chrome_options=options)
2 - But to be sure you can show the used profile in Chrome it self with chrome://version/.
options = webdriver.ChromeOptions()
options.add_argument(r"user-data-dir=C:\test\Profile 2")
chromedriver = r'C:\test\chromedriver.exe'
driver = webdriver.Chrome(executable_path=chromedriver, chrome_options=options)
driver.get('chrome://version/')
input('Enter')

Related

Python Selenium user data not loading in headless mode

When I create a profile and log into the account, the next time I create a driver using the same profile all accounts are stored. However, when I switch to headless, the profile does not seem to be recognized.
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=" + running_loc + "\\profiles\\" + profile) # Path to your chrome profile
options.add_argument("--log-level=3")
if 'visual' not in args:
options.add_argument("--headless")
options.add_argument("--window-size=2560,1440")
options.add_argument("--start-maximized")
caps = DesiredCapabilities().CHROME
caps["pageLoadStrategy"] = "none"
driver = webdriver.Chrome(ChromeDriverManager(cache_valid_range=7).install(), desired_capabilities=caps,
options=options)
So in the above code, when running non-headless, I face no issues with logins, but in headless everything requires a login once again.

Selenium Chrome: Load profile and change download folder - Python

OS: Win 10
Chrome: 81.0.4044.129
ChromeDriver: 81.0.4044.69
Goal:
Load an existing profile with extensions and preference configured AND specify default download locations.
Purpose:
I want to save images into their respective folders name.
Challenges
If I specify a Chrome profile to be loaded, then I cannot change the default download folder.
Code Snippets:
# Loading profile works!
options = webdriver.ChromeOptions()
options.add_argument(f'user-data-dir={profile_path}')
options.add_argument(f'--profile-directory={profile_name}')
driver = webdriver.Chrome(chrome_options=options)
# Changing default download location works!
options = webdriver.ChromeOptions()
prefs = {"download.default_directory" : "C:/Downloads/Folder A"}
options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(chrome_options=options)
# This DOESN'T work! Default download location is not changed.
options = webdriver.ChromeOptions()
options.add_argument(f'user-data-dir={profile_path}')
options.add_argument(f'--profile-directory={profile_name}')
prefs = {"download.default_directory" : "C:/Downloads/Folder A"}
options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(chrome_options=options)
Is it possible to BOTH load the profile and change the default download location before the driver is created?
I believe there isn't a way to both load an existing profile and also to change the default_directory option.
So instead, I used json.loads and json.dump to modify the 'Preference' file before loading the profile.
import json
from selenium import webdriver
# helper to edit 'Preferences' file inside Chrome profile directory.
def set_download_directory(profile_path, profile_name, download_path):
prefs_path = os.path.join(profile_path, profile_name, 'Preferences')
with open(prefs_path, 'r') as f:
prefs_dict = json.loads(f.read())
prefs_dict['download']['default_directory'] = download_path
prefs_dict['savefile']['directory_upgrade'] = True
prefs_dict['download']['directory_upgrade'] = True
with open(prefs_path, 'w') as f:
json.dump(prefs_dict, f)
options = webdriver.ChromeOptions()
set_download_directory(profile_path, profile_name, download_path) # Edit the Preferences first before loading the profile.
options.add_argument(f'user-data-dir={profile_path}')
options.add_argument(f'--profile-directory={profile_name}')
driver = webdriver.Chrome(chrome_options=options)

How to hide and then open browser in selenium python?

I need to hide browser do some actions and then open browser in selenium python?
some code:
driver = webdriver.Chrome('./chromedriver') # connecting driver
options.add_argument('headless') # that's how I hide browser
driver = webdriver.Chrome(chrome_options=options)
driver.get("google.com")
and now I need to open browser for user
You wont able to do it with your current code as your have initiated chromedriver in headless mode and your browser simulation program that does not have a user interface.Also your url is not corrent in above example. Try below code
options = webdriver.ChromeOptions()
options.add_argument("--headless")
driver = webdriver.Chrome(executable_path=r" path of chromedriver.exe",chrome_options=options)
driver = webdriver.Chrome(executable_path=r"C:\New folder\chromedriver.exe")
base = "https://www.google.com/"
driver.get(base)
Output:
Another example
import time
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--headless")
driver = webdriver.Chrome(options=options)
headless_page = "https://www.google.com/"
driver.get(headless_page)
url = driver.current_url
print(url) # print headless url
time.sleep(2)
driver = webdriver.Chrome() # reset headless to false
driver.get(url)

How to initiate Chrome Canary in headless mode through Selenium and Python

from selenium import webdriver
options = webdriver.ChromeOptions()
options.binary_location = 'C:\Users\mpmccurdy\Desktop\Google Chrome Canary.lnk'
options.add_argument('headless')
options.add_argument('window-size=1200x600')
driver = webdriver.Chrome(chrome_options=options)
driver.get("https://www.python.org")
If you are using Chrome Canary as a basic Requirement the server still expects you to have Chrome installed in the default location as per the underlying OS architecture as follows:
You can also override the default Chrome Binary Location following the documentation Using a Chrome executable in a non-standard location as follows:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.binary_location = r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'
options.add_argument('--headless')
options.add_argument('window-size=1200x600')
driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=options)
driver.get("https://www.python.org")
chrome_options = Options()
chrome_options.add_argument("--headless")
path = os.getcwd() +'\\chromedriver.exe' #needs to be in your current working directory
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=path)

How to create Python Selenium Chrome webdriver remote with Current user data?

How I create LOCAL Chrome Webdriver WITH Current user data
chromedriver = "/Users....../chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
options = webdriver.ChromeOptions()
options.add_argument(r"user-data-dir=/Users..../Google/Chrome")
webdriver.Chrome(executable_path=chromedriver,
chrome_options=options)
How I create REMOTE Chrome webdriver with clear - new user data
webdriver.Remote(command_executor="http://192.168.1.30:4441/wd/hub",
desired_capabilities=DesiredCapabilities.CHROME)
Now, How create Remote Chrome webdriver with current user data?
Try this:
os.environ["webdriver.chrome.driver"] = chromedriver
options = webdriver.ChromeOptions()
options.add_argument("--user-data-dir=/Users..../Google/Chrome")
webdriver.Remote("http://192.168.1.30:4441/wd/hub",
options.to_capabilities())
And if you have your chromedriver.exe on the PATH then you should not need this part:
chromedriver = "/Users....../chromedriver"
Not sure if that works for you, but here is an example that let me start remote chrome webdriver with desired chromeOption for language:
options = webdriver.ChromeOptions()
options.add_argument("--lang=de")
chrome_remote = webdriver.Remote('http://hostname:4444/wd/hub', options.to_capabilities())

Categories

Resources