Selenium2基礎API介紹

來源:互聯網
上載者:User

標籤:

輸入框:input

? 表現形式: 1. 在html中一般為:<input id="user" type="text">

? 主要操作:

1. driver.findElement(By. id("user")). sendKeys ("test ");

2. driver.findElement(By.id("user")).clear()

? 說明:

1. sendKeys代表輸入,參數為要輸入的值

2. clear代表清除輸入框中原有的資料 

1     public void testInput(String x)2     {3     //    注掉的部分是sendkeys沒有參數時可以這樣4     //    Driver.findElement(By.xpath(".//*[@id=‘kw‘]")).sendKeys(new String[]{"Selenium2培訓"});5         6     //使用這種方式,就可以把要使用的內容輸入到主函數裡的testinput裡了    7         Driver.findElement(By.xpath(".//*[@id=‘kw‘]")).sendKeys(new String[]{x});8         Driver.findElement(By.xpath(".//*[@id=‘su‘]")).click();9     }
View Code

 

超連結:a

? 表現形式:

1. 在html中一般為: <a class="baidu" href="http://www.baidu.com">baidu</a>

? 主要操作:

1. driver.findElement(By.xpath("//div[@id=‘link‘]/a")).click();

? 說明: 1. click代表點擊這個a連結 

1     public void testLink()2     {3         Driver.findElement(By.xpath(".//*[@id=‘1‘]/h3/a")).click();4     }
View Code

 

下拉式功能表:select

? 表現形式:

1. 在HTML中一般為:

<select name="select">

<option value="volvo">Volvo</option>

<option value="saab">Saab</option>

<option value="opel">Opel</option>

<option value="audi">Audi</option>

</select>

基本控制項介紹

? 主要操作:

WebElement element = driver.findElement(By.cssSelector("select[name=‘select‘]"));

Select select = new Select(element);

select.selectByValue("opel");

select.selectByIndex(2);

select.selectByVisibleText("Opel");

 

? 說明:

1. 需要一個Select的類

2. selectByValue的參數為option中的value屬性

3. selectByIndex的參數為option的順序

4. selectByVisibleText的參數為option的text值

 1     public void testOption() 2     { 3     //    WebElement elements=Driver.findElement(By.id("addr_province")); 4         WebElement elements=Driver.findElement(By.cssSelector("select[name=‘addr_province‘]")); 5         Select select=new Select(elements); 6         select.selectByValue("天津市"); 7     //    select.selectByIndex(5); 8     //    select.selectByVisibleText("福建省"); 9 10     //使用List遍曆11     /*12         List options=select.getOptions();13         int optionsize=options.size();14         int i;15         for(i=0;i<optionsize;i++)16         {17             select.selectByIndex(i);18         }19         */20     }
View Code

 

單選:radiobox

? 表現形式:

1. 在HTML中一般為:

<input class="Volvo" type="radio" name="identity">

? 主要操作:

List<WebElement> elements = driver.findElements(By.name("identity"));

elements.get(2).click(); boolean select = elements.get(2).isSelected();

 

? 說明:

1. click代表點擊選中這個單選框

2. isSelected代表檢查這個單選框有沒有被選中

1     public void testRadio()2     {3         //需考慮下用cssSeletor如何定位limit_area4         WebElement radio=Driver.findElement(By.id("limit_area"));5         radio.findElement(By.id("limitqy")).click();6     //    單選框是否被選定7     //    radio.isSelected();8     }
View Code

 

多選:checkbox

? 表現形式:

1. 在HTML中一般為:<input type="checkbox" name="checkbox1">

? 主要操作:

List<WebElement> elements = driver.findElements(By.xpath("//div[@id=‘checkbox‘]/input"));

WebElement element = elements.get(2);

element.click();

boolean check = element.isSelected();

? 說明:

1. click代表點擊選中這個多選框

2. isSelected代表檢查這個多選框有沒有被選中

 1     public void testCheckBox() 2     { 3     //定位可以再用css方式最佳化一下 4      5         //A 6     //    WebElement check=Driver.findElement(By.xpath(".//input[@type=‘checkbox‘]")); 7     //    check.findElement(By.id("shop_all")).click(); 8          9         //B10         List<WebElement> check=Driver.findElements(By.xpath(".//div[@class=‘inputArea‘]//input[@type=‘checkbox‘]"));11         int checksize=check.size();12         int i;13         for(i=0;i<checksize;i++)14         {15             WebElement element=(WebElement)check.get(i);16             element.click();17         }18         19     }
View Code

 

按鈕:button

? 表現形式:

1. 在HTML中,一般有兩種表現形式:

<input class="button" type="button" disabled="disabled" value="Submit">

<button class="button" disabled="disabled" > Submit</button>

? 主要操作:      

WebElement element = driver.findElement(By.className("button"));        

element.click();        

boolean button = element.isEnabled();

? 說明:

1. click代表點擊這個按鈕

2. isEnabled代表檢查這個按鈕是不是可用

 

Selenium2基礎API介紹

聯繫我們

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