首先创建Maven项目
在pom.xml里面加上,这个是selenium目前最新的Maven依赖包
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.0.0-alpha-5</version>
</dependency>
其次下载3个浏览器需要的selenium的驱动程序
我这里已经下载了目前最新的驱动,前提所有的浏览器版本是最新的;需要的可以点击这里下载3种都有。
链接:https://pan.baidu.com/s/18M1Fya1ZJbEpy0SskI9nqQ 提取码:0naa
如果不想下载,有第二种方法,前提浏览器要有这个selenium的插件(仅限谷歌和火狐,IE没有插件),而且谷歌还必须要有账号才能打开谷歌网上应用店,如下图所示:
一、Chrome的实现代码
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Chrome_Browser {
@Test
public void f() throws InterruptedException {
//写代码操作浏览器,模拟人工测试:api
// System.setProperty("webdriver.chrome.driver", "src/main/resources/chromedriver.exe");
// 1、 创建一个Chrome驱动
WebDriver driver = new ChromeDriver();
// 2、输入百度网站进行访问
driver.get("http://www.baidu.com");
// 3、找到百度输入框(元素定位,定位一个元素)
// 根据id去找 kw
WebElement input = driver.findElement(By.id("kw"));
// 说明确实找到这个输入框
System.out.println(input.getAttribute("maxlength"));
// 4、往输入框输入关键字
input.sendKeys("CSDN");
// 5、点击百度一下
WebElement searchButton = driver.findElement(By.id("su"));
searchButton.click();
//绝对路径
// driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div/form/span[2]/input")).click();
//相对路径
driver.findElement(By.xpath("//*[@id=\"su\"]")).click();
// 获得输入框的value值
String actualkeywords = driver.findElement(By.id("kw")).getAttribute("value");
System.out.println("输入框的值:" + actualkeywords);
// 断言: 验证结果符合我的预期,符合就不打印;不符合,就把不同结果打印出来
Assert.assertEquals("CSDN", actualkeywords);
// 等待时间 3秒
Thread.sleep(3000);
//退出浏览器
driver.quit();
}
}
二、FireFox的实现代码
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FireFox_Browser {
@Test
public void f() throws InterruptedException {
//写代码操作浏览器,模拟人工测试:api
// System.setProperty("webdriver.firefox.driver", "src/main/resources/geckodriver.exe");
// 1、firefox驱动
WebDriver driver = new FirefoxDriver();
// 2、输入百度网站进行访问
driver.get("http://www.baidu.com");
// 3、找到百度输入框(元素定位,定位一个元素)
// 根据id去找 kw
WebElement input = driver.findElement(By.id("kw"));
// 说明确实找到这个输入框
System.out.println(input.getAttribute("maxlength"));
// 4、往输入框输入关键字
input.sendKeys("CSDN");
// 5、点击百度一下
WebElement searchButton = driver.findElement(By.id("su"));
searchButton.click();
//绝对路径
// driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div/form/span[2]/input")).click();
//相对路径
driver.findElement(By.xpath("//*[@id=\"su\"]")).click();
// 获得输入框的value值
String actualkeywords = driver.findElement(By.id("kw")).getAttribute("value");
System.out.println("输入框的值:" + actualkeywords);
//// 验证结果符合我的预期,符合就不打印;不符合,就把不同结果打印出来
Assert.assertEquals("CSDN", actualkeywords);
// 等待时间 3秒
Thread.sleep(3000);
//退出浏览器
driver.quit();
}
}
三、IE浏览器实现代码
这个比较麻烦,所以有点特殊,会有安全提醒,最好把所有的杀毒软件关闭,不然可能会提示拦截信息。
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
/**
* @author ZF
* @Description TODO
* @Date 2020/4/9 20:29
*/
public class IE_Browser {
@Test
public void test() throws InterruptedException {
// 1、驱动文件找不到的异常
System.setProperty("webdriver.ie.driver", "src/main/resources/IEDriverServer.exe");
// 创建一个对象,用来设置创建ie驱动时的各种设置
DesiredCapabilities capabilities = new DesiredCapabilities();
// 2、取消IE安全设置(忽略IE的Protected Mode的设置)
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
// 3、忽略浏览器的页面缩放设置
capabilities.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);
// 4、设置一个初始化页面,防止window对象丢失
capabilities.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL, true);
// IE驱动
WebDriver driver = new InternetExplorerDriver(capabilities);
// 2、输入百度网站进行访问
driver.get("http://www.baidu.com");
// 等待时间 3秒
Thread.sleep(3000);
driver.quit();
}
}
总结:
这些代码我没少趟坑,特别是IE,很多博客说需要在安全设置把那个启用保护模式勾选去掉,其实根本没必要去掉,经验:所有的设置都没必要,只需要把360杀毒软件关闭就行了,防止提示恶意软件。下图就是勾选了也能打开,还有所有浏览器让设置什么环境变量的,也都不需要,只需要按照我这个全网最简单的博客,就都可以用的包括很多代码python也都适用的。下图就是证明那些博客不用设置的最好证据。