Selenium 3+Java自动化-xpath定位
Posted 上海-玉面小飞龙
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Selenium 3+Java自动化-xpath定位相关的知识,希望对你有一定的参考价值。
package com.mypro.jase; import java.net.URL; import java.security.cert.PKIXRevocationChecker.Option; import java.util.concurrent.TimeUnit; import javax.xml.bind.annotation.XmlID; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class FindByXPath { public static void main(String[] args) { WebDriver driver =new FirefoxDriver(); driver.get("http://www.baidu.com"); driver.manage().window().maximize(); //一、XPath常用的定位方法 /XPath通过id属性定位 WebElement element = driver.findElement(By.xpath("//*[@id = \'kw\']")); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); element.sendKeys("XPath通过id属性定位"); //XPath通过name属性定位 WebElement element = driver.findElement(By.xpath("//*[@name = \'wd\']")); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); element.sendKeys("XPath通过name属性定位"); //XPath通过class属性定位 WebElement element = driver.findElement(By.xpath("//*[@class = \'s_ipt\']")); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); element.sendKeys("XPath通过class属性定位"); //二、XPath:其它属性 //XPath用其它属性定位 WebElement element = driver.findElement(By.xpath("//*[@autocomplete = \'off\']")); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); element.sendKeys("XPath通过其他属性定位"); //三、XPath:标签 //XPath通过标签定位:属性为id WebElement element = driver.findElement(By.xpath("//input[@id = \'kw\']")); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); element.sendKeys("XPath通过标签定位:属性为id"); //XPath通过标签定位:属性为name WebElement element = driver.findElement(By.xpath("//input[@name = \'wd\']")); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); element.sendKeys("XPath通过标签定位:属性为name"); //XPath通过标签定位:属性为class WebElement element = driver.findElement(By.xpath("//input[@class = \'s_ipt\']")); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); element.sendKeys("XPath通过标签定位:属性为class"); //XPath通过标签定位:其它属性 WebElement element = driver.findElement(By.xpath("//input[@autocomplete = \'off\']")); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); element.sendKeys("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:索引 //selenium的索引是从1开始的 String Url2 = "https://passport.cnblogs.com/user/signin?ReturnUrl=http://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("input1")); name.sendKeys("814639416@qq.com"); WebElement PassWord = driver.findElement(By.id("input2")); PassWord.sendKeys("steven1990@"); WebElement login = driver.findElement(By.id("signin")); login.click(); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); WebElement option = driver.findElement(By.xpath("//select[@id = \'s1\']/option[2]")); option.click(); //六、XPath:逻辑运算 //1.XPath还有一个比较强的功能,是可以多个属性逻辑运算的,可以支持与(and)、或(or)、非(not) //2.一般用的比较多的是and运算,同时满足两个属性 WebElement element = driver.findElement(By.xpath("//*[@id = \'kw\' and @autocomplete = \'off\']")); element.sendKeys("逻辑运算"); //七、XPath:模糊匹配 //XPath模糊匹配某个属性:@name WebElement element = driver.findElement(By.xpath("//*[contains(@name,\'hao123\')]")); element.click(); //XPath模糊匹配 WebElement element = driver.findElement(By.xpath("//*[contains(text(),\'hao123\')]")); element.click();*/ //XPath模糊匹配:以什么开头 WebElement element = driver.findElement(By.xpath("//*[starts-with(@id,\'s_kw_\')]")); element.clear(); //XPath模糊匹配:以什么结果 WebElement element = driver.findElement(By.xpath("//*[ends-with(@class,\'quickdelete-wrap\')]/input")); element.sendKeys("1243243"); WebElement element = driver.findElement(By.xpath("//*[matchs(text(),\'hao13\')]")); element.click(); } }
以上是关于Selenium 3+Java自动化-xpath定位的主要内容,如果未能解决你的问题,请参考以下文章
解决selenium自动化,消息弹窗3秒消失,无法定位元素问题。
Python3-Selenium自动化测试框架之xpath元素定位