1、6种基本元素定位方法
package com.test;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
public class Elementlocation {
private static ChromeDriver chromedr;
public static void main(String []args) {
openChrome();
chromedr.findElement(By.id("kw")).sendKeys("python");
chromedr.findElement(By.name("wd")).clear();
chromedr.findElement(By.className("s_btn")).click();
chromedr.findElement(By.partialLinkText("新")).click();
chromedr.quit();
}
public static void openChrome() {
System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");
chromedr = new ChromeDriver();
chromedr.get("https://www.baidu.com");
}
}
CSSSelector定位
package com.test;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
public class Elementlocation {
private static ChromeDriver chromedr;
public static void main(String []args) {
openChrome();
chromedr.findElement(By.cssSelector("input"));
chromedr.findElement(By.cssSelector("#kw")).sendKeys("python");
chromedr.findElement(By.cssSelector(".s_ipt")).clear();
chromedr.findElement(By.cssSelector(".bg.s_btn")).click();
chromedr.findElement(By.cssSelector("input[value='百度一下']")).click();
chromedr.findElement(By.cssSelector("input[maxlength=\"255\"][autocomplete=\"off\"]")).sendKeys("python");
chromedr.quit();
}
public static void openChrome() {
System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");
chromedr = new ChromeDriver();
chromedr.get("https://www.baidu.com");
}
}
xpath定位
|