0

xpath not recognised in page object model in webdriver.

@FindBy(xpath = "//td[contains (text(),'xxx')]")
public WebElement topMenu;

topMenu.click();

Error: Unable to locate element: {"method":"xpath","selector":"//td[contains (text(),'xxx')]"}

I'm using page object model in WebDriver. Any suggestions?

So this is my actual code of my program

public WebElement userName;
public WebElement password;
public WebElement id;
@FindBy(xpath = "//td[contains (text(),'Procurement')]")
public WebElement topMenu;
  • I expect your HTML to contain an excerpt <td>Procurement</td>. Is it there? – dzieciou Mar 20 '14 at 10:43
  • @dzieciou Now i have edited the error – Emmanuel Angelo.R Mar 20 '14 at 11:23
  • Can you then provide also your HTML document, or at least its relevant part? – dzieciou Mar 20 '14 at 12:36
  • i dont have any html documents it is displayed in my eclipse console – Emmanuel Angelo.R Mar 20 '14 at 12:40
  • Well, I guess it is evaluated over some Web page (HTML), when you run your test with your PageObject, right? – dzieciou Mar 20 '14 at 13:12
1

There are two parts to this. The first is that you need to be sure that your xPath is correct. One way to validate that is to use a tool like Firefinder (a firefox extension for firebug). Navigate to your page and be sure the element you need is present and then hit f12 and go to the FireFinder section. Enter in the xpath you are using and see if it can be found.

Assuming your xpath is correct then you might want to investigate using an explicit wait if from some reason you are waiting on the page element to load.

Example:

    WebDriverWait wait = new WebDriverWait(_driver, 10);
    wait.until(ExpectedConditions.visibilityOf(topMenu));
    topMenu.click();

I would suspect though this is failing because your xPath is not quite right.

| improve this answer | |
1

Can you find it if you use the .Text and then clicking it if it matches?

String Expectedtext = "Procurement"
String ProcurementButtonText = driver.FindElement(By.XPath("//td")).Text;


if (ProcurementButtonText == Expectedtext)
{
   driver.FindElement(By.XPath("//td")).click()
}
| improve this answer | |

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