selenium实战:xpath定位实战

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;

import java.util.concurrent.TimeUnit;


public class FindByXPath {

    public static void main(String[] args) {
        //指定chrome驱动
        System.setProperty("webdriver.chrome.driver", "C:\\drivers\\chromedriver.exe");
        //测试八种方法,测试时请指定方法
        //一、XPath常用的定位方法
        //XpathById();
        //XpathByName();
        //XpathByClass();
        //XpathByOther();

        //二、XPath标签的定位方法
        //XpathByTagId();
        //XpathByTagName();
        //XpathByTagClass();
        //XpathByTagOther();

        //三、XPath层级的定位方法
        //XpathByLevel();

        //四、XPath索引的定位方法
        //XpathByIndex();

        //五、逻辑运算
        //XpathByLogic();

        //六、模糊匹配
        //XpathByLikeName();
        //XpathByLikeText();
        //XpathByAsBegin();
        //XpathByAsEndsWith();
        XpathByAsEndMatch();


    }

    /**
     * 一、XPath常用的定位方法
     */

    /**
     * 1. XPath通过id属性定位
     */
    public static void XpathById() {
        WebDriver driver = new ChromeDriver();
        String url = "http://www.baidu.com";
        driver.get(url);
        driver.manage().window().maximize();
        //XPath通过id定位百度搜索输入框,并输入“XPath通过id属性定位”
        WebElement element = driver.findElement(By.xpath("//*[@id = ‘kw‘]"));
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        element.sendKeys("XPath通过id属性定位");

    }

    /**
     * 2. XPath通过name属性定位
     */
    public static void XpathByName() {
        WebDriver driver = new ChromeDriver();
        String url = "http://www.baidu.com";
        driver.get(url);
        driver.manage().window().maximize();
        //XPath通过name定位百度搜索输入框,并输入“XPath通过name属性定位”
        WebElement element = driver.findElement(By.xpath("//*[@name = ‘wd‘]"));
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        element.sendKeys("XPath通过name属性定位");

    }

    /**
     * 3. XPath通过class属性定位
     */
    public static void XpathByClass() {
        WebDriver driver = new ChromeDriver();
        String url = "http://www.baidu.com";
        driver.get(url);
        driver.manage().window().maximize();
        //XPath通过class定位百度搜索输入框,并输入“XPath通过class属性定位”
        WebElement element = driver.findElement(By.xpath("//*[@class = ‘s_ipt‘]"));
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        element.sendKeys("XPath通过class属性定位");

    }

    /**
     * 4. XPath通过其它属性定位
     */
    public static void XpathByOther() {
        WebDriver driver = new ChromeDriver();
        String url = "http://www.baidu.com";
        driver.get(url);
        driver.manage().window().maximize();
        //XPath通过其它属性定位百度搜索输入框,并输入“XPath通过其他属性定位”
        WebElement element = driver.findElement(By.xpath("//*[@autocomplete = ‘off‘]"));
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        element.sendKeys("XPath通过其他属性定位");

    }


    /**
     * 二、XPath:标签的定位方法
     */

    /**
     * 1. XPath通过标签定位:id属性定位
     */
    public static void XpathByTagId() {
        WebDriver driver = new ChromeDriver();
        String url = "http://www.baidu.com";
        driver.get(url);
        driver.manage().window().maximize();
        //XPath通过标签id定位百度搜索输入框,并输入“XPath通过标签定位:属性为id”
        WebElement element = driver.findElement(By.xpath("//input[@id = ‘kw‘]"));
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        element.sendKeys("XPath通过标签定位:属性为id");

    }

    /**
     * 2. XPath通过标签定位:name属性定位
     */
    public static void XpathByTagName() {
        WebDriver driver = new ChromeDriver();
        String url = "http://www.baidu.com";
        driver.get(url);
        driver.manage().window().maximize();
        //XPath通过标签name定位百度搜索输入框,并输入“XPath通过标签定位:属性为name”
        WebElement element = driver.findElement(By.xpath("//input[@name = ‘wd‘]"));
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        element.sendKeys("XPath通过标签定位:属性为name");

    }

    /**
     * 3. XPath通过标签定位:class属性定位
     */
    public static void XpathByTagClass() {
        WebDriver driver = new ChromeDriver();
        String url = "http://www.baidu.com";
        driver.get(url);
        driver.manage().window().maximize();
        //XPath通过标签class定位百度搜索输入框,并输入“XPath通过标签定位:属性为class”
        WebElement element = driver.findElement(By.xpath("//input[@class = ‘s_ipt‘]"));
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        element.sendKeys("XPath通过标签定位:属性为class");

    }

