java 如何从java启动chrome浏览器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25320890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:50:23  来源:igfitidea点击:

How to launch chrome browser from java

javagoogle-chrome

提问by Richie

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 on a machine that has internet explorer as a default browser and java 1.4.2 installed.

有没有什么聪明的方法可以从 java 类启动 chrome 浏览器?我之所以这么问,是因为我想要一种智能方式来启动需要 Chrome 浏览器的应用程序,该应用程序将 Internet Explorer 作为默认浏览器并安装了 java 1.4.2。

thanks

谢谢

回答by bdunn

You can execute chrome.exelike this:

你可以这样执行chrome.exe

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.

前提是您知道 Chrome 的安装位置。

回答by sendon1982

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:

添加Maven依赖:

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.42.2</version>
    </dependency>