Creating a headless Chrome instance in Python

后端 未结 5 600
陌清茗
陌清茗 2020-12-23 18:09

This question describes my conclusion after researching available options for creating a headless Chrome instance in Python and asks for confirmation or resources that descr

5条回答
  •  礼貌的吻别
    2020-12-23 18:56

    This question is 5 years old now and at the time it was a big challenge to run a headless chrome using python, but the good news is:

    Starting from version 59, released in June 2017, Chrome comes with a headless driver, meaning we can use it in a non-graphical server environment and run tests without having pages visually rendered etc which saves a lot of time and memory for testing or scraping. Setting Selenium for that is very easy:

    (I assume that you have installed selenium and chrome driver):

    from selenium import webdriver
    
    #set a headless browser
    options = webdriver.ChromeOptions()
    options.add_argument('headless')
    browser = webdriver.Chrome(chrome_options=options)
    

    and now your chrome will run headlessly, if you take out options from the last line, it will show you the browser.

提交回复
热议问题