selenium-webdriver八種定位元素的常用方式

來源:互聯網
上載者:User

標籤:exception   常用   ext.get   rup   div   部分   元素   bdr   tle   

         /*<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();

         }

selenium-webdriver八種定位元素的常用方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.