Python Selenium user data not loading in headless mode - python

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.

Related

Selenium: How can I find the correct command for add_arguments()?

I am using selenium python and chrome driver and wanted to know if there is any way you can get the command to enable or disable options in add_arguments() function. For example, there are '--disable-infobars', etc., but if I come across a new setting, how do I find its appropriate command?
An example being the settings to auto-download pdfs.
Any help is appreciated.
Chromium has lots of command switches, such as --disable-extensions or --disable-popup-blocking that can be enabled at runtime using Options().add_argument()
Here is a list of some of the Chromium Command Line Switches.
Chromium also allows for other runtime enabled features, such as useAutomationExtension or plugins.always_open_pdf_externally. These features are enabled using DesiredCapabilities.
I normal review the source code for Chromium when I need to find find other features to control with DesiredCapabilities.
The code below uses both command switches and runtime enabled features to automatically save a PDF file to disk without being prompted.
For my answer I downloaded a PDF file from the Library of Congress.
If you have any questions related to this code or something else related to your question please let me know.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
chrome_options = Options()
chrome_options.add_argument('--disable-infobars')
chrome_options.add_argument('--start-maximized')
chrome_options.add_argument('--disable-extensions')
chrome_options.add_argument('--disable-popup-blocking')
# disable the banner "Chrome is being controlled by automated test software"
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])
# you can set the path for your download_directory
prefs = {
'download.default_directory': 'download_directory',
'download.prompt_for_download': False,
'plugins.always_open_pdf_externally': True
}
capabilities = DesiredCapabilities().CHROME
chrome_options.add_experimental_option('prefs', prefs)
capabilities.update(chrome_options.to_capabilities())
driver = webdriver.Chrome('/usr/local/bin/chromedriver', options=chrome_options)
url_main = 'https://www.loc.gov/aba/publications/FreeLCC/freelcc.html'
driver.get(url_main)
driver.implicitly_wait(20)
download_pdf_file = driver.find_element_by_xpath('//*[#id="main_body"]/ul[2]/li[1]/a')
download_pdf_file.click()
You can add options arguments to a chromium webdriver using Python the following way:
options = webdriver.ChromeOptions()
# Arguments go below
options.add_argument("--no-sandbox")
options.add_argument("--disable-gpu")
options.add_argument("--window-size=800,600")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--user-agent={}".format("your user agent string"))
# Etc etc..
options.binary_location = "absolute/path/to/chrome.exe"
driver = webdriver.Chrome(
desired_capabilities=caps,
executable_path="absolute/path/to/chromium-driver.exe",
options=options,
)
Here you can find the list of all the supported arguments for chrome.

How to use Chrome Profiles through Headless Chrome using Selenium and Python

def __init__(self):
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=bot_data")
options.add_argument("--headless") # Runs Chrome in headless mode.
options.add_argument('--no-sandbox') # Bypass OS security model
options.add_argument('--disable-gpu') # applicable to windows os only
options.add_argument('start-maximized') #
options.add_argument('disable-infobars')
options.add_argument("--disable-extensions")
# self.driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
self.driver = webdriver.Chrome('chromedriver.exe',
options=options)
self.driver.get('https://www.google.com')
self.wait = WebDriverWait(self.driver, 10)
There is have my codes. I want to change it to headless browser. But i am getting an error.
I added screenshot to show error.
This error message...
ERROR:devtools_http_handler.cc(288)] Error writing DevTools active port to file
...implies that there was an error while writing DevTools active port to the required file.
As per the discussion in How to open a Chrome Profile through Python instead of specifying only the directory name through user-data-dir, you need to pass the absolute path of the user-data-dir.
Solution
So you need to replace the line of code:
options.add_argument("user-data-dir=bot_data")
With:
options.add_argument("user-data-dir=C:\\Users\\AtechM_03\\AppData\\Local\\Google\\Chrome\\User Data\\bot_data")
Reference
You can find a couple of relevant discussions in:
How to use Chrome Profile in Selenium Webdriver Python 3
Selenium: Point towards default Chrome session
Outro
A couple of relevant documentations:
Session isolation in Headless Chrome
headless: Introduce a browser context
Save and restore browser sessions
Headless maintains a different profile folder structure to headful

Chrome does not load profile

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')

How to use my own user chrome profile to open a browser that already logged in Facebook?

options = webdriver.ChromeOptions()
options.add_argument('user-data-dir=/Users/yuzhipeng/Library/Application Support/Google/Chrome')
driver = webdriver.Chrome(executable_path='/Users/yuzhipeng/Downloads/chromedriver',chrome_options=options)
driver.implicitly_wait(a)
driver.get("https://www.facebook.com/")
This is my code, however it did not work.

Open a chrome profile in python that is already signed into google

I'm trying to open up my chrome profile which is signed into google however am not having any luck.
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument(r'user-data-
dir=G:\Users\Kareem\AppData\Local\Google\Chrome\User Data\Profile 10')
chrome_path= r"G:\Users\Kareem\Documents\Work\Computer
Science\Selenium\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get("https://www.google.co.uk/webhp?source=search_app")
When you create the driver, you're not actually passing the options to it.
driver = webdriver.Chrome(chrome_options=options)

Categories

Resources