What will be the XPath of a 'Search' button from the expression mentioned above?
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:
If the element is a
<button>which looks like:
<button class="favorite styled" type="button"> Search </button>
the relevant XPath locator would be
<input type="button" value="Search">
the relevant XPath locator would be:
It's also possible to use XPath Functions or Operators for example for partial match:
Using the browser dev tools
In Chrome, I believe that you can get the Xpath value for any element by:
- Opening the Developer Tools for your page
- Clicking on the Elements tab.
- Right-clicking on the target element and choosing Copy -> Copy Xpath.
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:
- For every search field it will be an input field so you are sure about input tag.
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"]
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.