简体   繁体   中英

Selenium: Headless mode

I just learned the selenium module and the video i watched is about 1 years ago. The example is

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = webdriver.ChromeOptions()
chrome_options.headless=True

path='/usr/bin/google-chrome'
chrome_options.binary_location = path

driver = webdriver.Chrome(executable_path=chrome_options.binary_location,options=chrome_options)

url="https://www.jd.com/?country=USA"
driver.get(url)
driver.save_screenshot("jingdong.png")

As a reuslt, errors occur.

Unexpected exception formatting exception. Falling back to standard exception...

After searching for multiple documents, i discover that the arguments have changed a little bit. Therefor i would like to share my findings. And make some explanation on this post for newbies like me.

from selenium import webdriver

# this is for finding elements, by_id/class/tag...
from selenium.webdriver.common.by import By

#this is for adding the chromedriver options such as headless, gpu-enable
from selenium.webdriver.chrome.options import Options


#Add options, the headless should be used in this way instead of "add_argument"
chrome_options = Options()
chrome_options.headless=True 
# setting it = true, enable headless mode

# this is for the driver path
executable= "/home/fish/Documents/WebScraping/driver/chromedriver_ubuntu"

# split the options and executable_path!!!
driver = webdriver.Chrome(executable_path=executable, options=chrome_options)


#Here is just my random trial for examining if the headless work
url="https://https://www.google.com.**/"
driver.get(url)
driver.save_screenshot("google.png")

As a result, selenium headless mode works for me.
My system: ubuntu22.04
Software: Jupyter notebook(ANaconda)
Date: 2022 08 31(in case later the selenium has other changes on its arguments)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM