1

What will be the XPath of a 'Search' button from the expression mentioned above?

  • 1
    Your question has only one statement. What do you mean by "expression mentioned above". – João Farias Jul 16 at 10:34
  • 1
    Please edit your question to add the source code, what you have tried, and the results you received. – Kate Paulk Jul 16 at 19:33
  • Please add code snippet of an XPath and an HTML page of your DOM element. – Bulat Jul 17 at 7:34
1

It's hard to come up with the correct XPath locator without seeing the source code of the page you're trying to automate.

Here are few hints:

  1. If the element is a <button> which looks like:

    <button class="favorite styled"
            type="button">
        Search
    </button>
    

    the relevant XPath locator would be

    //button[text()='Search']
    
  2. If the element is an <input> with the value attribute which looks like:

    <input type="button" value="Search">
    

    the relevant XPath locator would be:

    //input[@value='Search']
    
  3. It's also possible to use XPath Functions or Operators for example for partial match:

    //input[contains(@value, 'Search')]
    

More information:

1

Using the browser dev tools

In Chrome, I believe that you can get the Xpath value for any element by:

  1. Opening the Developer Tools for your page
  2. Clicking on the Elements tab.
  3. Right-clicking on the target element and choosing Copy -> Copy Xpath.

Good luck!

1

Using the placeholder text (when available)

Although all the answers given above are correct and would help you. But i just want to add my thoughts/work or suggestions for handling such type of elements like 'Search', login and Signup fields and buttons. For Search field case:

  1. For every search field it will be an input field so you are sure about input tag.
  2. Than how you got to know that it is a search field - By looking into the text inside that field(we call it placeholder text) and interpreting it. Placeholder must have the text like "Search" , "find" etc. So combining and observing the above two points you can come up with css/xpath locator, which could be like:

    xpath = //input[@placeholder="Search for products"]

    css = input[placeholder="Search for products"]

    Other alternatives:

    xpath = //input[@any-attribute-having-text="Search"]

    xpath = //input[contains(@any-attribute-contains-text,"Search")]

Hope it helps in your software testing services for building locators of other fields also, like username, password, firstname, lastname.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.