在firepath中我看到两个相同的属性,firepath有两个结果。
下面是 firebug 中突出显示的 HTML 代码:
<button class="list_header_search_toggle icon-search btn btn-icon table-btn-lg" style="margin-left:0px">
下面是整个代码:
<button class="list_header_search_toggle icon-search btn btn-icon table-btn-lg" style="margin-left:0px">
<span class="sr-only">Search</span>
</button>
注意:只有 1 个搜索按钮,我到处搜索,只有 1 个,但它显示两个??
如何在 selenium Web 驱动程序中编写此代码?
来自 firepath 的片段:
更新:
Html 代码图像,来自 firepath:
最佳答案
您可以使用XPath 函数,例如:
position()
返回元素在 DOM 中的位置
//button[@id='hdr_problem_task']/th[2]/button[position()=1]
last()
//button[@id='hdr_problem_task']/th[2]/button[last()]
- 类似 first() 的东西不存在,您可以使用索引代替:
//button[@id='hdr_problem_task']/th[2]/button[1]
此外,如果按钮
有一些文本,您也可以使用它:
//button[@id='hdr_problem_task']/th[2]/button[text()='button name']
或使用contains()
//button[@id='hdr_problem_task']/th[2]/button[contains(text(), 'button name')]
更新:
该按钮的名称为 Search
,您可以将 XPath 与 - contains()
一起使用。
还有一个小建议,不要忘记 future 的支持。而不是以下定位器:
//*[@id='hdr_problem_task']/th[2]/button
更好的是:
//button[@id='hdr_problem_task']/th[2]/button
关于java - 如何使用 Selenium WebDriver 单击两个相同的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42926821/