2014-09-18 15 views
0

我打算獲取屬性type="radio"但我不知道如何在硒webdriver中。 我嘗試用試圖獲得Selenium Webdriver中的特定屬性

if(driver.findElement(By.id("userStatusEnable")).getAttribute("type").equals("radio")) 

,並通過改變ID爲x-AUTO-210

<div id="userStatusEnable" class="x-form-check-wrap x-form-field x-component " role="presentation" style="position: relative;"> 
<input id="x-auto-210" class=" x-form-radio" type="radio" name="gxt.RadioGroup.5" style="position: relative; left: 0px; top: 4px;" tabindex="0" value="enabled" aria-describedby="x-auto-190" checked=""> 
<label class="x-form-cb-label" for="x-auto-210" htmlfor="x-auto-210" style="position: relative; left: 1px; top: 3px;">Enable</label> 
</div> 
+0

究竟是你想做些什麼?用'type =「radio」'查找元素,或者驗證在另一個選擇器中找到的元素是否具有'type =「radio」'? – Richard 2014-09-18 18:36:03

+0

是的,我試圖驗證該ID是類型的無線電。所以我必須確認,我現在正在處理的頁面上有2個單選按鈕,但我不知道如何獲取屬性。 – 2014-09-18 19:06:54

回答

1

一種可能的方法是使用findElements()和XPath的選擇找到input標籤與type="radio"

if(driver.findElements(By.xpath("//input[@type='radio']")).size() == 2) 
+0

似乎OP需要確保有兩個特定ID的無線電,那麼它將是:'if(driver.findElements(By.xpath(「// input [@ id ='x-auto-210'] [ @ type ='radio']「))。size()== 2)' – 2014-09-19 10:15:35

0

從你的問題,它聽起來像你想找到所有的輸入元素與編號x-auto-210和類型radio。你可以做到這一點與以下XPath:

"//input[@id='x-auto-210' and @type='radio']" 

我增加什麼XPath表達式的解釋是做

  1. //說,我們要搜索的所有元素
  2. input手段我們只對輸入元素感興趣
  3. []包含我們希望輸入匹配的條件(即ID爲x-auto-210,類型爲radio

如果您使用與硒findElements結合這個表達式,你應該能夠找到所需的元素

if (driver.findElements(By.XPath("//input[@id='x-auto-210' and @type='radio']")).size() == 2) { 
    //Do stuff 
} 
相關問題