要想完成自动化测试,首先就需要知道在哪输入或点击,一个网页上很多元素,就需要定位一个元素。
如何定位页面中的一个元素,以百度首页为例:
先打开开发者工具:右键检查,或者 F12 都可以
点击箭头,再点击输入框,即可确定输入框的位置(class=“s_ipt”)
运行代码,就会在百度首页找到输入框并输入 selenium
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class Main {
public static void main(String[] args) {
ChromeOptions options = new ChromeOptions();
// 允许所有请求
options.addArguments("--remote-allow-origins=*");
WebDriver webDriver = new ChromeDriver(options);
// 打开百度首页
webDriver.get("https://www.baidu.com");
// 通过选择器找到输入框
WebElement element = webDriver.findElement(By.cssSelector(".s_ipt"));
//输入selenium
element.sendKeys("selenium");
}
}
定位元素
大多数元素可以使用上面这个方法,如果不行,在使用下面这些。
下面介绍几个 常用的 定位元素的方法(唯一元素):
class 为 s_ipt 的元素,By.cssSelector(“.s_ipt”)
id 为 kw 的元素, By.cssSelector(“#kw”)
这两种方式都能找到百度输入框
使用 xpath:绝对路径和相对路径。
绝对路径(不常用):/html/body/div/div/…/span/input
相对路径: findElement(By.xpath(“//form/span/input”)) 当前页面只有一个 form。
相对路径 + 索引: findElement(By.xpath(“//form/span[1]/input”)) form 下的第一个span标签。
相对路径 + 属性: findElement(By.xpath(“//input[@autocomplete=‘off’]”)) input 标签且属性 autocomplete 为 off。
当然,属性可以有多个: findElement(By.xpath(“//input[@autocomplete=‘off’ and @name=‘wd’]”))
and 也可以换成 or
使用通配符:findElement(By.xpath(“//*[@autocomplete=‘off’ and @name=‘wd’]”)); 元素和属性名可以为* ,但属性值不能
属性以 o 开头:findElement(By.xpath(“//input[starts-with(@autocomplete,‘o’)]”))
属性以 ff 结尾:findElement(By.xpath(“//input[substring(@autocomplete,2)=‘ff’]”))
substring 从第二个字母开始截取,截取到末尾。
看了相关资料,说 ends-with 是2.0版本,而浏览器只支持 1.0 版本。
属性包含 f:findElement(By.xpath(“//input[contains(@autocomplete,‘f’)]”))
findElement(By.linkText(“贴吧”));
linkText 定位文本链接 <a>贴吧<\a>。
定位框架内元素
像当前这个页面如果使用上面方法,就不能定位到这个元素了。
此时我们就需要换个方法了
操作对象
上面是定位元素的常用方法,定位到元素后,就需要对这个元素进行操作。
常用的操作有以下几个:
click 点击对象
send_keys 输入,上传文件也是这个,定位到元素后,只需把文件路径填写到 send_keys 的参数里即可。
clear 清空文本内容
submit 提交表单
text 获取文本信息
submit 用来提交表单信息,而“百度一下”按钮的类型为 submit,所以 click 和 submit 在这里效果是一样的。
text 获取元素文本:
打印 title :
打印 url:
操作浏览器
浏览器后退: webDriver.navigate().back();
浏览器刷新: webDriver.navigate().refresh();
浏览器前进: webDriver.navigate().forward();
将浏览器滚动条滑到最底端: document.documentElement.scrollTop=10000
在 Java 中执行:((JavascriptExecutor)webDriver).executeScript(“document.documentElement.scrollTop=10000”);
滚动条回到最顶端则为:document.documentElement.scrollTop=0
浏览器最大化: webDriver.manage().window().maximize();
浏览器全屏: webDriver.manage().window().fullscreen();
浏览器窗口指定窗口大小(1200*900): webDriver.manage().window().setSize(new Dimension(1200,900));
键盘事件
输入:sendKeys(“要输入的内容”);
全选:sendKeys(Keys.CONTROL,“A”);
复制:sendKeys(Keys.CONTROL,“C”);
粘贴:sendKeys(Keys.CONTROL,“V”);
剪切:sendKeys(Keys.CONTROL,“X”);
鼠标事件
右击:contextClick()
双击:doubleClick()
拖动:dragAndDrop()
移动:moveToElement()
public static void main(String[] args) throws InterruptedException {
WebDriver webDriver = new ChromeDriver();
webDriver.get("https://www.baidu.com");
webDriver.findElement(By.cssSelector("#kw")).sendKeys("selenium");
Actions actions = new Actions(webDriver);
// 获取 图片 超链接元素
WebElement webElement= webDriver.findElement(By.linkText("图片"));
sleep(1000);
//moveToElemnt--移动到指定元素 contextClick--右击 perform--执行动作
actions.moveToElement(webElement).contextClick().perform();
}
下拉框选择
通过索引选择:
通过值选择:
alert 框操作
点击取消: webDriver.switchTo().alert().dismiss();
输入内容: webDriver.switchTo().alert().sendKeys(“selenium”);
点击确定: webDriver.switchTo().alert().accept();
浏览器关闭
close() 和 quit()
close() 关闭当前标签页,而 quit() 会关闭整个浏览器。
close() 不会清空缓存,而 quit() 会清空缓存。