How to launch chrome browser from java

前端 未结 2 1297
盖世英雄少女心
盖世英雄少女心 2021-01-20 01:25

Is there any smart way to launch the chrome browser from a java class? I\'m asking because I would like a smart way to launch an application that required a chrome browser o

相关标签:
2条回答
  • 2021-01-20 02:01

    You can try Selenium Here:

    import org.openqa.selenium.chrome.ChromeDriver;
    public class App
    {
        public static void main(String[] args) throws Throwable
        {
            ChromeDriver driver = new ChromeDriver();
    
            System.setProperty("webdriver.chrome.driver", "/usr/bin/google-chrome");
    
            // And now use this to visit Google
            driver.get("http://www.google.com");
    }
    

    }

    Add Maven Dependency:

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.42.2</version>
        </dependency>
    
    0 讨论(0)
  • 2021-01-20 02:07

    You can execute chrome.exe like this:

    try {
        Process p = Runtime.getRuntime().exec("\"/Program Files (x86)/Google/Chrome/Application/chrome.exe\"");
        p.waitFor();
        System.out.println("Google Chrome launched!");
    } catch (Exception e) {
        e.printStackTrace();
    }
    

    Provided you know where Chrome is installed.

    0 讨论(0)
提交回复
热议问题