How do I use Selenium Webdriver on headless Chrome?

后端 未结 3 1050
滥情空心
滥情空心 2021-02-20 15:51

I\'m learning to use Selenium for basic things like taking a screenshot, scraping, and testing and would like to use it with headless Chrome, which is now stable as of Chrome 59

3条回答
  •  礼貌的吻别
    2021-02-20 16:23

    Managed to work through this in the end via various docs, blog posts and gists.

    caps = Selenium::WebDriver::Remote::Capabilities.chrome("desiredCapabilities" => {"takesScreenshot" => true}, "chromeOptions" => {"binary" => "/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary"})
    
    driver = Selenium::WebDriver.for :chrome, desired_capabilities: caps, switches: %w[--headless --no-sandbox --disable-gpu --remote-debugin-port=9222 --screen-size=1200x800]
    

    You need to use a very recent version of Chrome (I'm using Canary) and tell Selenium the path to the binary. You also need to set the desired capabilities for 'takesScreenshot' to true.

提交回复
热议问题