Python Selenium Chrome Not Running Headless - python

I'm currently writing a program in which I'm connecting to chrome. I would like to make this process without a new chrome window popping up every time. I know that --headless makes chrome run silently, but my current code is not working; it is still opening up chrome tabs.
Current Code:
chrome_options = Options()
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(chrome_options = chrome_options)
What am I doing wrong or what do I need to add?

If you are using linux one alternate solution to run the browser in headless mode is to use pyvirtual display.
First install xvfb and pyvirtual display:
sudo apt-get install xvfb xserver-xephyr
sudo pip install pyvirtualdisplay
The code will be:
from pyvirtualdisplay import Display
import webdriver
display = Display(visible=0, size=(800, 600))
display.start()
browser = webdriver.Chrome()

Related

Python selenium headless start with extensions and xfvb

I'm trying to run a headless Chrome process with an extension1. I'm using WSL.
For testing purposes I can use one of two browsers:
Ubuntu: Google Chrome 86.0.4240.183
Windows: Version 86.0.4240.183 (Official Build) (64-bit)
For production purposes the Ubuntu's Chrome is the only viable option2.
The rest of the stack:
Python 3.6.9
selenium: 3.141.0
In order to overcome Chrome not being able to start headlessly with extensions I use PyVirtualDisplay.
from selenium import webdriver
from pyvirtualdisplay import Display
display = Display(visible=0, size=(800, 600), backend='xvfb')
display.start()
options = ChromeOptions()
options.add_extension('/home/plonca/selenium_draft/extensions.crx')
options.add_argument('--no-sandbox')
windows_chrome_driver = '/home/plonca/python_virtual_environments/selenium_jupyter_venv/bin/chromedriver.exe'
ubuntu_chrome_driver = '/home/plonca/python_virtual_environments/selenium_jupyter_venv/bin/chromedriver'
chrome_driver = webdriver.Chrome(executable_path=ubuntu_chrome_driver
, options=options
)
The problem occurs in the last line: if the windows_chrome_driver is set as executable_path everything is fine. If I set ubuntu_chrome_driver instead, I get an error:
WebDriverException: Message: unknown error: failed to wait for extension background page to load: chrome-extension://bmlddehkgnjdalnfaecgflmmeknlbohi/_generated_background_page.html
from tab crashed
I wonder what the cause of the error might be. How to make chrome_driver work in the headless environment?
1 The extension is needed so that I can authenticate via a pop-up window. The extension has been created according to instructions found here. Other options such as sending username and password in the URL or automating the click with AutoIt doesn't work.
2 Please note that the Chrome seems to be the only option since using Firefox is a matter of an unresolved issue.

Selenium-Python with chromium browser (windows)

I am trying to launch chromium browser with selenium python on windows 8.
Added binary_location as chromium binary location which is appdata.
But still chromedriver starts google chrome instead of chromium.
If I uninstall google chrome, then chromedriver by default launches chromium. But with chrome installed it always launches chrome regardless.
Does anyone have an idea on how to start chromium with selenium while chrome installed?
Please do not mark it as duplicate. The other one was about unix and solution provided to selenium java while this one is about windows and python.
Try this:
from selenium.webdriver.chrome.options import Options
options = Options()
options.binary_location = r"D:\.....\chrome.exe"
# This line defines your Chromium exe file location.
driver = webdriver.Chrome(chrome_options=options)
driver.get('https://www.google.com/')
Worked for me.
I installed both Chrome and Chromium.
It starts the specified exe.
To start Chromium browser you can use the following code block :
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.binary_location("C:\\path\\to\\chrome.exe") //path to chromium binary
options.add_argument("start-maximized")
options.add_argument("--disable-gpu-vsync") //only for Windows
options.add_argument("--remote-debugging-port=9222") //for MAC & Linux
driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=options)
driver.get('http://google.com/')
Note : Here you can find more details about Run Chromium with flags

python linux selenium: chrome not reachable

I'm trying to run selenium on Ubuntu 16.10 Server, but I'm getting WebDriverException : Message : chrome not reachable (Driver info: chromedriver 2.9.248304, platform=Linux 4.8.0-22-generic x86_64)
from pyvirtualdisplay import Display
from selenium import webdriver
display = Display(visible=0, size=(800, 600))
display.start()
browser = webdriver.Chrome('usr/bin/chromedriver')
browser.get('http://www.google.com')
print(browser.title)
browser.quit()
display.stop()
Chrome is installed:
google-chrome --version
Google Chrome 57.0.2987.110
Adding some chrome options helped!
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
chrome = webdriver.Chrome('/usr/local/bin/chromedriver', chrome_options=chrome_options)
If you're using docker and getting this error I have the solution!
The cause of the problem is chrome running out of memory as documented here.
You have to add the flag "--shm-size=2g" to the docker run command.
It is not enough to install chrome. You should have chrome web driver installed.
You can refer this link for details on installation of chromedriver
How install chrome webdriver
Try down grade chrome version. Download google-chrome old versions here:
https://www.slimjet.com/chrome/google-chrome-old-version.php
I've verified the following working combination:
google-chrome linux-v52
chromedriver 2.20.353124
jm

Python-Selenium test script not working

I am trying to automation some action on a website using Python and Selenium, this is the sample code I am trying to run, from the Mozilla website for running the Firefox webdriver
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities.FIREFOX
caps["marionette"] = True
caps["binary"] = "/usr/bin/firefox-aurora"
driver = webdriver.Firefox(capabilities=caps)
driver.get("https://www.google.com")
driver.quit()
When running this code, the Firefox instance opens normally, and I can even use the instance as if I just opened Firefox normally, but the code 'stops' executing at the driver = webdriver.Firefox(capabilities=caps) line, I tried debugging the code with no luck, the whole execution seems to just stop at this line, and nothing after it is reached!
I am running Python3.5, Selenium version 2.53.6, I have the 'Wires' executable at the /usr/local/bin which is in the environment's PATH, I also have Firefox Aurora version 49.0a2 running on ArchLinux.
Thanks in advance.
[Update:]
I managed to get it to work after all using Firefox 46 (the normal version).

Running selenium (chromedriver) in a terminal window?

How can i run a python script using selenium (chromedriver) in an terminal window (without X forwarding).
The results/interactions with the website don't need to be shown. Par example filling a form scheduled by a cronjob.
you can use PyVirtualDisplay with Xvfb
from pyvirtualdisplay import Display
from selenium import webdriver
display = Display(visible=0, size=(800, 600))
display.start()
# now Chrome will run in a virtual display.
browser = webdriver.Chrome()
browser.get('http://www.google.com')

Categories

Resources