Webdriver学习笔记(三)元素的查找定位

来源:互联网 发布:php socket select 编辑:程序博客网 时间:2024/05/16 16:05
     打开测试页面后,接下去的操作就是去操作页面上的元素,例如点击,输入值等。然后想操作某个元素,我们必须先知道这个元素在哪?该怎么去定位查找?
     webdriver通过“by”方法进行查找。针对不同的页面,页面元素我们需要使用不同的查找方式:
  •  ById.id(id)  或者 By.id(id)
  •  ByName.name(name)   或者By.name(name)
  •  ByClassName.className(className))  或者By.className(className) 
  •  ByLinkText.linkText(linkText)   或者 By.linkText(链接文本)
  •  ByPartialLinkText.partialLinkText(linkText)  或者 By.partialLinkText(部分链接文本)                                                                                     
  •  ByCssSelector.cssSelector(selector)   或者 By.cssSelector(Css路径)
  •  ByTagName.tagName(name)   或者 By.tagName(name)
  •  ByXpath.xpath(xpathExpression)  或者By.xpath(XPath路径)

 1.通过ID查找
          ID查找最有效,也是最快的,如果一个dom节点有ID,那用它是最好的定位方式,而且ID基本上唯一。
          ByID.id("id属性值") 或者 By.id(id)

     例子(百度首页,点击“登陆”):
     HTML 源码:
          <a onclick="return false;" id="lb" name="tj_login" href="https://passport.baidu.com/v2/?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2F">登录</a>
     得知id属性的值为“lb”
