zoukankan      html  css  js  c++  java
  • selenium-webdriver八种定位元素的常用方式

             /*<div id="divId">

           <input id="userid" type="text" value="liuhaixia" title="用户名"  name="userid" class="uid">

               <input id="password" type="password" value="123456" title="密码"  name="password">

               <a href="http://192.168.66.71:8080/dss" >超链接</a>

         </div>*/

             //getTagName()获得标签名称

             //getAttribute("属性名称")获得某属性的值

      public void locateOne() throws IOException{

                      WebDriver driver = new FirefoxDriver();

                       driver.get("http://192.168.2.128:8080/selenium/index.jsp");

                       //根据id

                       System.out.println("---------1、根据id定位---------");

                       WebElement eleById = driver.findElement(By.id("userid"));

                       System.out.println(eleById.getTagName());

                       System.out.println(eleById.getAttribute("title"));

                       //根据name

                       System.out.println("---------2、根据name定位---------");

                       WebElement eleByName = driver.findElement(By.name("userid"));

                       System.out.println(eleByName.getTagName());

                       System.out.println(eleByName.getAttribute("title"));

                       //根据css

                       System.out.println("---------3、根据css定位  id---------");

                       WebElement eleByCssId = driver.findElement(By.cssSelector("#userid"));

                       System.out.println(eleByCssId.getTagName());

                       System.out.println(eleByCssId.getAttribute("title"));

                       System.out.println("---------3、根据css定位  class---------");

                       WebElement eleByCssClass = driver.findElement(By.cssSelector(".uid"));

                       System.out.println(eleByCssClass.getTagName());

                       System.out.println(eleByCssClass.getAttribute("title"));

                       //根据tagName 如果有多个tagName,则获取的为第一个

                       System.out.println("---------4、根据tagName定位---------");

                       WebElement eleByTagName = driver.findElement(By.tagName("input"));

                       System.out.println(eleByTagName.getTagName());

                       System.out.println(eleByTagName.getAttribute("title"));

                       //根据className

                       System.out.println("---------5、根据className定位---------");

                       WebElement eleByClassName = driver.findElement(By.className("uid"));

                       System.out.println(eleByClassName.getTagName());

                       System.out.println(eleByClassName.getAttribute("title"));

                       //根据linkText 直接根据 <a>标签显示的内容定位

                       System.out.println("---------6、根据linkText定位---------");

                       WebElement eleByLinkText = driver.findElement(By.linkText("超链接"));

                       System.out.println(eleByLinkText.getTagName());

                       System.out.println(eleByLinkText.getAttribute("href"));

                      

                       //根据partialLinkText 直接根据 <a>标签显示的部分内容定位

                       System.out.println("---------7、根据partialLinkText定位 是linkText的 扩展---------");

                       WebElement eleByPartialLinkText = driver.findElement(By.partialLinkText("链接"));

                       System.out.println(eleByPartialLinkText.getText());

                       System.out.println(eleByPartialLinkText.getTagName());

                       System.out.println(eleByPartialLinkText.getAttribute("href"));

                      

                       //根据xpath定位 用firebug

                     System.out.println("---------8、根据xpath定位---------");

                       WebElement eleByXpath = driver.findElement(By.xpath("/html/body/form/div/input[1]"));

                       System.out.println(eleByXpath.getText());

                       System.out.println(eleByXpath.getTagName());

                       try {

                                Thread.sleep(3000);

                       } catch (InterruptedException e) {

                                // TODO Auto-generated catch block

                                e.printStackTrace();

                       }

                       driver.quit();

             }

  • 相关阅读:
    解决Maven下载速度缓慢问题
    IntelliJ IDEA 最新激活码
    Googel 浏览器 模拟发送请求工具--Advanced REST Client
    Firefox火狐 浏览器接口调试工具 JSON 格式化
    修复/lib/ld-linux.so.2: bad ELF interpreter: No such file or directory问题
    解决Nginx: [error] open() "/usr/local/Nginx/logs/Nginx.pid
    configure: error: You need a C++ compiler for C++ support.[系统缺少c++环境]
    解决编译apache出现的问题:configure: error: APR not found . Please read the documentation
    centos6 Linux安装redis 2.6.14
    Nginx+Tomcat负载均衡配置
  • 原文地址:https://www.cnblogs.com/liuhaixia/p/7120498.html
Copyright © 2011-2022 走看看