多种方式定位元素
ID和Xpath
package testDemo;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
/**
* @author 96510
* @version 1.0
* @date 2021/6/26
*/
public class IdXpathDemo {
public static void main(String[] args) throws InterruptedException {
WebDriver dr = new ChromeDriver();
String url = "http://www.baidu.com";
dr.manage().window().maximize();
dr.get(url);
dr.findElement(By.id("kw")).sendKeys("selenium webdriver");
dr.findElement(By.id("su")).click();
// dr.manage().wait(3000);
Thread.sleep(3000);
dr.quit();
}
}
Name和Xpath
package testDemo;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
/**
* @author 96510
* @version 1.0
* @date 2021/6/26
*/
public class XpathNameDemo {
public static void main(String[] args) throws InterruptedException {
WebDriver dr = new ChromeDriver();
String url = "http://www.baidu.com";
dr.manage().window().maximize();
dr.get(url);
dr.findElement(By.name("wd")).sendKeys("selenium webdriver");
dr.findElement(By.id("su")).click();
// dr.manage().wait(3000);
Thread.sleep(3000);
dr.quit();
}
}
Linktext和PartialLinktext
package testDemo;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
/**
* @author 96510
* @version 1.0
* @date 2021/6/26
*/
public class LinktextDemo {
public static void main(String[] args) throws InterruptedException {
WebDriver dr = new ChromeDriver();
String url = "http://www.baidu.com";
dr.manage().window().maximize();
dr.get(url);
// dr.findElement(By.name("wd")).sendKeys("selenium webdriver");
dr.findElement(By.linkText("hao123")).click();//linktext
dr.findElement(By.partialLinkText("123")).click();//partiallinktext
// dr.manage().wait(3000);
Thread.sleep(3000);
dr.quit();
}
}
ClassName
package testDemo;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
/**
* @author 96510
* @version 1.0
* @date 2021/6/26
*/
public class ClassNameDemo {
public static void main(String[] args) throws InterruptedException {
WebDriver dr = new ChromeDriver();
String url = "http://www.baidu.com";
dr.manage().window().maximize();
dr.get(url);
// dr.findElement(By.name("wd")).sendKeys("selenium webdriver");
Thread.sleep(3000);
dr.findElement(By.className("hot-refresh-text")).click();
// dr.manage().wait(3000);
Thread.sleep(3000);
dr.quit();
}
}
TagName(基本用不到)
package testDemo;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
/**
* @author 96510
* @version 1.0
* @date 2021/6/26
*/
public class TagNameDemo {
public static void main(String[] args) throws InterruptedException {
WebDriver dr = new ChromeDriver();
String url = "http://www.baidu.com";
dr.manage().window().maximize();
dr.get(url);
dr.findElement(By.tagName("a")).click();//必须保证整个页面唯一
// dr.manage().wait(3000);
Thread.sleep(3000);
dr.quit();
}
}
面试常问:
常用的定位元素的方法有哪些?
id 最快
name
class name
xpath
linktext
partial linktext
tag name
css selector
id xpath css selector 用的最多