java selenium 罕見web UI 元素操作及API應用。本站提示廣大學習愛好者:(java selenium 罕見web UI 元素操作及API應用)文章只能為提供參考,不一定能成為您想要的結果。以下是java selenium 罕見web UI 元素操作及API應用正文
本篇引見我們若何應用selenium 來操作各類頁面元素
浏覽目次
鏈接(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("坦克"));
// 點擊鏈接
link1.click();
輸出框 textbox
<div> <p>輸出框 testbox</p> <input type="text" id="usernameid" value="username" /> </div>
輸出框的操作
// 找到元素
WebElement element = driver.findElement(By.id("usernameid"));
// 在輸出框中輸出內容
element.sendKeys("test111111");
// 清空輸出框
element.clear();
// 獲得輸出框的內容
element.getAttribute("value");
按鈕(Button)
<div> <p>按鈕 button</p> <input type="button" value="添加" id="proAddItem_0" /> </div>
找到按鈕元素
//找到按鈕元素 String xpath="//input[@value='添加']"; WebElement addButton = driver.findElement(By.xpath(xpath)); // 點擊按鈕 addButton.click(); // 斷定按鈕能否enable addButton.isEnabled();
下拉選擇框(Select)
<div> <p>下拉選擇框框 Select</p> <select id="proAddItem_kind" name="kind"> <option value="1">電腦硬件</option> <option value="2">房產</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")));
// 選擇對應的選擇項, index 從0開端的
select.selectByIndex(2);
select.selectByValue("18");
select.selectByVisibleText("品種AA");
// 獲得一切的選項
List<WebElement> options = select.getOptions();
for (WebElement webElement : options) {
System.out.println(webElement.getText());
}
單選按鈕(Radio Button)
<div> <p>單選項 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>
單選項元素的操作
// 找到單選框元素
String xpath="//input[@type='radio'][@value='Apple']";
WebElement apple = driver.findElement(By.xpath(xpath));
//選擇某個單選框
apple.click();
//斷定某個單選框能否曾經被選擇
boolean isAppleSelect = apple.isSelected();
// 獲得元素屬性
apple.getAttribute("value");
多選框 check box
<div> <p>多選項 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 罕見web UI 元素操作的材料整頓,後續持續彌補,感謝年夜家對本站的支撐!