java中导入selenium

需要的环境:

java的开发环境这个是必须的,本次使用的myeclipse进行测试的。

chrome谷歌浏览器和对应的驱动chromedriver.exe

下载selenium的jar包 

新建一个java项目:

引入该jar包,新建一个java类来测试一下。

package selenium_demo;

import org.openqa.selenium.chrome.ChromeDriver;

public class selenium_case {
	public static void test(){
		//
		ChromeDriver dr=new ChromeDriver();
		dr.get("https://www.baidu.com");
	}

	public static void main(String[] args) {
		test();
	}
	

}

报错Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://chromedriver.storage.googleapis.com/index.html
    at com.google.common.base.Preconditions.checkState(Preconditions.java:197)
    at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:109)
    at org.openqa.selenium.chrome.ChromeDriverService.access$0(ChromeDriverService.java:1)
    at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:137)
    at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:296)
    at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:88)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:116)
    at selenium_demo.selenium_case.test(selenium_case.java:8)
    at selenium_demo.selenium_case.main(selenium_case.java:13)

需要指定一下谷歌驱动的路径:

package selenium_demo;

import java.io.File;

import org.openqa.selenium.chrome.ChromeDriver;

public class selenium_case {
	public static void test(){
		//指定驱动位置
		String filePath =  new File("").getAbsolutePath() + File.separator;
		System.setProperty("webdriver.chrome.driver", filePath + "recourse/chromedriver.exe");
		//
		ChromeDriver dr=new ChromeDriver();
		dr.get("https://www.baidu.com");
	}

	public static void main(String[] args) {
		test();
	}
	

}

能正常打开浏览器访问百度了。说明环境搭建成功!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值