web ⾃动化测试的操作核⼼是能够找到⻚⾯对应的元素,然后才能对元素进⾏具体的操作。常⻅的元素定位⽅式⾮常多,如 id,classname,tagname,xpath,cssSelector 常⽤的主要由 cssSelector 和 xpath
选择器的功能:选中页面中指定的标签元素
选择器的种类分为基础选择器和复合选择器,常见的元素定位方式可以通过 id 选择器和子类选择器来进行定位

XML 路径语⾔,不仅可以在 XML ⽂件中查找信息,还可以在 HTML 中选取节点。
xpath 使⽤路径表达式来选择 xml ⽂档中的节点
xpath 语法中:
//*//[指定节点] //ul:获取 HTML 页面所有的 ul 节点//input:获取 HTML 页面所有的 input 节点/ //span/input.. //input/..:获取 input 节点的父节点[@...] //[@id='kw]:匹配 HTML 页面中的 id 属性为 kw 的节点xpath 的索引是从 1 开始的//div/ul/li[3],定位到第三个百度热搜标签更便捷的⽣成 selector/xpath 的⽅式:右键选择复制"Copy selector/xpath"
注意:登录状态下和非登录状态下,自动化打开的页面不一定相同。做自动化测试一定要注意页面状态的一致性
findElement,在页面查找元素,返回值 WebElement
查找页面元素driver.findElement(By.cssSelector("#s-hotsearch-wrapper > div > a.hot-title > div > i:nth-child(1)")).click();
driver.findElement((By.xpath("//*[@id=\"s-hotsearch-wrapper\"]/div/a[1]/div/i[1]"))).click();findElements,在页面查找元素,返回值为 List<WebElement>List<WebElement> elements = driver.findElements(By.cssSelector("#hotsearch-content-wrapper > li > a > span.title-content-title"));
for(WebElement str : elements){
System.out.println(str.getText());
}
//找到百度一下并点击
driver.finElement(By.cssSelector("#su")).click();页面中,除了按钮可以点之外,其他绝大多数元素也能点击

这个方法可以在任意能输入文本的地方进行输入
driver.findElement(By.cssSelector("#kw")).sendKeys("今天天气");
Thread.sleep(3000);
driver.quit();![[屏幕录制 2024-11-16 165917.mp4]]
输入文本后,又想换一个新的关键词,这里就需要用到 clear()
若想在一个场景下更换多个关键词,需要将前一个关键词清楚掉;若不清除,每次 sendKeys 将完成拼接的操作
driver.findElement(By.cssSelector("#kw")).sendKeys("我爱游戏");
driver.findElement(By.cssSelector("#kw")).clear();
driver.findElement(By.cssSelector("#kw")).sendKeys("我爱学习");WebElement bdtext = driver.findElement(By.cssSelector("#hotsearch-content-wrapper > li:nth-child(3) > a > span.title-content-title"));
System.out.println(bdtext.getText()); 问题:是否可以通过
getText()获取到“百度⼀下按钮”上的⽂字“百度⼀下”呢?
//获取百度一下按钮上的文本
String text = driver.findElement(By.cssSelector("#su")).getText();
System.out.println("百度一下上的文字为:"+text);

//获取百度一下按钮上的文本
String text = driver.findElement(By.cssSelector("#su")).getAttribute("value");
System.out.println("百度一下上的文字为:"+text);
String title = driver.getTitle();
String url = driver.getCurrentUrl();
System.out.println(title);
System.out.println(url);
//设置窗口大小
//窗口最小化
driver.manage().window().minimize();
Thread.sleep(2000);
//窗口最大化
driver.manage().window().maximize();
Thread.sleep(2000);
//窗口全屏
driver.manage().window().fullscreen();
Thread.sleep(2000);
//自定义尺寸
driver.manage().window().setSize(new Dimension(800,400));
Thread.sleep(2000);当创建一个驱动之后,驱动对象就会指向打开的那个页面的句柄
driver 指向别的标签页,就需要使其指向改变driver.getWindowHandle()driver.getWindowHandles()driver.findElement(By.cssSelector("#s-hotsearch-wrapper > div > a.hot-title > div")).click();
String curHandle = driver.getWindowHandle();
Set<String> allHandles = driver.getWindowHandles();
for(String handle : allHandles){
if(handle != curHandle){
//切换 driver 的句柄指向
driver.switchTo().window(handle);
}
}driver 获取所有页面的句柄,但指向只有一个注意:执行了
driver.close()之前需要切换到未被关闭的窗口
driver.get(URL) 进行跳转//关闭当前标签页
driver.close();
//关闭浏览器,释放driver对象
driver.quit();close() 用到的场景比较少,只会在以下场景下去使用
假如写自动化代码出现了 NoSuchElement 错误
Thread.sleep(秒),设置的时间长一点以阻塞线程的方式,达到等待的效果
Thread.sleep(秒);100 条测试用例,每个用例添加强制等待平均时间 3 s
1000 * 3 = 3000s = 50min2 - 3min隐式等待是一种智能等待,他可以规定在查找元素时,在指定时间内不断查找元素。如果找到则代码继续执行,知道超时没找到元素才会报错。
你的女朋友转校了,让你等她三年。但第二年的时候,她回来了
implicitlyWait() 参数:Duration 类中提供的毫秒、秒、分钟等方法//隐式等待3秒
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
//隐式等待3000毫秒
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(3000));3s 内找到元素,继续执行3s 内没有找到元素,报错 NoSuchElement隐式等待作⽤域是整个脚本的所有元素。即只要 driver 对象没有被释放掉(driver.quit()),隐式等待就⼀直⽣效。
显式等待也是一种智能等待,在指定超时时间范围内只要满足操作的条件就会继续执行后续代码
new WebDriverWait(driver, Duration.ofSeconds(3).until($express))$press:涉及到 selenium.support.ui.ExpectedCondition 包下的 ExprxtedCondition 类booleanWebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(3));
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#su")));
//先保证元素存在且可以点击后
driver.findElement(By.cssSelector("#su")).click();ExpectedConditions 预定义的一些示例:
elementToBeClickable(By location):用于检查元素的期望是可见的并已启用,以便可以单击它textToBe(By locator, String str):检查元素(精确匹配)presenceOfElementLocated(By locator):检查页面的 DOM 上是否存在元素urlToBe(java.lang.String url):检查当前页面的 URL 是一个特定的 URL//更长的方法
driver.navigate().to("https://www.bytedance.com");
//简单的方法
driver.get("https://www.bytedance.com");//后退
driver.navigate().back();
//前进
driver.navigate().forward();
//刷新
driver.navigate().refresh();