    /**
     * 4. XPath通过标签定位:其它属性定位
     */
    public static void XpathByTagOther() {
        WebDriver driver = new ChromeDriver();
        String url = "http://www.baidu.com";
        driver.get(url);
        driver.manage().window().maximize();
        //XPath通过标签其它属性定位百度搜索输入框,并输入“XPath通过标签定位:其它属性”
        WebElement element = driver.findElement(By.xpath("//input[@autocomplete = ‘off‘]"));
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        element.sendKeys("XPath通过标签定位:其它属性");

    }

    /**
     * 三、XPath:层级
     */

    /**
     * 通过定位它爷爷来定位input输入框
     */
    public static void XpathByLevel() {
        WebDriver driver = new ChromeDriver();
        String url = "http://www.baidu.com";
        driver.get(url);
        driver.manage().window().maximize();
        //XPath通过层级定位百度搜索输入框,并输入“XPath通过定位它爷爷来定位input输入框”
        WebElement element = driver.findElement(By.xpath("//form[@id = ‘form‘]/span/input"));
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        element.sendKeys("XPath通过定位它爷爷来定位input输入框");

    }

    /**
     * 四、XPath:索引
     */

    /**
     *  1.如果一个元素它的兄弟元素跟它的标签一样,这时候无法通过层级定位到。因为都是一个父亲生的,多胞胎兄弟。
     *
     *  2.虽然双胞胎兄弟很难识别,但是出生是有先后的,于是可以通过它在家里的排行老几定位到。
     *
     *  3. selenium的索引是从1开始的
     */
    public static void XpathByIndex() {
        WebDriver driver = new ChromeDriver();
        String Url2 = "https://home.cnblogs.com/set/profile";
        driver.get(Url2);
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

        WebElement name = driver.findElement(By.id("mat-input-0"));
        name.sendKeys("814639416@qq.com");
        WebElement PassWord = driver.findElement(By.id("mat-input-1"));
        PassWord.sendKeys("steven1990@");
        WebElement login = driver.findElement(By.tagName("button"));
        login.click();

        //登录后一定要页面等待,否则会报错
        try {
            //页面等待
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //定位ul下多个同等级的li,选择第5个
        WebElement option = driver.findElement(By.xpath("//ul[@class = ‘app_list‘]/li[7]"));
        option.click();
    }

    /**
     * 五、XPath:逻辑运算
     */

    /**
     * 1.XPath还有一个比较强的功能,是可以多个属性逻辑运算的,可以支持与(and)、或(or)、非(not)
     * 2.一般用的比较多的是and运算,同时满足两个属性
     */
    public static void XpathByLogic() {
        WebDriver driver = new ChromeDriver();
        String url = "http://www.baidu.com";
        driver.get(url);
        driver.manage().window().maximize();
        //XPath通两个属性定位百度搜索输入框,并输入“逻辑运算”
        WebElement element = driver.findElement(By.xpath("//*[@id = ‘kw‘ and @autocomplete = ‘off‘]"));
        element.sendKeys("逻辑运算");

    }

    /**
     * 六、XPath:模糊匹配
     */

    /**
     * 1.1 XPath模糊匹配:contains匹配name中含有briicon的元素
     */
    public static void XpathByLikeName() {
        WebDriver driver = new ChromeDriver();
        String url = "http://www.baidu.com";
        driver.get(url);
        driver.manage().window().maximize();
        //XPath通过属性:@name模糊关键字定位,并打开更多页面
        WebElement element = driver.findElement(By.xpath("//*[contains(@name,‘briicon‘)]"));
        element.click();


    }


    /**
     * 1.2. XPath模糊匹配:contains匹配text中含有"贴吧"的元素
     */
    public static void XpathByLikeText() {
        WebDriver driver = new ChromeDriver();
        String url = "http://www.baidu.com";
        driver.get(url);
        driver.manage().window().maximize();
        //XPath通过属性:@text模糊关键字定位,并点击
        WebElement element = driver.findElement(By.xpath("//*[contains(text(),‘贴吧‘)]"));
        element.click();

    }

    /**
     * 2. XPath模糊匹配:starts-with以什么开头
     */
    public static void XpathByAsBegin() {
        WebDriver driver = new ChromeDriver();
        String url = "http://www.baidu.com";
        driver.get(url);
        driver.manage().window().maximize();
        //XPath通过两个属性定位百度搜索输入框,并输入“@id  and @autocomplete”
        WebElement element = driver.findElement(By.xpath("//*[@id = ‘kw‘ and @autocomplete = ‘off‘]"));
        element.sendKeys("通过@id  and @autocomplete定位");
        //输入后点击“百度一下”按钮进行查询
        WebElement element2 = driver.findElement(By.xpath("//*[@id = ‘su‘]"));
        element2.click();

        try {
            //页面等待
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //XPath通过以w开头模糊查询属性name,一般是第1个,所以选择了name属性,并输入“starts-with(@name,‘w‘)”
        WebElement element3 = driver.findElement(By.xpath("//*[starts-with(@name,‘w‘)]"));
        element3.clear();
        element3.sendKeys("通过starts-with(@name,‘w‘)清空关键字后,并输入此句内容");

        try {
            //页面等待
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //10秒后清空关键字
        element3.clear();

    }

    /**
     * 3.1 XPath模糊匹配:ends-with以什么结尾,暂未测试通过,原因:可能是标准的XPath 2.0功能。 它在XPath 1.0中不可用
     */
    public static void XpathByAsEndsWith() {
        WebDriver driver = new ChromeDriver();
        String url = "http://www.baidu.com";
        driver.get(url);
        driver.manage().window().maximize();
        //XPath通过ends-with以什么结尾定位百度搜索输入框,并输入“ends-with定位”,,暂未测试通过定位
        WebElement element = driver.findElement(By.xpath("//*[ends-with(@class,‘quickdelete-wrap‘)]/input"));
        element.sendKeys("ends-with定位");

    }


    /**
     * 3.2 XPath模糊匹配:matches以什么结果,,暂未测试通过,原因:可能是标准的XPath 2.0功能。 它在XPath 1.0中不可用
     */
    public static void XpathByAsEndMatch() {
        WebDriver driver = new ChromeDriver();
        String url = "http://www.baidu.com";
        driver.get(url);
        driver.manage().window().maximize();
        //XPath通过层级定位百度搜索输入框,并输入“XPath通过定位它爷爷来定位input输入框”
        WebElement element = driver.findElement(By.xpath("//*[matches(text(),‘地图‘)]"));
        element.click();

    }



}

 

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;

import java.util.concurrent.TimeUnit;


public class FindByXPath {

public static void main(String[] args) {
//指定chrome驱动
System.setProperty("webdriver.chrome.driver", "C:\\drivers\\chromedriver.exe");
//测试八种方法,测试时请指定方法
//一、XPath常用的定位方法
//XpathById();
//XpathByName();
//XpathByClass();
//XpathByOther();

//二、XPath标签的定位方法
//XpathByTagId();
//XpathByTagName();
//XpathByTagClass();
//XpathByTagOther();

//三、XPath层级的定位方法
//XpathByLevel();

//四、XPath索引的定位方法
//XpathByIndex();

//五、逻辑运算
//XpathByLogic();

//六、模糊匹配
//XpathByLikeName();
//XpathByLikeText();
//XpathByAsBegin();
//XpathByAsEndsWith();
XpathByAsEndMatch();


}

/**
* 一、XPath常用的定位方法
*/

/**
* 1. XPath通过id属性定位
*/
public static void XpathById() {
WebDriver driver = new ChromeDriver();
String url = "http://www.baidu.com";
driver.get(url);
driver.manage().window().maximize();
//XPath通过id定位百度搜索输入框,并输入“XPath通过id属性定位
WebElement element = driver.findElement(By.xpath("//*[@id = ‘kw‘]"));
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
element.sendKeys("XPath通过id属性定位");

}

/**
* 2. XPath通过name属性定位
*/
public static void XpathByName() {
WebDriver driver = new ChromeDriver();
String url = "http://www.baidu.com";
driver.get(url);
driver.manage().window().maximize();
//XPath通过name定位百度搜索输入框,并输入“XPath通过name属性定位
WebElement element = driver.findElement(By.xpath("//*[@name = ‘wd‘]"));
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
element.sendKeys("XPath通过name属性定位");

}

/**
* 3. XPath通过class属性定位
*/
public static void XpathByClass() {
WebDriver driver = new ChromeDriver();
String url = "http://www.baidu.com";
driver.get(url);
driver.manage().window().maximize();
//XPath通过class定位百度搜索输入框,并输入“XPath通过class属性定位
WebElement element = driver.findElement(By.xpath("//*[@class = ‘s_ipt‘]"));
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
element.sendKeys("XPath通过class属性定位");

}

/**
* 4. XPath通过其它属性定位
*/
public static void XpathByOther() {
WebDriver driver = new ChromeDriver();
String url = "http://www.baidu.com";
driver.get(url);
driver.manage().window().maximize();
//XPath通过其它属性定位百度搜索输入框,并输入“XPath通过其他属性定位
WebElement element = driver.findElement(By.xpath("//*[@autocomplete = ‘off‘]"));
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
element.sendKeys("XPath通过其他属性定位");

}


/**
* 二、XPath:标签的定位方法
*/

/**
* 1. XPath通过标签定位:id属性定位
*/
public static void XpathByTagId() {
WebDriver driver = new ChromeDriver();
String url = "http://www.baidu.com";
driver.get(url);
driver.manage().window().maximize();
//XPath通过标签id定位百度搜索输入框,并输入“XPath通过标签定位:属性为id”
WebElement element = driver.findElement(By.xpath("//input[@id = ‘kw‘]"));
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
element.sendKeys("XPath通过标签定位:属性为id");

}

/**
* 2. XPath通过标签定位:name属性定位
*/
public static void XpathByTagName() {
WebDriver driver = new ChromeDriver();
String url = "http://www.baidu.com";
driver.get(url);
driver.manage().window().maximize();
//XPath通过标签name定位百度搜索输入框,并输入“XPath通过标签定位:属性为name”
WebElement element = driver.findElement(By.xpath("//input[@name = ‘wd‘]"));
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
element.sendKeys("XPath通过标签定位:属性为name");

}

/**
* 3. XPath通过标签定位:class属性定位
*/
public static void XpathByTagClass() {
WebDriver driver = new ChromeDriver();
String url = "http://www.baidu.com";
driver.get(url);
driver.manage().window().maximize();
//XPath通过标签class定位百度搜索输入框,并输入“XPath通过标签定位:属性为class”
WebElement element = driver.findElement(By.xpath("//input[@class = ‘s_ipt‘]"));
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
element.sendKeys("XPath通过标签定位:属性为class");

}

/**
* 4. XPath通过标签定位:其它属性定位
*/
public static void XpathByTagOther() {
WebDriver driver = new ChromeDriver();
String url = "http://www.baidu.com";
driver.get(url);
driver.manage().window().maximize();
//XPath通过标签其它属性定位百度搜索输入框,并输入“XPath通过标签定位:其它属性
WebElement element = driver.findElement(By.xpath("//input[@autocomplete = ‘off‘]"));
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
element.sendKeys("XPath通过标签定位:其它属性");

}

/**
* 三、XPath:层级
*/

/**
* 通过定位它爷爷来定位input输入框
*/
public static void XpathByLevel() {
WebDriver driver = new ChromeDriver();
String url = "http://www.baidu.com";
driver.get(url);
driver.manage().window().maximize();
//XPath通过层级定位百度搜索输入框,并输入“XPath通过定位它爷爷来定位input输入框
WebElement element = driver.findElement(By.xpath("//form[@id = ‘form‘]/span/input"));
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
element.sendKeys("XPath通过定位它爷爷来定位input输入框");

}

/**
* 四、XPath:索引
*/

/**
* 1.如果一个元素它的兄弟元素跟它的标签一样,这时候无法通过层级定位到。因为都是一个父亲生的,多胞胎兄弟。
*
* 2.虽然双胞胎兄弟很难识别,但是出生是有先后的,于是可以通过它在家里的排行老几定位到。
*
* 3. selenium的索引是从1开始的
*/
public static void XpathByIndex() {
WebDriver driver = new ChromeDriver();
String Url2 = "https://home.cnblogs.com/set/profile";
driver.get(Url2);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

WebElement name = driver.findElement(By.id("mat-input-0"));
name.sendKeys("814639416@qq.com");
WebElement PassWord = driver.findElement(By.id("mat-input-1"));
PassWord.sendKeys("steven1990@");
WebElement login = driver.findElement(By.tagName("button"));
login.click();

//登录后一定要页面等待,否则会报错
try {
//页面等待
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//定位ul下多个同等级的li,选择第5
WebElement option = driver.findElement(By.xpath("//ul[@class = ‘app_list‘]/li[7]"));
option.click();
}

/**
* 五、XPath:逻辑运算
*/

/**
* 1.XPath还有一个比较强的功能,是可以多个属性逻辑运算的,可以支持与(and)、或(or)、非(not
* 2.一般用的比较多的是and运算,同时满足两个属性
*/
public static void XpathByLogic() {
WebDriver driver = new ChromeDriver();
String url = "http://www.baidu.com";
driver.get(url);
driver.manage().window().maximize();
//XPath通两个属性定位百度搜索输入框,并输入逻辑运算
WebElement element = driver.findElement(By.xpath("//*[@id = ‘kw‘ and @autocomplete = ‘off‘]"));
element.sendKeys("逻辑运算");

}

/**
* 六、XPath:模糊匹配
*/

/**
* 1.1 XPath模糊匹配:contains匹配name中含有briicon的元素
*/
public static void XpathByLikeName() {
WebDriver driver = new ChromeDriver();
String url = "http://www.baidu.com";
driver.get(url);
driver.manage().window().maximize();
//XPath通过属性:@name模糊关键字定位,并打开更多页面
WebElement element = driver.findElement(By.xpath("//*[contains(@name,‘briicon‘)]"));
element.click();


}


/**
* 1.2. XPath模糊匹配:contains匹配text中含有"贴吧"的元素
*/
public static void XpathByLikeText() {
WebDriver driver = new ChromeDriver();
String url = "http://www.baidu.com";
driver.get(url);
driver.manage().window().maximize();
//XPath通过属性:@text模糊关键字定位,并点击
WebElement element = driver.findElement(By.xpath("//*[contains(text(),‘贴吧‘)]"));
element.click();

}

/**
* 2. XPath模糊匹配:starts-with以什么开头
*/
public static void XpathByAsBegin() {
WebDriver driver = new ChromeDriver();
String url = "http://www.baidu.com";
driver.get(url);
driver.manage().window().maximize();
//XPath通过两个属性定位百度搜索输入框,并输入“@id and @autocomplete”
WebElement element = driver.findElement(By.xpath("//*[@id = ‘kw‘ and @autocomplete = ‘off‘]"));
element.sendKeys("通过@id and @autocomplete定位");
//输入后点击百度一下按钮进行查询
WebElement element2 = driver.findElement(By.xpath("//*[@id = ‘su‘]"));
element2.click();

try {
//页面等待
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//XPath通过以w开头模糊查询属性name,一般是第1个,所以选择了name属性,并输入“starts-with(@name,‘w‘)”
WebElement element3 = driver.findElement(By.xpath("//*[starts-with(@name,‘w‘)]"));
element3.clear();
element3.sendKeys("通过starts-with(@name,‘w‘)清空关键字后,并输入此句内容");

try {
//页面等待
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//10秒后清空关键字
element3.clear();

}

/**
* 3.1 XPath模糊匹配:ends-with以什么结尾,暂未测试通过,原因:可能是标准的XPath 2.0功能。 它在XPath 1.0中不可用
*/
public static void XpathByAsEndsWith() {
WebDriver driver = new ChromeDriver();
String url = "http://www.baidu.com";
driver.get(url);
driver.manage().window().maximize();
//XPath通过ends-with以什么结尾定位百度搜索输入框,并输入“ends-with定位,暂未测试通过定位
WebElement element = driver.findElement(By.xpath("//*[ends-with(@class,‘quickdelete-wrap‘)]/input"));
element.sendKeys("ends-with定位");

}


/**
* 3.2 XPath模糊匹配:matches以什么结果,,暂未测试通过,原因:可能是标准的XPath 2.0功能。 它在XPath 1.0中不可用
*/
public static void XpathByAsEndMatch() {
WebDriver driver = new ChromeDriver();
String url = "http://www.baidu.com";
driver.get(url);
driver.manage().window().maximize();
//XPath通过层级定位百度搜索输入框,并输入“XPath通过定位它爷爷来定位input输入框
WebElement element = driver.findElement(By.xpath("//*[matches(text(),‘地图‘)]"));
element.click();

}



}

selenium实战:xpath定位实战

上一篇:编译安装升级openssh最新版


下一篇:使用systemd-resolved的系统中DNS来源优先级