Why does headless in python selenium with Chromium not work? - python

I'm try to run a code on my Raspberry Pi headless. In normal mode it works totally fine, but if I try to make it headless the code "ignores" it.
I tried different ways of headless, with -- or without , it didn't changes anything.
My current code looks so:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import time
options = Options()
options.add_argument ("-headless")
options.add_argument ("-disable-gpu")
allesFertig = True
driver = webdriver.Chrome(options = options, executable_path ='/usr/lib/chromium-browser/chromedriver')
driver = webdriver.Chrome()
Any ideas to fix it ?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import time
options = Options()
options.add_argument ("headless")
options.add_argument ("disable-gpu")
allesFertig = True
driver = webdriver.Chrome(options = options, executable_path ='/usr/lib/chromium-browser/chromedriver')
just remove second driver , you are creating chrome instance again without options thats why its opening GUI

Related

Not start Chromium with headless mode from selenium

Lately one of my python selenium script running on Raspi4 linux stops working properly. The same code worked well before and I'm trying to figure out the reason.
The example code below works without headless option, but it is stuck in starting Chromium. Does anyone encounter similar issue?
Chromium and chromedriver version is 97.0.4692.99.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
url = 'https://www.google.com'
options = Options()
options.add_argument('headless')
#options.add_argument('-headless')
#options.add_argument('--headless')
#options.headless = True
service = ChromeService(executable_path='C:\selenium\driver\chromedriver.exe')
chrome = webdriver.Chrome(service=service, options=options)
chrome.get(url)
chrome.save_screenshot('xx.png')
chrome.close()
Instead of using --headless argument, you need to use the headless property as follows:
from selenium.webdriver.chrome.options import Options
options = Options()
options.headless = True
Reference
You can find a couple of relevant detailed discussion in:
DeprecationWarning: use setter for headless property instead of set_headless opts.set_headless(headless=True) using Geckodriver and Selenium in Python
How to make Firefox headless programmatically in Selenium with Python?
How to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium?

Cannot use "get" function to load site after opening chrome with active profile on Chrome with Selenium?

I am sucesfully able to load my chrome profile using the flags:
user-data-dir as well as profile-directory, yet once the profile is loaded and the chrome window is actually open, no webpage appears. It simply gets stuck on a blank screen.
When I remove the code for the profile it is actually able to open the webpage stored in the login-url variable.
Tried updating to latest version of chrome (94.0.4606.81) and I also used the exact steps listed here to ensure I have the right chrome driver version.
I also did the obvious like making sure there are not any instances of chrome running in the background.
Code is as follows:
import os
from os.path import exists
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
headless = False
login_url = "https://google.com)"
def startChrome():
global headless
try:
chrome_options = Options()
if headless:
chrome_options.add_argument("--headless")
chrome_options.add_argument("----user-data-dir=C:/Users/ERIK/AppData/Local/Google/Chrome/User Data")
chrome_options.add_argument("--profile-directory=Profile 1")
global driver
driver = webdriver.Chrome(path+"/chromedriver.exe", options=chrome_options)
except:
print("Failed to start Chrome!")
input()
exit()
startChrome()
driver.get(login_url)
input()
The following successfully opens google.com for me.
Selenium Version 3.141.0
ChromeDriver Version 94.0.4606.61
Chrome Version 94.0.4606.71
from os import path
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
def getDriver(profile_directory, headless = False):
chrome_options = Options()
if headless:
chrome_options.add_argument("--headless")
userDataDir = path.expandvars(r"%LOCALAPPDATA%\Google\Chrome\User Data")
chrome_options.add_argument(f"--user-data-dir={userDataDir}")
chrome_options.add_argument(f"--profile-directory={profile_directory}")
return webdriver.Chrome("./chromedriver.exe", options=chrome_options)
driver = getDriver("Profile 2")
driver.get("https://google.com")

Loading Selenium user profile in Tor Chrome on Windows

This code works for Windows where it launches Chrome connected via Tor. Keep in mind you have to have Tor browser running beforehand. How can I enable the user-profile and start the browser logged in? I have tried the regular method. I have only 1 profile. Default. Doesn't seem to be working. Any clues?
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
tor_proxy = "127.0.0.1:9150"
chrome_options = Options()
'''chrome_options.add_argument("--test-type")'''
chrome_options.add_argument('--ignore-certificate-errors')
'''chrome_options.add_argument('--disable-extensions')'''
chrome_options.add_argument('disable-infobars')
'''chrome_options.add_argument("--incognito")'''
chrome_options.add_argument('--user-data=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data\\Default')
chrome_options.add_argument('--proxy-server=socks5://%s' % tor_proxy)
driver = webdriver.Chrome(executable_path='C:\\chromedriver.exe', options=chrome_options)
driver.get('https://www.gmail.com')
time.sleep(4)
driver.switch_to.frame(0)
driver.find_element_by_id("introAgreeButton").click()
Use this instead.
chrome_options.add_argument("user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data")
you don't have to specify the profile directory (Default) as it is used by default if nothing is explicitly specified using below code. SO in your case use only the above line of code
chrome_options.add_argument("profile-directory=Profile 1")
Eg:
from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument(r"user-data-dir=C:\Users\prave\AppData\Local\Google\Chrome\User Data")
driver =webdriver.Chrome("./chromedriver.exe",options=options)
driver.get(url)
Output:

Disable python chrome driver extensions without lose driver path

I started having issues running a python script which uses selenium and chrome driver, so I want to disable extensions of chrome driver using python without losing the path where the driver is located.
Currently I have the below:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
path_to_Ie = 'C:\\Python34\\ChromeDriver\\ChromeDriver.exe'
browser = webdriver.Chrome(executable_path = path_to_Ie)
url = 'https://wwww.test.com/'
browser.get(url)
and I would like to add the below lines:
chrome_options = Options()
chrome_options.add_argument("--disable-extensions")
browser = webdriver.Chrome(chrome_options=chrome_options)
Any idea how I can merge both codes to make it work properly?
Thanks a lot.
Just provide multiple keyword arguments:
chrome_options = Options()
chrome_options.add_argument("--disable-extensions")
browser = webdriver.Chrome(executable_path=path_to_Ie, chrome_options=chrome_options)

How to start Chrome with webdriver and quic disabled?

I need to start chrome with webdriver with quic disabled as follow:
--flag-switches-begin --disable-quic --flag-switches-end
I am using python with selenium 2.47.3
from selenium import webdriver
from selenium.webdriver.chrome.options import Options as ChromeOptions
options = webdriver.ChromeOptions()
options.add_argument("--disable-quic")
_browser = webdriver.Chrome(chrome_options=options)
Doing that does not put --disable-quic in between --flags-switches-begin and end.
In case anyone is still looking for this, the correct way to do this is:
options = webdriver.ChromeOptions()
options.add_argument("disable-quic") # not "--disable-quic"
_browser = webdriver.Chrome(options=options)

Categories

Resources