What is correct format for using XPath?

Does XPath vary with the browser?

  • It should't change with browser, at least not this one. Ttry to use an attribute like href instead of text, if you still have issues then please add more info, url of the page and used selector and browser name.
    – lauda
    Aug 16, 2017 at 11:24
  • 1
    Add your HTML code, so that we could help you how to locate then hyperlink. Aug 16, 2017 at 11:33

3 Answers 3


Whenever you are trying to locate Hyperlink Element & you have '/a' attribute, just forget about the XPath method and try using linkText element Locator.

    <title>My Page</title>
          <a href="http://www.google.com">Google</a>

In this situation use:


As far as i know xpath doesn't depends on any browser. Make sure you have created correct xpath it will work.

Second : It's depend on your tag which attribute it has and how efficient you are in xpath.

For example this is the simple hyperlink:

<a href="https://stackexchange.com/questions?tab=hot" name="hotnetwork" class="js-gps-track">Hot Network Questions</a>

So here i can create the xpath to locate the same in different ways like :




OR combine both attribute for uniqueness like :


OR if you want to access the link using its text then you can use contains() method

//a[contains(text(),'Hot Network')]

Note : It will locate the element based on match found with your given string (Hot Network)

If you require to locate an element if self or its child tag contains some text then using dot . in contains method as you are using :

 //a[contains(.,'Hot Network')]

OR you can use text() method like :

//a[text()=' Hot Network Questions'] 

While using text() method you have to pass the full link text including spaces else it won't work.


When you want to locate Hyperlink Element try linkText element Locator or Partial LinkText.

Example using linktext:

driver.findElement(By.linkText("click here")).click();  

Example using partial linktext:


Your Answer

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

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