标签:多个 open end xpath public log cli logs element
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自动化(5)】-xpath定位
标签:多个 open end xpath public log cli logs element
原文地址:http://www.cnblogs.com/stevenx/p/7376242.html