driver2.findElement(By.id())

driver.findElement(By.name())

driver.findElement(By.className())

driver.findElement(By.tagName())

driver.findElement(By.linkText())

driver.findElement(By.partialLinkText())

driver.findElement(By.xpath())

driver.findElement(By.cssSelector())

ID 实例代码:

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

 

public class ByIdTest {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

        

WebDriver driver=init();

        driver.get("http://www.baidu.com");

        //通过ID 定位

        driver.findElement(By.id("kw")).sendKeys("软件测试工程师");

        driver.findElement(By.id("su")).click();

        // 停留 5s

        try {

Thread.sleep(6000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

        // 关闭

        driver.close();

        

}

public static WebDriver init() {

System.setProperty("webdriver.chrome.driver","C:\\Users\\huai.chen\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe");

WebDriver driver=new ChromeDriver();

return driver;

}

 

}

Name 实例代码:

package com.chenhuai.test;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

 

public class ByNameTest {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

WebDriver driver =new InitWebDriver().init();

 

// 通过名字定位元素

driver.findElement(By.name("wd")).sendKeys("软件开发工程师");

driver.findElement(By.id("su")).click();

try {

Thread.sleep(5000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

driver.close();

}

 

}

 

ClassName 实例代码:

package com.chenhuai.test;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

 

public class ByClassName {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

       WebDriver driver=new InitWebDriver().init();

       // 通过 样式名称 定位元素

       driver.findElement(By.className("s_ipt")).sendKeys("大数据分析");

       driver.findElement(By.className("s_btn")).click();

       try {

Thread.sleep(3000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

       driver.close();

}

 

}

 

Link 实例代码:

package com.chenhuai.test;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

 

public class ByLinkText {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

     WebDriver driver=new InitWebDriver().init();

     driver.findElement(By.linkText("新闻")).click();

     driver.findElement(By.linkText("地图")).click();

     driver.findElement(By.partialLinkText("hao")).click();

}

 

}

 

CssSelector 实例代码如下:

 

package com.chenhuai.test;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

 

public class CssSelectorTest {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

WebDriver driver=new InitWebDriver().init();

// 通过css 中间的class 进行定位 .表示通过class 属性来定位元素

//driver.findElement(By.cssSelector(".s_ipt")).sendKeys("长城");

//driver.findElement(By.cssSelector(".s_btn")).click();

// 通过css 中间的class 进行定位,# 表示id 属性定位元素

driver.findElement(By.cssSelector("#kw")).sendKeys("深圳");

driver.findElement(By.cssSelector("#su")).click();

 

}

 

}

 

Xpath 绝对路径实例代码:

package com.chenhuai.test;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

 

public class XpathTest {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

WebDriver driver=new InitWebDriver().init();

// 通过 xpath 定位元素位置

driver.findElement(By.xpath("/html/body/div/div/div/div/div/form/span/input")).sendKeys("xpath");

driver.findElement(By.xpath("/html/body/div/div/div/div/div/form/span[2]/input")).click();

 

}

 

}

 

元素属性定位实例代码如下:

package com.chenhuai.test;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

 

public class XpathTest02 {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

WebDriver driver=new InitWebDriver().init();

//driver.findElement(By.xpath("//input[@name='wd']")).sendKeys("微信");

//driver.findElement(By.xpath("//input[@id='su']")).click();

/*

 * driver.findElement(By.xpath("//input[@class='s_ipt']")).sendKeys("腾讯云");

 * driver.findElement(By.xpath("//input[@class='bg s_btn']")).click();

 */

//driver.findElement(By.xpath("//input[@maxlength='255']")).sendKeys("华为云");

//driver.findElement(By.xpath("//input[@value='百度一下']")).click();

//driver.findElement(By.xpath("//input[@type='submit']")).click();

driver.findElement(By.xpath("//input[@id='kw' and @class='s_ipt']")).sendKeys("爱奇艺");

driver.findElement(By.xpath("//input[@type='submit' and @value='百度一下']")).click();

}

 

}

 

如果你觉的文章阅读不过瘾,可以查看详细的视频教程

【软件测试全栈系列课程】请点击我哦…

 https://edu.51cto.com/course/25359.html

【博主完整视频课程系列】请点击我哦…

 https://edu.51cto.com/lecturer/13226632.html

【JMETER基础和实践课程】请点击我哦…

 https://edu.51cto.com/course/28017.html

【JMETER 性能测试基础与项目实战视频课程】请点击我哦…

 https://edu.51cto.com/course/16055.html

【Jmeter+ant+jenkins接口层性能与自动化测试课程】请点击我哦…

 https://edu.51cto.com/course/19323.html

【零基础新手入门软件测试基础课程】请点击我哦…

 https://edu.51cto.com/course/27846.html

【软件测试之移动端测试系列课程】请点击我哦…

 https://edu.51cto.com/course/26878.html

【Fiddler接口抓包神器使用教程】请点击我哦…

 https://edu.51cto.com/course/28066.html