Chrome Options in Python Selenium : Disable GPU vs Headless
You saw it right. Adding the argument --headless
initiates the Chrome Browsing Context in headless mode.
However the purpose of the argument --disable-gpu
was to enable google-chrome-headless on windows platform. It was needed as SwiftShader fails an assert on Windows in headless mode earlier.
This issue was resolved through Headless: make --disable-gpu flag unnecessary
You can find a relevant detailed discussion in ERROR:gpu_process_transport_factory.cc(1007)-Lost UI shared context : while initializing Chrome browser through ChromeDriver in Headless mode
AWS EC2 times out when trying to authenticate using a selenium headless chrome
A bit of more details about your usecase would have helped us to analyze the reason behind the program executing too slow. Here are a few considerations:
- A common cause for Chrome to crash during startup is running Chrome as
root
user (administrator
) on Linux. While it is possible to work around this issue by passing --no-sandbox flag when creating your WebDriver session, such a configuration is unsupported and highly discouraged. You need to configure your environment to run Chrome as a regular user instead. So you may need to drop the--no-sandbox
option.
Here is the link to the Sandbox story.
- While using
--headless
option you won't be able to use--window-size=1280x1696
due to certain constraints.
You can find a couple of relevant detailed discussion in:
- Fullscreen in Headless Chrome using Selenium
- Not able to maximize Chrome Window in headless mode
- The argument
--disable-gpu
was to enable google-chrome-headless on windows platform. It was needed as SwiftShader fails an assert on Windows in headless mode earlier. This issue was resolved through Headless: make --disable-gpu flag unnecessary
You can find a relevant detailed discussion in ERROR:gpu_process_transport_factory.cc(1007)-Lost UI shared context : while initializing Chrome browser through ChromeDriver in Headless mode
- Further you haven't mentioned any specific requirement of using
--hide-scrollbars
,--enable-logging
,--log-level=0
,--v=99
,--single-process
,--data-path=tmp/data-path
,--homedir=tmp
,--disk-cache-dir=tmp/cache-dir
,--no-proxy-server
,--proxy-server='direct://'
and--proxy-bypass-list=*
arguments which you opt to drop for the time being and add them back as per your Test Specification.
References
You can find a couple of relevant detailed discussions in:
- Selenium works on AWS EC2 but not on AWS Lambda
Selenium works on AWS EC2 but not on AWS Lambda
I was finally able to get it to work
Python 3.7
selenium==3.14.0
headless-chromium v1.0.0-55
chromedriver 2.43
Headless-Chromium
https://github.com/adieuadieu/serverless-chrome/releases/download/v1.0.0-55/stable-headless-chromium-amazonlinux-2017-03.zip
Chromedriver
https://chromedriver.storage.googleapis.com/2.43/chromedriver_linux64.zip
I added headless-chromium and chromedriver to a Lambda Layer
Permissions 755
for both works
Lambda
The Lambda function looks like this
import os
import selenium
from selenium import webdriver
def handler(event, context):
print(os.listdir('/opt'))
#
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--single-process')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.binary_location = f"/opt/headless-chromium"
driver = webdriver.Chrome(
executable_path = f"/opt/chromedriver",
chrome_options=chrome_options
)
driver.get("https://www.google.com/")
html = driver.page_source
driver.close()
driver.quit()
print(html)
Hope this helps someone in Q4 2020 and after.
Related Topics
Pandas Selecting by Label Sometimes Return Series, Sometimes Returns Dataframe
Tab Completion in Python's Raw_Input()
How to Trace the Path in a Breadth-First Search
MySQL "Incorrect String Value" Error When Save Unicode String in Django
Creating Lowpass Filter in Scipy - Understanding Methods and Units
How to Implement Band-Pass Butterworth Filter with Scipy.Signal.Butter
Python Matplotlib Framework Under MACosx
Differencebetween a Pandas Series and a Single-Column Dataframe
How to Remove Leading and Trailing Zeros in a String? Python
What's a Good Equivalent to Subprocess.Check_Call That Returns the Contents of Stdout
Pandas Read CSV File with Float Values Results in Weird Rounding and Decimal Digits
How to Get Ftp File's Modify Time Using Python Ftplib
Python Requests.Get Always Get 404
Accessing the List While Being Sorted