0

I am trying to locate Ajax control (mouse over) on Amazon home page for sign in.

WebElement element = driver.findElement(By.xpath("//*[@id='nav-link-yourAccount']"));

however this element locator works for some time and other times its not finding element and script is failing.

I have noticed that Xpath of this element is changing sometimes to //*[@id='nav-link-yourAccount']/span[1], there is no any other unique identifier which can be used to locate this element.

Could you please let me know how to resolve this variable xpath issue.

piyushj
  • 1,546
  • 5
  • 21
  • 29
shweta
  • 1
  • 2

1 Answers1

0

If you fail to find an element at one of the xpath values, you could try searching for the other xpath value. You could use ExpectedConditions to wait for a certain period of time for an element to exist. If that time elapses and the element is not found, then use the second locator to find the element. This assumes that the xpath is only changing between these two known values. Also, once you locate the element, you might want to make some assertions about the other properties of the element to further ensure you've found the element you're looking for. Here's a post about waiting for an element:

Equivalent of waitForVisible/waitForElementPresent in Selenium WebDriver tests using Java?

Community
  • 1
  • 1
jstewart379
  • 436
  • 4
  • 9