selenium实战:八种元素定位

selenium的webdriver提供了以下八种基本的元素定位方法:

1、通过id定位:findElement(By.id());
2、通过name定位:findElement(By.name());
3、通过class定位:findElement(By.className());
4、通过tag定位:findElement(By.tagName());
5、通过link定位:findElement(By.linkText());
6、通过partialLink定位:findElement(By.partialLinkText());
7、通过xpath定位:findElement(By.xpath());
8、通过css定位:findElement(By.cssSelector());

实测脚本,测试通过:PS:非专,脚本规范请忽略
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;




public class FindElement {

    public static void main(String[] args) {
        //指定chrome驱动
        System.setProperty("webdriver.chrome.driver", "C:\\drivers\\chromedriver.exe");
        //测试八种方法,测试时请指定方法
        findById();
        findByName();
        findByClassName();
        findByTagName();
        findByLinkText();
        findPartailLinkText();
        findByXpath();
        findByCssSelector();
    }

    /**
     * 定位元素1:findElement(By.id())
     */
    public static void findById() {
        WebDriver driver = new ChromeDriver();
        String url = "http://www.baidu.com";
        driver.get(url);
        //通过id定位百度搜索输入框,并输入“selenium.By.id”
        WebElement element = driver.findElement(By.id("kw"));
        element.sendKeys("selenium.By.id");


    }

    /**
     * 元素定位2:findElement(By.name())
     */
    public static void findByName() {
        WebDriver driver = new ChromeDriver();
        String url = "http://www.baidu.com";
        driver.get(url);
        //通过name定位百度搜索输入框,并输入“selenium.By.name”
        WebElement element = driver.findElement(By.name("wd"));
        element.sendKeys("selenium.By.name");
    }


    /**
     * 元素定位3:findElement(By.className())
     */
    public static void findByClassName() {
        WebDriver driver = new ChromeDriver();
        String url = "http://www.baidu.com";
        driver.get(url);
        //通过className定位百度搜索输入框,并输入java.className
        WebElement element = driver.findElement(By.className("s_ipt"));
        element.sendKeys("selenium.className");
    }

    /**
     * 元素定位4:findElement(By.tagName())
     */
    public static void findByTagName() {
        WebDriver driver = new ChromeDriver();
        String url = "http://news.baidu.com";
        driver.get(url);
        //通过tagName(标签)定位百度搜索输入框,并输入“selenium.tagName”
        //www 无效,改为news.baidu.com 正常
        WebElement element = driver.findElement(By.tagName("input"));
        element.sendKeys("selenium.tagName");
    }

    /**
     * 元素定位5:findElement(By.linkText())
     */
    public static void findByLinkText() {
        WebDriver driver = new ChromeDriver();
        String url = "http://www.baidu.com";
        driver.get(url);
        //通过linkText(超链接)定位到hao123按钮,并点击;这个是精确查找
        WebElement element = driver.findElement(By.linkText("hao123"));
        element.click();
    }

    /**
     * 元素定位6:findElement(By.partialLinkText())
     */
    public static void findPartailLinkText() {
        WebDriver driver = new ChromeDriver();
        String url = "http://www.baidu.com";
        driver.get(url);
        //通过partailLinkText(超链接)定位到hao123按钮,并点击;这个是模糊查找
        WebElement element = driver.findElement(By.partialLinkText(""));
        element.click();
    }

    /**
     * 元素定位7:findElement(By.xpath())
     */
    public static void findByXpath() {
        WebDriver driver = new ChromeDriver();
        String url = "http://www.baidu.com";
        driver.get(url);
        //通过XPath定位到百度输入框,并输入“XPath”
        WebElement element = driver.findElement(By.xpath(".//*[@id='kw']"));
        element.sendKeys("selenium.XPath");
    }

    /**
     * 元素定位8:findElement(By.cssSelector())
     */
    public static void findByCssSelector() {
        WebDriver driver = new ChromeDriver();
        String url = "http://www.baidu.com";
        driver.get(url);
        //通过CSS定位到百度输入框,并输入“Css_Selector”
        WebElement element = driver.findElement(By.cssSelector("#kw"));
        element.sendKeys("Css_Selector");
    }

}

 

posted @ 2021-08-12 15:06  测试长流  阅读(378)  评论(0)    收藏  举报