Is it possible to run Google Chrome in headless mode with extensions?

后端 未结 5 983
甜味超标
甜味超标 2020-11-28 06:45

I cannot use my currently installed extensions in Google Chrome using headless mode. Is there any way to enable them?

An easy way to check if the extensions work is

5条回答
  •  心在旅途
    2020-11-28 07:24

    You can use pyvirtualdisplay to run the chrome with zero display on your server. The best thing is you can run extensions by using this trick.

    Here is my implementation:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    import time
    from pyvirtualdisplay import Display
    
    display = Display(visible=0, size=(800, 600))
    display.start()
    
    chrome_options = Options()
    
    chrome_options.add_extension("proxy.zip")
    
    driver = webdriver.Chrome(executable_path='/usr/bin/chromedriver', chrome_options=chrome_options)
    time.sleep(3)
    driver.get("https://ipinfo.io/json")
    print(driver.page_source)
    driver.close()
    
    display.stop()
    

    You need to install xvfb on your server/machine:

    sudo apt install -y xvfb
    pip install pyvirtualdisplay
    

    Running it on my AWS Server

提交回复
热议问题