Java 如何使用Selenium WebDriver单击两个相同的属性?

Java 如何使用Selenium WebDriver单击两个相同的属性?,java,button,xpath,selenium-webdriver,Java,Button,Xpath,Selenium Webdriver,在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-ic

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>

搜寻
注:只有一个搜索按钮,我搜索它每一个地方,只有一个,但它显示两个

如何在selenium web驱动程序中编写此代码?

firepath中的代码片段:

更新:

来自firepath的Html代码图像:


您可以使用XPath函数,例如:

  • position()
    返回元素在DOM中的位置
//按钮[@id='hdr\u problem\u task']/th[2]/button[position()=1]

  • last()
//按钮[@id='hdr\u problem\u task']/th[2]/button[last()]

  • 类似于first()的内容不存在,您可以使用索引来代替它:
//按钮[@id='hdr\u problem\u task']/th[2]/button[1]

如果
按钮
有一些文本,您也可以使用它:

//按钮[@id='hdr\u problem\u task']/th[2]/button[text()='button name']

或使用
contains()

//按钮[@id='hdr\u problem\u task']/th[2]/button[contains(text(),'button name')]

更新:

该按钮具有名称
Search
,您可以将XPath与-
contains()一起使用

还有一个小建议,别忘了未来的支持。而不是以下定位器:

//*[@id='hdr\u problem\u task']/th[2]/按钮

更好的办法是:

//按钮[@id='hdr\u problem\u task']/th[2]/按钮


您可以使用XPath函数,例如:

  • position()
    返回元素在DOM中的位置
//按钮[@id='hdr\u problem\u task']/th[2]/button[position()=1]

  • last()
//按钮[@id='hdr\u problem\u task']/th[2]/button[last()]

  • 类似于first()的内容不存在,您可以使用索引来代替它:
//按钮[@id='hdr\u problem\u task']/th[2]/button[1]

如果
按钮
有一些文本,您也可以使用它:

//按钮[@id='hdr\u problem\u task']/th[2]/button[text()='button name']

或使用
contains()

//按钮[@id='hdr\u problem\u task']/th[2]/button[contains(text(),'button name')]

更新:

该按钮具有名称
Search
,您可以将XPath与-
contains()一起使用

还有一个小建议,别忘了未来的支持。而不是以下定位器:

//*[@id='hdr\u problem\u task']/th[2]/按钮

更好的办法是:

//按钮[@id='hdr\u problem\u task']/th[2]/按钮


您可以使用标签的属性值来识别正确的搜索按钮,如下所示:

//th[@name='search'][1]/button/span[text()='Search']

让我知道它是否适用于您。

您可以使用标签的名称属性值来识别正确的搜索按钮,如下所示:

//th[@name='search'][1]/button/span[text()='Search']

让我知道它是否适合你。

你能分享
网站
Url或
Html
?试试:
//button[@id='hdr\u problem\u task']/th[2]/button[0]
//button[@id='hdr\u problem\u task']/th[2]/button[1]
//button[@id='hdr\u problem\u task']/th[2]/button[2]
在完成xpath后使用
索引
。例如,您可以使用其他选择器LinkText@Richardmadis看一看你能分享
站点
Url或
Html
?试试:
//button[@id='hdr\u problem\u task']/th[2]/button[0]
//button[@id='hdr\u problem\u task']/th[2]/button[1]
//button[@id='hdr\u problem\u task']/th[2]/button[2]
在完成xpath后使用
索引
。例如,您可以使用其他选择器LinkText@RichardMadsi看一看代码在firepath中可见,它有两个相同的结果:第一个结果:
Search
第二个结果:
Search
,但当我运行它时,它在Eclipse中不可见使用上述xpath运行代码时,您会看到什么异常/错误?代码在firepath中可见,并且有两个相同的结果结果:第一个结果:
Search
第二个结果:
Search
但当我运行它时,它在Eclipse中不可见。当使用上述xpath运行代码时,您会看到什么异常/错误?