import org.junit.Test;import org.openqa.selenium.By.*;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.chrome.ChromeDriver;public class By {        /**        * 通过ID定位        *        */        @Test        public void byID() throws InterruptedException {               // 设置chromedriver的路径,根据你具体存放位置来设置路径              System. setProperty("webdriver.chrome.driver", "C:\\holmosconf\\driverServers\\chromedriver.exe" );               // 启动Chrome浏览器              WebDriver driver = new ChromeDriver();               // get方式打开百度首页              driver.get( "http://www.baidu.com" );               // 定位“登陆”标签              WebElement baiduLogin=driver.findElement(ById. id("lb"));               // click为点击操作              baiduLogin.click();               // 为了看效果,等待3S              Thread. sleep(3000);               // 结束测试              driver.quit();       }}

2.通过Name查找
           ByName.name("name属性值") 或者By.name(name)

     例子(百度首页,点击“注册”):
     HTML源码:
     <a class="reg" name="tj_reg" target="_blank" href="https://passport.baidu.com/v2/?reg&amp;regType=1&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2F">注册</a>
     得知name属性的值为“tj_reg”
     
import org.junit.Test;import org.openqa.selenium.By.*;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.chrome.ChromeDriver;public class By {        /**        * 通过Name定位        * @throws InterruptedException        */        @Test        public void byName() throws InterruptedException{               // 设置chromedriver的路径,根据你具体存放位置来设置路径              System. setProperty("webdriver.chrome.driver", "C:\\holmosconf\\driverServers\\chromedriver.exe" );               // 启动Chrome浏览器              WebDriver driver = new ChromeDriver();               // get方式打开百度首页              driver.get( "http://www.baidu.com" );               // 定位“注册”标签              WebElement baidu=driver.findElement(ByName. name("tj_reg"));               // 点击“注册”标签              baidu.click();               // 为了看效果,等待3S              Thread. sleep(3000);               // 结束测试              driver.quit();       }}
3.通过class属性查找
          ByClassName.className("class属性值")  或者By.className(className) 

     例子还是(百度首页,点击“注册”):
     HTML源码:
     <a class="reg" name="tj_reg" target="_blank" href="https://passport.baidu.com/v2/?reg&amp;regType=1&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2F">注册</a>
     得知class属性的值为“reg”

import org.junit.Test;import org.openqa.selenium.By.*;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.chrome.ChromeDriver;public class By {        /**        * 通过ClassName定位        * @throws InterruptedException        */        @Test        public void byClassName() throws InterruptedException {               // 设置chromedriver的路径,根据你具体存放位置来设置路径              System. setProperty("webdriver.chrome.driver", "C:\\holmosconf\\driverServers\\chromedriver.exe" );               // 启动Chrome浏览器              WebDriver driver = new ChromeDriver();               // get方式打开百度首页              driver.get( "http://www.baidu.com" );               // 定位“注册”标签              WebElement baidu = driver.findElement(ByClassName.className( "reg"));               // 点击“注册”标签              baidu.click();               // 为了看效果,等待3S              Thread. sleep(3000);               // 结束测试              driver.quit();       }
4.通过LinkText属性查找
          LinkText按链接的文本进行查找
          ByLinkText.linkText("文本值") 或者 By.linkText(链接文本)

     例子(百度首页,点击“搜索设置”):
     HTML源码:
     <a name="tj_setting" href="http://www.baidu.com/gaoji/preferences.html">搜索设置</a>
     得知LinkText文本的值为“搜索设置”
          
import org.junit.Test;import org.openqa.selenium.By.*;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.chrome.ChromeDriver;public class By {               /**        * 通过linkText定位        */        @Test        public void byLinkText() throws InterruptedException {               // 设置chromedriver的路径,根据你具体存放位置来设置路径              System. setProperty("webdriver.chrome.driver", "C:\\holmosconf\\driverServers\\chromedriver.exe" );               // 启动Chrome浏览器              WebDriver driver = new ChromeDriver();               // get方式打开百度首页              driver.get( "http://www.baidu.com" );               // 定位“搜索设置”标签              WebElement baidu = driver.findElement(ByLinkText.linkText( "搜索设置" ));               // 点击“搜索设置”标签              baidu.click();               // 为了看效果,等待3S              Thread. sleep(3000);               // 结束测试              driver.quit();       }}
5.通过PartialLinkText属性查找
        PartialLinkText按链接的部分文本进行查找
        ByPartialLinkText.partialLinkText("部分文本值") 或者 By.partialLinkText(部分链接文本)  

     例子(百度首页,点击“搜索设置”):
     HTML源码:
     <a name="tj_setting" href="http://www.baidu.com/gaoji/preferences.html">搜索设置</a>
     得知LinkText文本的值为“设置”
import org.junit.Test;import org.openqa.selenium.By.*;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.chrome.ChromeDriver;public class By {        /**        * 通过PartiallinkText定位        */        @Test        public void byPartialLinkText() throws InterruptedException {               // 设置chromedriver的路径,根据你具体存放位置来设置路径              System. setProperty("webdriver.chrome.driver", "C:\\holmosconf\\driverServers\\chromedriver.exe" );               // 启动Chrome浏览器              WebDriver driver = new ChromeDriver();               // get方式打开百度首页              driver.get( "http://www.baidu.com" );               // 定位“搜索设置”标签              WebElement baidu = driver.findElement(ByPartialLinkText.partialLinkText( "设置" ));               // 点击“搜索设置”标签              baidu.click();               // 为了看效果,等待3S              Thread. sleep(3000);               // 结束测试              driver.quit();       }}
6.通过CssSelector定位
     CssSelector按CSS路径地址查找定位。
     ByCssSelector.cssSelector(selector)  或者 By.cssSelector(Css路径)
例子(百度首页,点击“更多”标签)
 
import org.junit.Test;import org.openqa.selenium.By.*;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.chrome.ChromeDriver;public class By {        /**        * 通过CSS定位        */        @Test        public void byCssSelector() throws InterruptedException {               // 设置chromedriver的路径,根据你具体存放位置来设置路径              System. setProperty("webdriver.chrome.driver", "C:\\holmosconf\\driverServers\\chromedriver.exe" );               // 启动Chrome浏览器              WebDriver driver = new ChromeDriver();               // get方式打开百度首页              driver.get( "http://www.baidu.com" );               // 定位“更多”标签              WebElement baidu = driver.findElement(ByCssSelector.cssSelector( "#lk>span>a" ));               // 点击“更多”标签              baidu.click();               // 为了看效果,等待3S              Thread. sleep(3000);               // 结束测试              driver.quit();       } }
7.通过TagName定位
     TagName按dom节点名进行查找定位,并返回第一个匹配的dom节点。
     ByTag.cssSelector(selector) 或者 By.tagName(name)

例子:百度首页第一个<a></a>标签“搜索设置”
import org.junit.Test;import org.openqa.selenium.By.*;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.chrome.ChromeDriver;public class By {        /**        * 通过TagName定位        */        @Test        public void byTagName() throws InterruptedException {               // 设置chromedriver的路径,根据你具体存放位置来设置路径              System. setProperty("webdriver.chrome.driver", "C:\\holmosconf\\driverServers\\chromedriver.exe" );               // 启动Chrome浏览器              WebDriver driver = new ChromeDriver();               // get方式打开百度首页              driver.get( "http://www.baidu.com" );               // 定位到第一个<a>标签,为“搜索设置”              WebElement baidu = driver.findElement(ByTagName. tagName("a"));               // 点击“搜索设置”标签               baidu.click();               // 为了看效果,等待3S              Thread. sleep(3000);               // 结束测试              driver.quit();       }}
8.通过XPath定位   
XPath跟CSS定位有点类似,根据dom节点的相对或者绝对路径进行查找定位。(推荐使用火狐的插件FireBug和FirePath)
     ByXPath.XPath(XPath或者By.xpath(XPath路径)

例子:定位百度首页的“新闻”标签
import org.junit.Test;import org.openqa.selenium.By.*;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.chrome.ChromeDriver;public class By {         /**        * 通过XPath定位        */        @Test        public void byXPath () throws InterruptedException {               // 设置chromedriver的路径,根据你具体存放位置来设置路径              System. setProperty("webdriver.chrome.driver", "C:\\holmosconf\\driverServers\\chromedriver.exe" );               // 启动Chrome浏览器              WebDriver driver = new ChromeDriver();               // get方式打开百度首页              driver.get( "http://www.baidu.com" );               // 定位"新闻"标签              WebElement baidu = driver.findElement(ByXPath.xpath(".//*[@id='nv']/a[1]"));               // 点击“新闻”标签              baidu.click();               // 为了看效果,等待3S              Thread. sleep(3000);               // 结束测试              driver.quit();       }}

原创粉丝点击