大家好,又见面了,我是全栈君。
在pom文件中添加selenium依赖
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.4.0</version>
</dependency>
本文使用Firefox和Chrome两种最常用的浏览器
代码执行的效果是打开浏览器,进入百度,自动输入hello Selenium然后搜索
(1)使用Firefox
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class HelloTest1 {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "D:\\1Program\\Selenium_Driver\\geckodriver.exe");
System.setProperty("webdriver.firefox.bin", "E:\\Mozilla Firefox\\firefox.exe");
WebDriver dr = new FirefoxDriver();
dr.get("http://www.baidu.com");
dr.findElement(By.id("kw")).sendKeys("hello Selenium");
dr.findElement(By.id("su")).click();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
本文只介绍前几行,剩余部分在后续文章介绍
System.setProperty("webdriver.gecko.driver", "D:\\1Program\\Selenium_Driver\\geckodriver.exe");
System.setProperty("webdriver.firefox.bin", "E:\\Mozilla Firefox\\firefox.exe");
WebDriver dr = new FirefoxDriver();
使用FirefoxDriver时,程序会去默认路径寻找firefox浏览器,大部分人可能都不是默认的,所以需要通过webdriver.firefox.bin设置系统变量,这时候直接运行会报如下错误
Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases
at com.google.common.base.Preconditions.checkState(Preconditions.java:738)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:124)
at org.openqa.selenium.firefox.GeckoDriverService.access$100(GeckoDriverService.java:41)
at org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:115)
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:330)
at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:207)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:108)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:104)
at com.mysite.selenium.HelloTest1.main(HelloTest1.java:11)
于是去下载geckodriver,然后设置路径到webdriver.gecko.driver即可(PS:看网上的说法,似乎设置到环境变量也可以,各位可以试一下)
(2)使用Chrome
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class HelloTest {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "D:\\1Program\\Chrome_Driver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.baidu.com");
driver.findElement(By.id("kw")).sendKeys("hello Selenium");
driver.findElement(By.id("su")).click();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//driver.quit();
}
}
Chrome浏览器需要单独下载driver,地址在这里(需要梯子),然后设置系统变量webdriver.chrome.driver即可
转载于:https://my.oschina.net/icebergxty/blog/913825
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/108444.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...