How to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium?
So after correcting my code to:
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches",["ignore-certificate-errors"])
options.add_argument('--disable-gpu')
options.add_argument('--headless')
chrome_driver_path = "C:\Python27\Scripts\chromedriver.exe"
The .exe file still came up when running the script. Although this did get rid of some extra output telling me "Failed to launch GPU process".
What ended up working is running my Python script using a .bat file
So basically,
- Save python script if a folder
Open text editor, and dump the following code (edit to your script of course)
c:\python27\python.exe c:\SampleFolder\ThisIsMyScript.py %*
Save the .txt file and change the extension to .bat
- Double click this to run the file
So this just opened the script in Command Prompt and ChromeDriver seems to be operating within this window without popping out to the front of my screen and thus solving the problem.
How To Configure Chrome Browser To Run In Headless Mode
I'd recommend not using mouse click for the submit button but use the keyboard instead:
from selenium.webdriver.common.keys import Keys
consent.send_keys(Keys.ENTER)
Running Selenium with Headless Chrome Webdriver
To run chrome-headless just add --headless
via chrome_options.add_argument
, i.e.:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
#chrome_options.add_argument("--disable-extensions")
#chrome_options.add_argument("--disable-gpu")
#chrome_options.add_argument("--no-sandbox") # linux only
chrome_options.add_argument("--headless")
# chrome_options.headless = True # also works
driver = webdriver.Chrome(options=chrome_options)
start_url = "https://duckgo.com"
driver.get(start_url)
print(driver.page_source.encode("utf-8"))
# b'<!DOCTYPE html><html xmlns="http://www....
driver.quit()
So my thought is that running it with headless chrome would make my
script faster.
Try using chrome options like --disable-extensions
or --disable-gpu
and benchmark it, but I wouldn't count with much improvement.
References: headless-chrome
Printing a PDF with Selenium Chrome Driver in headless mode
For anyone else coming across this with a similar issue, I fixed it by using the print method described here: Selenium print PDF in A4 format
Using my example from above, I replaced:
driver.execute_script("window.print();")
with:
pdf_data = driver.execute_cdp_cmd("Page.printToPDF", print_settings)
with open('Google.pdf', 'wb') as file:
file.write(base64.b64decode(pdf_data['data']))
And that worked just fine for me.
Related Topics
Using Pip to Install Packages to Anaconda Environment
What Do Ellipsis [...] Mean in a List
Pythonic Way to Print List Items
How to Convert a String with Dot and Comma into a Float in Python
Removing Elements That Have Consecutive Duplicates
How to Get the Caller's Method Name in the Called Method
How to Process Sigterm Signal Gracefully
Matplotlib (Pyplot) Savefig Outputs Blank Image
How to Embed Matplotlib in Pyqt - for Dummies
Why Is Button Parameter "Command" Executed When Declared
Dump a Numpy Array into a CSV File
Working with Utf-8 Encoding in Python Source
How to Create a Trie in Python
Remove Duplicates by Columns A, Keeping the Row with the Highest Value in Column B