selenium java maven 自動化測試(二) 頁面元素擷取與操作

來源:互聯網
上載者:User

標籤:static   font   自動化測試   selenium   sele   []   rom   測試   system   

在第一節中,我們已經成功開啟了頁面,但是自動化測試必然包含了表單的填寫與按鈕的點擊. 所以在第二章中我以部落格園為例,完成按鈕點擊,表單填寫

 

還是以代碼為準,先上代碼:

package com.ryan;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.chrome.ChromeDriver;public class Demo {    public static void main(String[] args) {        System.setProperty("webdriver.chrome.driver", ".\\tools\\chromedriver.exe");        WebDriver driver = new ChromeDriver();        driver.get("http://www.cnblogs.com/ryan255/");        System.out.println(driver.getTitle());

     //擷取頁面上的按鈕 WebElement button = driver.findElement(By.cssSelector("#blog_nav_admin"));
     //點擊按鈕 button.click();
     //擷取頁面上的輸入框   WebElement input1 = driver.findElement(By.cssSelector("#input1"));
     //清理輸入框的內容 input1.clear();
     //輸入 input1.sendKeys("ryan255");
     Thread.sleep(5000);  driver.quit(); }}

如上面的注釋所示這裡的代碼增加了擷取頁面元素、點擊按鈕、輸入表單的操作。

 

Selenium提供了8種定位方式:
  • id
  • name
  • class name
  • tag name
  • link text
  • partial link text
  • xpath
  • css selector
這8種定位方式在代碼中所對應的方法為:
  • findElement(By.id())
  • findElement(By.name())
  • findElement(By.className())
  • findElement(By.tagName())
  • findElement(By.linkText())
  • findElement(By.partialLinkText())
  • findElement(By.xpath())
  • findElement(By.cssSelector())

selenium java maven 自動化測試(二) 頁面元素擷取與操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.