java selenium 常見(jiàn)web UI 元素操作及API使用
本篇介紹我們?nèi)绾卫胹elenium 來(lái)操作各種頁(yè)面元素
閱讀目錄
- 鏈接(link)
- 輸入框 textbox
- 按鈕(Button)
- 下拉選擇框(Select)
- 單選按鈕(Radio Button)
- 多選框 check box
鏈接(link)
<div> <p>鏈接 link</p> <a href="www.cnblogs.com/tankxiao">小坦克</a> </div>
鏈接的操作
// 找到鏈接元素
WebElement link1 = driver.findElement(By.linkText("小坦克"));
WebElement link11 = driver.findElement(By.partialLinkText("坦克"));
// 點(diǎn)擊鏈接
link1.click();
輸入框 textbox
<div> <p>輸入框 testbox</p> <input type="text" id="usernameid" value="username" /> </div>
輸入框的操作
// 找到元素
WebElement element = driver.findElement(By.id("usernameid"));
// 在輸入框中輸入內(nèi)容
element.sendKeys("test111111");
// 清空輸入框
element.clear();
// 獲取輸入框的內(nèi)容
element.getAttribute("value");
按鈕(Button)
<div> <p>按鈕 button</p> <input type="button" value="添加" id="proAddItem_0" /> </div>
找到按鈕元素
//找到按鈕元素 String xpath="http://input[@value='添加']"; WebElement addButton = driver.findElement(By.xpath(xpath)); // 點(diǎn)擊按鈕 addButton.click(); // 判斷按鈕是否enable addButton.isEnabled();
下拉選擇框(Select)
<div> <p>下拉選擇框框 Select</p> <select id="proAddItem_kind" name="kind"> <option value="1">電腦硬件</option> <option value="2">房產(chǎn)</option> <option value="18">種類AA</option> <option value="19">種類BB</option> <option value="20">種類BB</option> <option value="21">種類CC</option> </select> </div>
下拉選擇框的操作
// 找到元素
Select select = new Select(driver.findElement(By.id("proAddItem_kind")));
// 選擇對(duì)應(yīng)的選擇項(xiàng), index 從0開(kāi)始的
select.selectByIndex(2);
select.selectByValue("18");
select.selectByVisibleText("種類AA");
// 獲取所有的選項(xiàng)
List<WebElement> options = select.getOptions();
for (WebElement webElement : options) {
System.out.println(webElement.getText());
}
單選按鈕(Radio Button)
<div> <p>單選項(xiàng) Radio Button</p> <input type="radio" value="Apple" name="fruit>" />Apple <input type="radio" value="Pear" name="fruit>" />Pear <input type="radio" value="Banana" name="fruit>" />Banana <input type="radio" value="Orange" name="fruit>" />Orange </div>
單選項(xiàng)元素的操作
// 找到單選框元素
String xpath="http://input[@type='radio'][@value='Apple']";
WebElement apple = driver.findElement(By.xpath(xpath));
//選擇某個(gè)單選框
apple.click();
//判斷某個(gè)單選框是否已經(jīng)被選擇
boolean isAppleSelect = apple.isSelected();
// 獲取元素屬性
apple.getAttribute("value");
多選框 check box
<div> <p>多選項(xiàng) checkbox</p> <input type="checkbox" value="Apple" name="fruit>" />Apple <input type="checkbox" value="Pear" name="fruit>" />Pear <input type="checkbox" value="Banana" name="fruit>" />Banana <input type="checkbox" value="Orange" name="fruit>" />Orange </div>
多選框的操作和單選框一模一樣的, 這里就不再講了。
以上就是java selenium 常見(jiàn)web UI 元素操作的資料整理,后續(xù)繼續(xù)補(bǔ)充,謝謝大家對(duì)本站的支持!
相關(guān)文章
java計(jì)算機(jī)器人的運(yùn)動(dòng)范圍
這篇文章主要為大家詳細(xì)介紹了java計(jì)算機(jī)器人運(yùn)動(dòng)范圍的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
Spring遠(yuǎn)程調(diào)用HttpClient/RestTemplate的方法
這篇文章主要介紹了Spring遠(yuǎn)程調(diào)用HttpClient/RestTemplate的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
Redisson延遲隊(duì)列執(zhí)行流程源碼解析
這篇文章主要為大家介紹了Redisson延遲隊(duì)列執(zhí)行流程源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Java使用動(dòng)態(tài)規(guī)劃算法思想解決背包問(wèn)題
背包問(wèn)題(Knapsack problem)是一種組合優(yōu)化的NP完全問(wèn)題。問(wèn)題可以描述為:給定一組物品,每種物品都有自己的重量和價(jià)格,在限定的總重量?jī)?nèi),我們?nèi)绾芜x擇,才能使得物品的總價(jià)格最高2022-04-04
使用lombok的@Data會(huì)導(dǎo)致棧溢出StackOverflowError問(wèn)題
這篇文章主要介紹了使用lombok的@Data會(huì)導(dǎo)致棧溢出StackOverflowError問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11

