selenium判斷元素類型

來源:互聯網
上載者:User

標籤:省份   input   sendkeys   clean   add   img   com   end   code   

在做級聯的下拉框時發現第一次選擇了下拉框(如省份),第二個下拉框可能是輸入框,也可能是下拉框,這個時候就需要判斷他的元素類型,來做判斷

圖1

圖2

原理很簡單:擷取控制項的html檔案內容,拿到內容後在做判斷

圖1的html元素為:

<input type="text" name="province" class="choiceCountry inputText long js_choiceState" value="">

圖2的html元素為:

做法:

WebElement elem = driver.findElement(By.cssSelector("#addressfrom > ul > li:nth-child(7) > div > div"));String province = elem.getAttribute("innerHTML"); #擷取他的html檔案if (province.contains("select")) {op.getSelect("province", 20).selectByIndex(1);// 給下拉框賦值} else {op.loopSendKeysClean("province", "chengdu", 3, 20); 給文字檔輸入值}

 

selenium判斷元素類型

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.