chromedriver in headless mode - python

I am using chromedriver for web scraping by giving the binary path.
driver = webdriver.Chrome(r"C:\Program Files\JetBrains\PyCharm Community Edition 2017.3.3\bin\chromedriver.exe")
driver.get("https://www.example.com/")
This invoke chromedriver in GUI mode. How can I start chrome in headless mode?

Chrome headless is ideally much better than PhantomJS, whose owner decided to stop maintaining the project because the arrival of Chrome headless made it a bit less necessary. That being said, if you have a Chrome version which supports headless, you can do this:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('headless')
driver = webdriver.Chrome(chrome_options=options)
As you see, headless is an argument, so if for some reason you want to run the same code but you need to see the GUI, remove that argument.
By the way, if you ever wanted to give the binary location, a nice way to do it is also with options:
options.binary_location = 'path to your chrome binary'
But if your installed version is recent enough, there should be no reason to do so.

Related

How can I use Firefox webdriver in pythonanywhere.com

I can use Chromedriver easyly in pythonanywhere.com but how can I use Firefox driver in pythonanywhere's python script ?
this is how I use chromedriver
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
browser = webdriver.Chrome(options=chrome_options)
browser.get("https://www.youtube.com/")
print("Done")
browser.quit()
PythonAnywhere dev here: unfortunately Firefox doesn't work well enough to be fully supported on PythonAnywhere now -- it's too resource-intensive. Chrome is more lightweight, so it's the only Selenium option we support.
(Interestingly, it used to be the other way around -- Firefox was more lightweight, and was the one that we supported. But the browsers have changed over time, so we have to change with them.)

Chrome Issues with Python Selenium on Windows 10

Apologies if this has been answered elsewhere (I'm sure it will have been) but I've spent the past few days searching high and low, trying all the solutions I've come across.
I've been trying to automate some form completion on Chrome using Selenium and Python.
My solution works fine on a personal device but on my company machine I've been hitting some issues.
Running my test code, originally produced the "DevToolsActivePort file doesn't exist" error but I managed to get around that.
The script now opens a Chrome window but then produces an error:
selenium.common.exceptions.WebDriverException: Message: unknown error:
Chrome failed to start: crashed. (chrome not reachable) (The
process started from chrome location C:\Program
Files\Google\Chrome\Application\chrome.exe is no longer running, so
ChromeDriver is assuming that Chrome has crashed.)
This is whilst the Chrome window does open and remains open.
Unfortunately with this being a work machine I am unable to reinstall windows or Python at this time.
I've checked the versions and everything appears to line up.
Python: 3.7.0
Selenium: 3.141.0
Chrome: 91.0.4472.114
ChromeDriver: 91.0.4472.101 (also tried other versions)
Code Used:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
options.add_argument("--disable-setuid-sandbox")
options.add_argument("--remote-debugging-port=9222")
options.binary_location = "C:\Program Files\Google\Chrome\Application\chrome.exe"
options.add_argument("--disable-dev-shm-using")
options.add_argument("--disable-extensions")
options.add_experimental_option("useAutomationExtension", True) #also tried False
options.add_argument("--disable-gpu")
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("user-data-dir=C:\\Users\\UP1170\\AppData\\Local\\Google\\Chrome\\User Data")
options.add_experimental_option("detach",True)
driver = webdriver.Chrome(chrome_options=options,executable_path="C:\\Users\\UP1170\\Desktop\\chromedriver.exe")
driver.get("https://www.google.com")
This is not the full code I've developed for the automation but a test script to just try and get Chrome to open on a company windows 10 laptop.
Anyone have any ideas?
I've got a feeling it's going to be linked to a company restriction placed on my device as a similar issue occurs when I've attempted this with both Firefox and Edge.

Python, PhantomJS says I am not using headless?

my code is:
from selenium import webdriver
driver = webdriver.PhantomJS(executable_path='driver/bin/phantomjs.exe')
driver.get("https://www.test.com")
print(driver.current_url)
It seems to run fine but before it runs I always get this error:
UserWarning: Selenium support for PhantomJS has been deprecated, please use headless versions of Chrome or Firefox instead warnings.warn('Selenium support for PhantomJS has been deprecated, please use headless
Why am I getting this error? I thought my PhantomJS was headless as it still works and no browser pops-up is this error save to ignore?
Selenium considers PhantomJS as deprecated, so you need to us either Chrome or Firefox in headless mode.
Here are the steps to use Chrome in headless mode:
download chrome driver from https://sites.google.com/a/chromium.org/chromedriver/getting-started
extract it to a folder
add this folder to your PATH environment variable (if you don't do it, you will have to use webdriver.Chrome('/your/path/to/chromedriver') in the code below instead of webdriver.Chrome())
Then use it like this:
from selenium import webdriver
# prepare the option for the chrome driver
options = webdriver.ChromeOptions()
options.add_argument('headless')
# start chrome browser
browser = webdriver.Chrome(chrome_options=options)
browser.get('http://www.google.com/xhtml')
print(browser.current_url)
browser.quit()
More on how to use ChromeDriver
For the other options: here (also here and here)
In Selenium 3.8.1 PhantomJS marked as deprecated webdriver and recommend us to use either Chrome or Firefox in headless mode.
You could use this:
from selenium import webdriver
browser = webdriver.Chrome('chromedriver_path/chromedriver')
browser.get("https://www.test.com")
print(browser.current_url)
browser.quit()
Found an alternative you can add options.add_argument('headless') to chrome

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

Selenium Chromedriver disable log on Windows

I am running selenium via python 3 with the chromedriver browser on windows 10.
The program is very active and should run constantly. Means: lots of logging going on.
Problem: the crhomedriver logs get way too long after a few hours, and windows tends to crash.
Everything is working fine, the logs are just minor issues about which I do not really care.
Question:
How may I disable the chromedriver logs?
Notes:
I did my research and did not find any working solution yet.
I am forced to use chromedriver, hence no headless browser like phantomJS is a valid alternative.
Try this, not sure it will work. Pass the options to your chromedriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('--disable-logging')

Categories

Resources