1.通过name查找
2.通过id 查找
3.通过className 查找
4.通过linkText查找文本(注意:只能查找为a标签当中的文本,其他标签中的文本找不到)
5.通过partialLinkText 查找部分文本 如:我爱你,可通过我这一个字查找,(注意点与LinkText一样)
6.通过tagName(dom节点名)查找,就是标签名字 如a,span,div等标签名查找
7.通过xpath(xpath路径)查找,此方法与id,name方法使用较多
8.通过cssSelector(Css路径)查找,使用较少
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class locationElemet {
WebDriver driver;
@BeforeMethod
public void cpenChrome(){
//设置webdriver路劲
System.setProperty("webdriver.chrome.driver","D:\\Ideaprojects\\webdrivers\\chromedriver.exe");
//打来浏览器
driver=new ChromeDriver();
}
//通过name名字查找
@Test
public void findName(){
//输入百度地址
driver.get("http://www.baidu.com");
//通过name 查找
WebElement we =driver.findElement(By.name("wd"));
}
//通过id查找
@Test
public void findId(){
driver.get("http://www.baidu.com");
WebElement we=driver.findElement(By.id("kw"));
}
//通过id 查找
@Test
public void findId1() throws InterruptedException {
driver.get("http://www.baidu.com/");
//定位到这个地方,然后右键点击检查,此处也可以用name,
WebElement element1=driver.findElement(By.id("kw"));
//往百度搜索框输入selenium
element1.sendKeys("selenium");
driver.findElement(By.id("su")).click();
Thread.sleep(5000);
element1.clear(); //清空刚才输入的内容
@AfterMethod
public void closeChrom(){
driver.quit();
}
}