vinay kumar
vinay kumar

Reputation: 45

Unable to locate an element using text attribute in XPath

Element description

I am trying to locate element using relative XPath. I have attached HTML schema of the element. Below is the XPath I am using: //a[contains(text(),"Sales")].

Upvotes: 0

Views: 195

Answers (2)

Andersson
Andersson

Reputation: 52685

If you want to locate link by text you might need to use search by link text instead of XPath:

  • Java:

    driver.findElement(By.linkText("Sales")).click();
    
  • Python:

    driver.find_element_by_link_text("Sales").click()
    

Note that you should use exact value as it appears on rendered page in browser:

  • if it appears as SALES:

    driver.find_element_by_link_text("SALES")
    
  • if it appears as "Sales":

    driver.find_element_by_link_text("\"Sales\"")
    

In case some extra text is added by ::before pseudo-element, you can also use search by partial link text:

driver.find_element_by_partial_link_text("Sales")

Upvotes: 1

Vijay Anand
Vijay Anand

Reputation: 44

//*[@class='**your class name**']//*[text()='Sales']

Hope above XPath helps you!

Upvotes: 0

Related Questions