【发布时间】:2015-12-23 04:28:47
【问题描述】:
我在 Java 中使用 Selenium 来测试 webapp 中复选框的检查。这是我的代码:
WebElement checkBox1;
checkBox1 = driver.findElement((By.xpath(xpathOfElement)));
checkBox1.click();
Selenium Webdriver 找到复选框的位置,但由于某种原因它没有点击。只有复选框区域变为灰色,但复选框不会处于活动状态。
这是我的 HTML 的相关部分:
<div id="componentIndicators:1:j_idt2989:sbc" class="ui-chkbox ui-widget" style="position:relative; top:5px;">
<div class="ui-helper-hidden-accessible">
<input id="componentIndicators:1:j_idt2989:sbc_input" type="checkbox" onchange="PrimeFaces.ab({s:"componentIndicators:1:j_idt2989:sbc",e:"change",f:"newApplicationForm",p:"componentIndicators:1:j_idt2989:sbc",u:"componentIndicators:1:j_idt2989:wrapper",ps:true});" aria-checked="true" name="componentIndicators:1:j_idt2989:sbc_input">
</div>
<div class="ui-chkbox-box ui-widget ui-corner-all ui-state-default ui-state-active">
<span class="ui-chkbox-icon ui-icon ui-c ui-icon-check"></span>
</div>
</div>
如何在 Selenium WebDriver 中使用 Java 解决这个问题?将不胜感激。
活动复选框 HTML 如下所示:
<div class="ui-chkbox-box ui-widget ui-corner-all ui-state-default ui-state-active">
<span class="ui-chkbox-icon ui-icon ui-icon-check ui-c"></span>
</div>
非活动复选框 HTML 如下所示
<div class="ui-chkbox-box ui-widget ui-corner-all ui-state-default">
<span class="ui-chkbox-icon ui-icon ui-c ui-icon-blank"></span>
</div>
【问题讨论】:
-
你能分享那个元素的 xpath 吗?
-
当然。
xpath1 = "/html/body/div[1]/div[2]/form/div[3]/div/div[2]/div/div[1]/div/div/div[2]/div/div/div/div/div/div/ul/li[2]/div/div/fieldset/div/div[1]/div[2]/span"; -
我认为这不是写xpath的方法。因为当开发人员更改任何元素时,您肯定会得到 No such Element 异常。
-
您可以手动选中和取消选中复选框并每次发布 html 吗?我认为复选框的“状态”将在 div 元素的属性中发生变化,让我们看看
-
活动复选框
<div class="ui-chkbox-box ui-widget ui-corner-all ui-state-default ui-state-active"> <span class="ui-chkbox-icon ui-icon ui-icon-check ui-c"></span> </div>非活动复选框<div class="ui-chkbox-box ui-widget ui-corner-all ui-state-default"> <span class="ui-chkbox-icon ui-icon ui-c ui-icon-blank"></span> </div>