Lokesh
Lokesh

Reputation: 214

Unable to find element using Xpath

Click on the element for the below HTML code is not working.

a class="add" href="/travelPlan_revamp/addTravel.htm?travel_type=D">Add</a>

I'm using the next Xpath: html/body/div[1]/div[2]/div/a

But No Such Element Found exception is displayed.

Upvotes: 1

Views: 2419

Answers (7)

Lokesh
Lokesh

Reputation: 214

Sorry guys the the thing is it was in Iframe. Now i have found the solution by switching to the iframe and then clicking an event.

driver.switchTo().frame("rightMenu"); driver.findElement(By.xpath("html/body/div[1]/div[2]/div/a")).click();

Upvotes: 1

snigdha
snigdha

Reputation: 26

Try with this below xpath

//a[contains(text(),'Add')]

Upvotes: 0

user3864935
user3864935

Reputation:

//a[@class='add' and text()='Add'] 

Try that one.

Through which method are you looking for the element?

@FindBy (how = How.XPATH)

or

driver.findElement(By.xpath());

Sometimes the @FindBy notation doesn't like working with Xpaths properly.

Upvotes: 0

Saritha G
Saritha G

Reputation: 2608

Try this xpath:

 //a[contains(text(),'Add')];

Upvotes: 1

Jayakumar A U
Jayakumar A U

Reputation: 23

An absolute xpath always starts with a forward slash '/'. I think you have used an absolute xpath and either you have missed'/' or you quoted it wrong here. Please verify it. Normally absolute xpath will look like /html/body/form/div[3]/div[2]/div[1]/div[2]/input

Upvotes: 1

Vishal Jagtap
Vishal Jagtap

Reputation: 896

First make sure the web element is not inside iframe. If yes, Then you need to switch to iframe first and then try clicking on element.

Also, try below xpath, in case you have captured wrong xpath:-

//a[@class='add' and .='Add']

Upvotes: 1

crashxxl
crashxxl

Reputation: 679

Hard to tell if your XPATH expression is right without the HTML. Your XPATH expression tell:

  • Take a "a" elements
  • In the 2nd div element
  • In the first div element
  • In the body element
  • That is in the html element

So checks there is a "a" element that check the Xpath expression.

Upvotes: 0

Related Questions