2016-05-09 54 views
1

Im unsing Selenium自动化一些测试。我对硒有些新鲜。所以即时通讯尝试验证表单。有些字段带有星号,表示必填字段。我目前正在遍历所有可见的字段,然后检查前面的元素是否有标签。我还需要检查标签是否有星级。林不是很好这个xpath的东西,所以任何帮助将不胜感激。动态查找以前的sibling元素和java selenium xpath

for(int index = 0; index < optionOneinputList.size(); index++){ 
     if(optionOneinputList.get(index).isDisplayed()) { 
      optionOneListFinal.add(optionOneinputList.get(index).getAttribute("id")); 
      if(driver.findElement(By.xpath("//*[@id='" + optionOneinputList.get(index).getAttribute("id") + "']/preceding-sibling::label")) != null) { 
       System.out.println(driver.findElement(By.xpath("//*[@id='" + optionOneinputList.get(index).getAttribute("id") + "']/preceding-sibling::label"))); 
      } 

     } 
    } 

和我的HTML结构,这些结构中有些有明星类和一些不.:

<div> 
    <label></label><div class="star">*</div> 
    <input type="text"> 
</div> 
+0

你能完成HTML吗? 'id'属性只出现在你的代码中。 –

回答

0

可能这是你在找什么?

"//*[@id='" + optionOneinputList.get(index).getAttribute("id") + 
     "' and preceding-sibling::div[@class = 'star' ] ]/preceding-sibling::label"