0

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:

enter image description here

Update:

Html code image, from firepath:

enter image description here

11
  • Can you share site Url or Html? Commented Mar 21, 2017 at 12:15
  • Try:.//button[@id='hdr_problem_task']/th[2]/button[0] .//button[@id='hdr_problem_task']/th[2]/button[1] .//button[@id='hdr_problem_task']/th[2]/button[2] Commented Mar 21, 2017 at 12:16
  • Use indexing after completion of your xpath. Commented Mar 21, 2017 at 12:18
  • you can use an other selector LinkText for example !! Commented Mar 21, 2017 at 12:21
  • @RichardMadsi have a look How do I ask a good question Commented Mar 21, 2017 at 13:11

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

3 Comments

the code is visible in firepath and it has two the same results: 1rst Results: <span class="sr-only">Search</span> 2nd Results: <span class="sr-only">Search</span>
but it is not visible in Eclipse when I ran it
What exception/error do you see when you run code using above xpath?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.