国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java selenium 常見web UI 元素操作及API使用

2019-11-26 13:59:11
字體:
來源:轉載
供稿:網友

本篇介紹我們如何利用selenium 來操作各種頁面元素

閱讀目錄

  1. 鏈接(link)
  2. 輸入框 textbox
  3. 按鈕(Button)
  4. 下拉選擇框(Select)
  5. 單選按鈕(Radio Button)
  6. 多選框 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("坦克"));    // 點擊鏈接  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="http://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="http://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 元素操作的資料整理,后續繼續補充,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 古浪县| 阿坝县| 澄迈县| 同德县| 镇江市| 新兴县| 太康县| 泸溪县| 潼关县| 哈巴河县| 凤台县| 永吉县| 汉沽区| 瑞安市| 左权县| 彭山县| 临泉县| 佛坪县| 连平县| 忻州市| 东方市| 洮南市| 三台县| 商都县| 乐陵市| 潼关县| 秦安县| 台中市| 绥滨县| 将乐县| 安庆市| 嘉兴市| 庆阳市| 永宁县| 永靖县| 海盐县| 会理县| 五河县| 镇平县| 杂多县| 嘉义县|