Java实现打开google浏览器

说明:

  博主的Google浏览器版本:75.0.3770.142,如果运行异常,需要自行查找对应版本的驱动(chromedriver.exe)

 

需要的jar包:

https://pan.baidu.com/s/1KeBhp--zc6BZdoUsrGSkbQ

 

需要的驱动文件(放在Google浏览器.exe文件安装的相同目录):

https://pan.baidu.com/s/1xtznVCXM4KJmd6Xxms2EXw

 

代码:

package selenuimTest;

import java.io.File;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

/**
 * 测试----- 通过窗口打开谷歌浏览器
 * @author WangSong
 *
 */
public class TestOpenBrowser {

    public static void main(String[] args) {
        //设置启动google浏览器的驱动和位置
        System.setProperty("webdriver.chrome.driver",
                "C:"+File.separator+
                "Program Files (x86)"+File.separator+
                "Google"+File.separator+
                "Chrome"+File.separator+
                "Application"+File.separator+
                "chromedriver.exe");
        System.out.println("启动 ...");
        //获取谷歌浏览器驱动
        WebDriver driver=new ChromeDriver();
        //设置默认打开的页面
        driver.get("http://www.baidu.com/");
        //设置默认搜索的关键字
        driver.findElement(By.id("kw")).sendKeys("eclipse窗口集成浏览器");
        //搜索按钮点击
        driver.findElement(By.id("su")).click();
        //关闭浏览器
//        driver.close();
    }
}

 

posted @ 2019-08-13 11:20  SoyWang  阅读(4295)  评论(0编辑  收藏  举报