目录
2.4.1 根据元素属性,属性名=属性值,id,class,等都可写成这种形式
1.基本元素定位
1.1 id定位
package com.example.demo;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
public class ElementLocate {
private static FirefoxDriver firefoxDriver;
@Test
public void testFindElementById(){
openFirefox();
//定位到百度的搜索框元素,并且输入数据(ID定位)---唯一的
firefoxDriver.findElement(By.id("kw")).sendKeys("selenium");
}
public static void openFirefox() {
firefoxDriver = new FirefoxDriver();
firefoxDriver.get("https://www.baidu.com");
}
}
1.2 name定位
package com.example.demo;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
public class ElementLocate {
private static FirefoxDriver firefoxDriver;
@Test
public void testFindElementByName(){
openFirefox();
//定位到百度的搜索框元素,并且输入数据(Name定位)---重复
firefoxDriver.findElement(By.name("wd")).sendKeys("selenium");
}
public static void openFirefox() {
firefoxDriver = new FirefoxDriver();
firefoxDriver.get("https://www.baidu.com");
}
}
1.3 tagName定位
注意:会报错:element not visible
这是因为当tagName不唯一的时候,会定位到当前html页面的第一个所选属性
在通过TagName定位元素的时候,他不是定位到百度搜索框了,而是定位到html当中的第一个input元素,而第一个input元素是不可见的,所以会报这个错误
package com.example.demo;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
public class ElementLocate {
private static FirefoxDriver firefoxDriver;
@Test
public void testFindElementByTagName(){
openFirefox();
//定位到百度的搜索框元素,并且输入数据(TagName定位)---找到的元素是会有多个--不推荐使用
//会报错:element not visible,这是因为在通过TagName定位元素的时候,
//他不是定位到百度搜索框了,而是定位到html当中的第一个input元素,第一个input元素是不可见的,
//所以会报这个错误
firefoxDriver.findElement(By.tagName("input")).sendKeys("selenium");
}
public static void openFirefox() {
firefoxDriver = new FirefoxDriver();
firefoxDriver.get("https://www.baidu.com");
}
}
1.4 className定位
注意:当有多个className定位时候,需选择当前html页面全局唯一的那个className属性去定位
package com.example.demo;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
public class ElementLocate {
private static FirefoxDriver firefoxDriver;
@Test
public void testFindElementByClassName(){
openFirefox();
//定位到百度的搜索框元素,并且输入数据(className定位)
firefoxDriver.findElement(By.className("s_ipt")).sendKeys("selenium");
//会报错:Compoud class names not permitted--->复合类名的问题(属性不止有一个)
//解决办法:通过其中一个属性去定位,要求是该属性在当前html页面必须唯一
firefoxDriver.findElement(By.className("bg s_btn")).click();
}
public static void openFirefox() {
firefoxDriver = new FirefoxDriver();
firefoxDriver.get("https://www.baidu.com");
}
}
1.5 LinkText()定位
package com.example.demo;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
public class ElementLocate {
private static FirefoxDriver firefoxDriver;
@Test
public void testFindElementByLinkText(){
openFirefox();
//定位“新闻”元素,并且点击(LinkText定位)--->超链接完整文本
firefoxDriver.findElement(By.linkText("新闻")).click();
//定位“新闻”元素,并且点击(partialLinkText)--->超链接部分文本
firefoxDriver.findElement(By.partialLinkText("闻")).click();
}
public static void openFirefox() {
firefoxDriver = new FirefoxDriver();
firefoxDriver.get("https://www.baidu.com");
}
}
2.cssSlector定位
2.1 根据tagName
package com.example.demo;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
public class ElementLocate {
private static FirefoxDriver firefoxDriver;
@Test
public void testFindElementByCssSelector(){
//cssSlector定位
openFirefox();
//(1)通过tagName定位--->不推荐
//会报错,具体原因和上述tagName定位一致,这里不再赘述
firefoxDriver.findElement(By.cssSelector("input"));
}
public static void openFirefox() {
firefoxDriver = new FirefoxDriver();
firefoxDriver.get("https://www.baidu.com");
}
}
2.2 根据ID
package com.example.demo;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
public class ElementLocate {
private static FirefoxDriver firefoxDriver;
@Test
public void testFindElementByCssSelector(){
//cssSlector定位
openFirefox();
//(2)通过id定位,注意:需加#
firefoxDriver.findElement(By.cssSelector("#kw")).sendKeys("selenium");
}
public static void openFirefox() {
firefoxDriver = new FirefoxDriver();
firefoxDriver.get("https://www.baidu.com");
}
}
2.3 根据className(样式名)
package com.example.demo;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
public class ElementLocate {
private static FirefoxDriver firefoxDriver;
@Test
public void testFindElementByCssSelector(){
//cssSlector定位
openFirefox();
//(3)通过className定位,注意:需加.
//如果属性不唯一,需给每个属性前面都加.
firefoxDriver.findElement(By.cssSelector(".s_ipt")).sendKeys("selenium");
}
public static void openFirefox() {
firefoxDriver = new FirefoxDriver();
firefoxDriver.get("https://www.baidu.com");
}
}
2.4 css精确定位
2.4.1 根据元素属性,属性名=属性值,id,class,等都可写成这种形式
语法:By.cssSelector("标签名[属性名='属性值']");
如:By.cssSelector("input[name='xxx']");
举个例子:定位百度搜索框,我们可以通过属性名=属性值精确定位
@Test
public void testCssSelector(){
openFirefox();
firefoxDriver.findElement(By.cssSelector("input[maxlength='255']")).sendKeys("selenium");
}
2.4.2 多属性
语法:By.cssSelector("标签名[属性1='属性值'][属性2]='属性值']");
举个例子:同样是定位百度搜索框,我们可以通过两个属性来精确定位
@Test
public void testCssSelector(){
openFirefox();
firefoxDriver.findElement(By.cssSelector("input[maxlength='255'][autocomplete='off']")).sendKeys("selenium");
}
3.xpath定位
- xpath其实就是一个path(路径),一个描述页面元素位置信息的路径,相当于元素的坐标
- xpath基于XML文档树状结构,是XML路径语言,用来查询XML文档中的节点
3.1 绝对路径
从根开始找--/(根目录)/html/body/div[2]/div/form/div[5]/button
缺点:一旦页面结构发生变化(比如重新设计时,路径少了两节),该路径也随之失效,必须重新写,不推荐使用
3.2 相对路径
xpath相对路径:只要不是/开始的,就是相对路径//*[@id="kw"]
路径解释:
- //匹配指定节点,不考虑他们位置(/则表示绝对路径,从根下开始)
- *通配符,匹配任意节点元素
- @选取属性
- []属性判断条件表达
相对定位优点:灵活、方便、耦合性低
举个例子:定位百度搜索框
@Test
public void testFindElementByXpath(){
openFirefox();
firefoxDriver.findElement(By.xpath("//input[@maxlength='255' and @autocomplete='off']")).sendKeys("selenium");
}
xpath也可以通过文本值定位
举个例子:定位新闻这个超链接
@Test
public void testFindElementByXpath(){
openFirefox();
firefoxDriver.findElement(By.xpath("//a[text()='新闻']")).click();
}
也可以匹配部分文本值,还是定位新闻超链接
@Test
public void testFindElementByXpath(){
openFirefox();
firefoxDriver.findElement(By.xpath("//a[contains(text(),'新')]")).click();
}
4.元素操作API
4.1 click()
触发当前元素的点击事件
4.2 clear()
清空内容
4.3 sendkeys(...)
往文本框一类元素中写入内容(按键操作)
package com.example.demo;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
public class ElementOperate {
private static FirefoxDriver firefoxDriver;
@Test
public void testSendKeysAndClear() throws InterruptedException {
openFirefox();
firefoxDriver.findElement(By.id("kw")).sendKeys("selenium");
//让当前线程等待3s
Thread.sleep(3000);
firefoxDriver.findElement(By.id("kw")).clear();
}
public static void openFirefox() {
firefoxDriver = new FirefoxDriver();
firefoxDriver.get("https://www.baidu.com");
}
}
4.4 getTagName()
获取元素的标签名
4.5 getAttribute(属性名)
根据属性名获取元素属性值
4.6 getText()
获取当前元素的文本值
4.7 isDisplayed()
查看元素是否显示
package com.example.demo;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class ElementOperate {
private static FirefoxDriver firefoxDriver;
@Test
public void testSendKeysAndClear() throws InterruptedException {
openFirefox();
firefoxDriver.findElement(By.id("kw")).sendKeys("selenium");
//让当前线程等待3s
Thread.sleep(3000);
firefoxDriver.findElement(By.id("kw")).clear();
}
@Test
public void testOthers(){
openFirefox();
WebElement webElement = firefoxDriver.findElement(By.id("kw"));
System.out.println("元素标签名:" + webElement.getTagName());
System.out.println("元素maxlength属性:" + webElement.getAttribute("maxlength"));
WebElement webElement1 = firefoxDriver.findElement(By.xpath("//a[text()='hao123']"));
System.out.println("元素文本值:" + webElement1.getText());
System.out.println("元素是否显示:"+webElement1.isDisplayed());
}
public static void openFirefox() {
firefoxDriver = new FirefoxDriver();
firefoxDriver.get("https://www.baidu.com");
}
}