Selenium+java - 下拉框处理
生活随笔
收集整理的這篇文章主要介紹了
Selenium+java - 下拉框处理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
常見下拉框也分兩種:一種是標準控件和非標準控件(一般為前端開發人員自己封裝的下拉框),本篇文章中將重點講解標準下拉框操作。
1、Select提供了三種選擇某一項的方法
- select.selectByIndex # 通過索引定位
- selectByValue # 通過value值定位
- selectByVisibleText # 通過可見文本值定位
使用說明:
- index索引是從“0”開始;
- value是option標簽中value屬性值定位;
- VisibleText是在option是顯示在下拉框的文本;
2、Select提供了三種返回options信息的方法
- getOptions() # 返回select元素所有的options
- getAllSelectedOptions() # 返回select元素中所有已選中的選項
- getFirstSelectedOption() # 返回select元素中選中的第一個選項
3、Select提供了四種取消選中項的方法
- select.deselectAll() # 取消全部的已選擇項
- deselectByIndex() # 取消已選中的索引項
- deselectByValue() # 取消已選中的value值
- deselectByVisibleText() # 取消已選中的文本值
4、被測頁面代碼
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Select控件練習案例</title> </head> <body><h4>請選擇你的英雄:</h4> <select id="select"><option value="1">李白</option><option selected="selected" value="2">韓信</option><option value="3">典韋</option><option value="4">凱</option> </select> </body> </html>具體示例代碼如下:
package com.brower.demo;import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.Select; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test;import java.util.concurrent.TimeUnit;public class TestSelectDemo {WebDriver driver;@BeforeClasspublic void beforeClass() {System.setProperty("webdriver.chrome.driver", "driver/chromedriver.exe");driver = new ChromeDriver();}@Testpublic void testSelectDemo() {//打開測試頁面driver.get("file:///C:/Users/Administrator/Desktop/SelectDemo.html");driver.manage().window().maximize();driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);//獲取select元素對象WebElement element = driver.findElement(By.id("select"));Select select = new Select(element);//根據索引選擇,選擇第1個英雄:李白select.selectByIndex(0);//根據value值選擇第4個英雄:凱select.selectByValue("4");//根據文本值選擇第2個英雄:韓信select.selectByVisibleText("韓信");//判斷是否支持多選 System.out.println(select.isMultiple());}@AfterClasspublic void afterClass() {driver.quit();} }以上便是關于select控件處理的演示案例,具體實踐還需要結合實際的工作需要來進行。
轉載于:https://www.cnblogs.com/longronglang/p/11285956.html
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的Selenium+java - 下拉框处理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 洛谷 P3372 【模板】线段树 1(线
- 下一篇: 多重for循环如何提速