1.簡介
在實際自動化測試過程中,我們也避免不了會遇到下拉選擇的測試,因此宏哥在這里直接分享和介紹一下,希望小伙伴或者童鞋們在以后工作中遇到可以有所幫助。
2.select 下拉框
2.1Select類
1.在Selenium中,針對html的標簽select多選下拉列表有幾種方法:
selectByIndex(index); //根據索引選擇 selectByValue(value); //根據value屬性選擇 selectByVisibleText(text); //根據選項文字選擇 注意的是: *index是從0開始的 **Value是option標簽的一個屬性值,並不是顯示在下拉框中的值 ***VisibleText是在option標簽中間的值,是顯示在下拉框的值
2.四種取消方法:
deselectByIndex(0); deselectByValue(value); deselectByVisibleText(Text); deselectAll(); //取消所有選中
3.下拉選的處理類:Select 如果頁面元素是一個下拉框,我們可以將此web元素封裝成Select對象。
Select select = new Select(WebElement element); //Select select = new Select(driver.findElement(By.id("xxx"))); //獲取所有選項的方法 select.getOptions(); //根據索引選中對應的元素 select.selectByIndex(index); //根據value值選中對應的選項 select.selectByValue(value); //根據文本值選中對應的選項 select.selectByVisibleText(text); //判斷是不是多選的選擇框返回boolean值 select.isMultiple(); //取消所有的選中 select.deselectAll();
3.select.html
1.准備測試練習select.html,如下:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>測試Select</title> <style type="text/css"> .button1 { background-color: #f44336; border: none; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 28px; margin-bottom: 100px; text-decoration:none; color: white; } #myAnchor { text-decoration:none; color: white; } </style> </head> <body> <button class="button1"><a id="myAnchor" href="https://www.cnblogs.com/du-hong/">北京-宏哥</a></button></br> 快遞郵寄地址: <select id="select_id" name="select_name" class ="select_cls"> <option value="0">請選擇</option> <option value="1">山西</option> <option value="2">陝西</option> <option value="3">山東</option> <option value="4">四川</option> <option value="5">河北</option> </select>省_XXX_市_ XXX_街道 </body> </html>
2.頁面效果,如下圖所示:
4.代碼實戰練習
4.1代碼設計
4.2參考代碼
package lessons; import java.util.List; 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.junit.Test; /** * @author 北京-宏哥 * * 《手把手教你》系列技巧篇(三十二)-java+ selenium自動化測試-select 下拉框(詳解教程) * * 2021年10月16日 */ public class SelectTest { @Test public void test() throws InterruptedException { System.setProperty("webdriver.gecko.driver", ".\\Tools\\chromedriver.exe"); WebDriver driver =null; driver =new ChromeDriver(); driver.get("file:///C:/Users/DELL/Desktop/test/select.html"); //Select select = new Select(WebElement element); Select select = new Select(driver.findElement(By.id("select_id"))); //獲取所有選項的方法 List<WebElement> lst = select.getOptions(); for (WebElement webElement : lst) { System.out.println("獲取所有選項的方法依次輸出文本值:"+webElement.getText()); } //根據索引選中對應的元素 select.selectByIndex(1); Thread.sleep(5000); //根據value值選中對應的選項 select.selectByValue("4"); Thread.sleep(5000); //根據文本值選中對應的選項 select.selectByVisibleText("山東"); Thread.sleep(5000); //判斷是不是多選的選擇框返回boolean值 System.out.println(select.isMultiple()); //取消所有的選中 select.deselectAll(); Thread.sleep(5000); } }
4.3運行代碼
1.運行代碼,右鍵Run AS->Junit Test,控制台輸出,如下圖所示:
2.運行代碼后電腦端的瀏覽器的動作,從視頻中可以看到選擇的值不斷地在修改變化(山西->四川->山東),如下小視頻所示:
5.新的select
宏哥發現隨着技術的更新換代,現在好多下拉選擇都很少用以前那種的方式,而是采用一種類似pop彈出的效果,直接彈出一個一個頁面選擇,如下圖所示:
12306網站:
快遞:
5.1項目實戰
宏哥這里就以12306網站的“出發地”選擇框給小伙伴們或者童鞋們來打個樣。
具體步驟:
1.首先訪問12306網站;
2.定位到出發站,點擊;
3.彈出選項,定位要選擇的選項,點擊即可。
5.2代碼設計
5.3參考代碼
package lessons; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; /** * @author 北京-宏哥 * * 《手把手教你》系列技巧篇(三十二)-java+ selenium自動化測試-select 下拉框(詳解教程) * * 2021年10月16日 */ public class Select { public static void main(String[] args) throws InterruptedException { System.setProperty("webdriver.gecko.driver", ".\\Tools\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.get("https://www.12306.cn/"); Thread.sleep(5000); //By id 定位 WebElement fromStationText = driver.findElement(By.id( "fromStationText" )); fromStationText.click(); Thread.sleep(2000); WebElement fromStation = driver.findElement(By.xpath("//div/ul/li[text()='北京']")); fromStation.click(); } }
5.4運行代碼
1.運行代碼,右鍵Run AS->java Application,控制台輸出,如下圖所示:
2.運行代碼后電腦端的瀏覽器的動作,從視頻中可以看到宏哥成功的將“出發站”選擇為“北京”,如下小視頻所示:
6.小結
其實無論哪種你只要定位到就可以操作實現自動化,只不過是以前的selenium封裝好select類,直接調用比較方便而已,好了時間不早了,今天就分享到這里!!!