Execute selenium scripts with headless chrome

Source: Internet
Author: User

? Article source:
Pinterest: Http://www.jianshu.com/p/b01de206a0d7

In the past we executed selenium scripts, and if we were to improve the execution speed of our scripts, we might consider using htmlunit or more headless browsers that use PHANTOMJS, but these tools are not good for JavaScript support. For the support of the web is not good, occupy more resources, and real browser there are some differences and so on.

Now, the Chrome browser offers headless chrome, which simply says we can execute our selenium script under chrome without opening the chrome GUI.

If you want to use headless chrome for the chrome version, we can see from the official documentation that the MAC and Linux environments require that the chrome version is 59+, and that the Windows version of Chrome is 60+, At the same time Chromedriver requires 2.30+ version

Assuming your environment is already in place, then I run the following Java demo example directly to feel the following:

@Test public
void Openchrometest () {
    String path = System.getproperty ("User.dir");
    System.setproperty ("Webdriver.chrome.driver", Path + "\\drivers\\chromedriver.exe");
        Chromeoptions chromeoptions = new Chromeoptions ();        set to headless mode (must)
        chromeoptions.addarguments ("--headless");        Set browser window open size  (not required)
        chromeoptions.addarguments ("--window-size=1920,1080");
        Webdriver Driver = new Chromedriver (chromeoptions);
        Driver.get ("http://www.baidu.com");
        String title = Driver.gettitle ();
        System.out.println (title);
        Driver.quit ();
    }

Script run we did not see any Chrome browser launch, but the console also output exactly:

If you want to learn more about headless chrome, you can view the official website document: Https://developers.google.cn/web/updates/2017/04/headless-chrome

Finally, from the official Firefox, Firefox will also launch headless mode, is expected in the Linux environment 55 release, and the Firefox 56 version on the full platform launch. Worth looking forward to.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.