2014-09-18 49 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 
} 
相关问题