我正在尝试从字符串数组列表中选择页面上的复选框。例如,我的数组包含[wva-00:04:F3:12:A7:2E,wva-00:04:F3:12:A6:F7,wva-00:04:F3:12:A6:90] >
我已经测试了该程序,它正在for循环和if语句中进行,所以我知道它正在从getPageSource中查找文本。但是使用下面的xpath会引发此错误。没有此类元素:无法找到元素:{“ method”:“ xpath”,“ selector”:“ // label [text()='wva-00:04:F3:12:A7:2E']”}。请让我知道我在做什么错。另外请注意,我不能使用id(例如“ marked_439”)为每个页面生成不同的ID,并且它们的编号不会相同。
for(int i = 0; i < edcNumbers.size(); i++)
if(driver.getPageSource().contains(edcNumbers.get(i)))
driver.findElement(By.xpath("//label[text()='"+edcNumbers.get(i)+"']/preceding-sibling::input[@type='checkbox']")).click();
</tr>
</thead>
<tbody class="yui-dt-body">
<tr class="yui-dt-odd">
<td id="yui-0-0" headers="yui-header-0" class="yui-dt-col-"><input type="checkbox" id="marked_439" name="439"></td>
<td id="yui-0-1" headers="yui-header-1" class="yui-dt-col-ALPHA">wva-00:04:F3:12:A7:2E</td>
<td id="yui-0-2" headers="yui-header-2" class="yui-dt-col-ALPHA">imiprairie</td>
<td id="yui-0-3" headers="yui-header-3" class="yui-dt-col-NUMERIC"></td>
<td id="yui-0-4" headers="yui-header-4" class="yui-dt-col-ALPHA"></td>
<td id="yui-0-5" headers="yui-header-5" class="yui-dt-col-NUMERIC"></td>
<td id="yui-0-6" headers="yui-header-6" class="yui-dt-col-ALPHA">2018-07-25</td>
<td id="yui-0-7" headers="yui-header-7" class="yui-dt-col-ALPHA">2018-06-18</td>
<td id="yui-0-8" headers="yui-header-8" class="yui-dt-col-ALPHA">2018-11-01</td>
<td id="yui-0-9" headers="yui-header-9" class="yui-dt-col-ALPHA"></td>
<td id="yui-0-10" headers="yui-header-10" class="yui-dt-col-ALPHA">203</td>
<td id="yui-0-11" headers="yui-header-11" class="yui-dt-col-ALPHA">0004F3:12A72E</td>
<td id="yui-0-12" headers="yui-header-12" class="yui-dt-col-ALPHA"></td>
<td id="yui-0-13" headers="yui-header-13" class="yui-dt-col-ALPHA"></td>
<td id="yui-0-14" headers="yui-header-14" class="yui-dt-col-edit">
<a rel="modal" href="settings/edc/mod/439" title="Edit">
<button></button>
</a>
</td>
<td id="yui-0-15" headers="yui-header-15" class="yui-dt-col-recycle">
<a rel="modal" href="settings/edc/rma/439" title="RMA">
<button></button>
</a>
</td>
<td id="yui-0-16" headers="yui-header-16" class="yui-dt-col-delete yui-dt-last">
<a rel="modal" href="settings/edc/del/439" title="Delete Confirmation">
<button></button>
</a>
</td>
</tr>
<tr class="yui-dt-even">
<td id="yui-1-0" headers="yui-header-0" class="yui-dt-col-"><input type="checkbox" id="marked_440" name="440"></td>
<td id="yui-1-1" headers="yui-header-1" class="yui-dt-col-ALPHA">wva-00:04:F3:12:A6:F7</td>
<td id="yui-1-2" headers="yui-header-2" class="yui-dt-col-ALPHA">imiprairie</td>
<td id="yui-1-3" headers="yui-header-3" class="yui-dt-col-NUMERIC"></td>
<td id="yui-1-4" headers="yui-header-4" class="yui-dt-col-ALPHA"></td>
<td id="yui-1-5" headers="yui-header-5" class="yui-dt-col-NUMERIC"></td>
<td id="yui-1-6" headers="yui-header-6" class="yui-dt-col-ALPHA">2018-07-25</td>
<td id="yui-1-7" headers="yui-header-7" class="yui-dt-col-ALPHA">2018-06-18</td>
<td id="yui-1-8" headers="yui-header-8" class="yui-dt-col-ALPHA">2018-11-01</td>
<td id="yui-1-9" headers="yui-header-9" class="yui-dt-col-ALPHA"></td>
<td id="yui-1-10" headers="yui-header-10" class="yui-dt-col-ALPHA">203</td>
<td id="yui-1-11" headers="yui-header-11" class="yui-dt-col-ALPHA">0004F3:12A6F7</td>
<td id="yui-1-12" headers="yui-header-12" class="yui-dt-col-ALPHA"></td>
<td id="yui-1-13" headers="yui-header-13" class="yui-dt-col-ALPHA"></td>
<td id="yui-1-14" headers="yui-header-14" class="yui-dt-col-edit">
<a rel="modal" href="settings/edc/mod/440" title="Edit">
<button></button>
</a>
</td>
<td id="yui-1-15" headers="yui-header-15" class="yui-dt-col-recycle">
<a rel="modal" href="settings/edc/rma/440" title="RMA">
<button></button>
</a>
</td>
<td id="yui-1-16" headers="yui-header-16" class="yui-dt-col-delete yui-dt-last">
<a rel="modal" href="settings/edc/del/440" title="Delete Confirmation">
<button></button>
</a>
</td>
</tr>
<tr class="yui-dt-odd">
<td id="yui-2-0" headers="yui-header-0" class="yui-dt-col-"><input type="checkbox" id="marked_441" name="441"></td>
<td id="yui-2-1" headers="yui-header-1" class="yui-dt-col-ALPHA">wva-00:04:F3:12:A6:90</td>
<td id="yui-2-2" headers="yui-header-2" class="yui-dt-col-ALPHA">imiprairie</td>
<td id="yui-2-3" headers="yui-header-3" class="yui-dt-col-NUMERIC"></td>
<td id="yui-2-4" headers="yui-header-4" class="yui-dt-col-ALPHA"></td>
<td id="yui-2-5" headers="yui-header-5" class="yui-dt-col-NUMERIC"></td>
<td id="yui-2-6" headers="yui-header-6" class="yui-dt-col-ALPHA">2018-07-25</td>
<td id="yui-2-7" headers="yui-header-7" class="yui-dt-col-ALPHA">2018-06-18</td>
<td id="yui-2-8" headers="yui-header-8" class="yui-dt-col-ALPHA">2018-11-01</td>
<td id="yui-2-9" headers="yui-header-9" class="yui-dt-col-ALPHA"></td>
<td id="yui-2-10" headers="yui-header-10" class="yui-dt-col-ALPHA">203</td>
<td id="yui-2-11" headers="yui-header-11" class="yui-dt-col-ALPHA">0004F3:12A690</td>
<td id="yui-2-12" headers="yui-header-12" class="yui-dt-col-ALPHA"></td>
<td id="yui-2-13" headers="yui-header-13" class="yui-dt-col-ALPHA"></td>
<td id="yui-2-14" headers="yui-header-14" class="yui-dt-col-edit">
<a rel="modal" href="settings/edc/mod/441" title="Edit">
<button></button>
</a>
</td>
<td id="yui-2-15" headers="yui-header-15" class="yui-dt-col-recycle">
<a rel="modal" href="settings/edc/rma/441" title="RMA">
<button></button>
</a>
</td>
<td id="yui-2-16" headers="yui-header-16" class="yui-dt-col-delete yui-dt-last">
<a rel="modal" href="settings/edc/del/441" title="Delete Confirmation">
<button></button>
</a>
</td>
答案 0 :(得分:0)
尝试使用ID定位元素,而不是使用XPath:
driver.findElement(By.id("yui-0-1")).click();
我认为这就是您想要抓住的要素。
答案 1 :(得分:0)
感谢所有帮助人员,你们都为找到解决方案做出了贡献。通过找到数组中的文本,然后获取其ID并将其添加到其他列表中,我可以使用其他方法解决此问题。由于复选框ID仅相差一个数字(即yui-0-1到yui-0-0),因此我仅更改了字符串以匹配复选框ID,以便选择它。
wvaIDs.add( driver.findElement(By.xpath("//*[contains(text(), '"+edcNumbers.get(i)+"')]")).getAttribute("id"));
char numToChange = wvaIDs.get(i).charAt(wvaIDs.get(i).length()-1);
int a=Integer.parseInt(String.valueOf(numToChange));
a--;
String newName = wvaIDs.get(i).substring(0,6)+a;
driver.findElement(By.id(newName)).click();