简体   繁体   中英

how to click two identical attributes with Selenium WebDriver?

In firepath I saw two identical attributes, firepath has two results.

Here is the highlighted HTML code below in firebug:

<button class="list_header_search_toggle icon-search btn btn-icon table-btn-lg" style="margin-left:0px">

And below is the whole code:

<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>

NOTE: There is only 1 search button, I search it every where and there is only 1 but it shows two??

How to code this in selenium web driver?

The snippet from firepath:

在此处输入图片说明

Update:

Html code image, from firepath:

在此处输入图片说明

You can use XPath functions , for example:

  • position() returns the position of element at DOM

//button[@id='hdr_problem_task']/th[2]/button[position()=1]

  • last()

//button[@id='hdr_problem_task']/th[2]/button[last()]

  • something like first() doesn't exist, instead of this you can use index:

//button[@id='hdr_problem_task']/th[2]/button[1]

Also if button has some text you can use it as well:

//button[@id='hdr_problem_task']/th[2]/button[text()='button name']

or with contains()

//button[@id='hdr_problem_task']/th[2]/button[contains(text(), 'button name')]

UPDATE:

The button has name Search you can use XPath with - contains() .

One more small suggestion, don't forget about future support. And instead of the following locator:

//*[@id='hdr_problem_task']/th[2]/button

Much better will be:

//button[@id='hdr_problem_task']/th[2]/button

You can use th tag's name attribute value in order to recognize the correct Search button, as shown below:

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

Let me know, whether it works for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM