Shravan
Shravan

Reputation: 11

Error stating "element is not found in the cache perhaps the page has changed"

I'm trying to click search button on flipkart through Selenium Webdriver using Java, i'm able to click the button by the X-path and i written 'Boolean' to display button was clicked. Here's the code:

WebElement search = driver.findElement(By.xpath(".//*[@id='fk-header-search-form']/div/div/div[2]/input[1]"));
search.click();
boolean clicked = search.isEnabled();
System.out.println("Serach Button Clicked"+clicked);

Upvotes: 1

Views: 39

Answers (2)

JeffC
JeffC

Reputation: 25644

There are a few issues.

  1. .isEnabled() Determines whether the element is enabled. According to the docs, this is pretty much always going to be true except in a case where there's an INPUT that is disabled (which doesn't apply here). So your code is just telling you that the Search button is not disabled, not whether you clicked it or not.

  2. You didn't post enough code to tell why you are getting this error. I can see what you are trying to do and wrote a simple example of how to do this.

Try this

FirefoxDriver driver = new FirefoxDriver();
driver.get("http://www.flipkart.com/");
By searchBoxLocator = By.id("fk-top-search-box");
By searchButtonLocator = By.cssSelector("input[value='Search']");
driver.findElement(searchBoxLocator).sendKeys("watch");
driver.findElement(searchButtonLocator).click();
  1. I would suggest that you use something other than XPaths. They are brittle and slower than other methods. In the code above, I used a CSS Selector. Read some tutorials and use this page as a reference. They are very powerful and are, IMHO, better than XPath. There is some stuff that can only be done with XPath... avoid XPath until you hit one of those cases.

Upvotes: 1

Mahsum Akbas
Mahsum Akbas

Reputation: 1583

If page is change after click on button, it is normal to not find element. You perform a search peocess, after click, new page being loading. Another point, isEnabled, everytime returns true except disabled. In this situation, it looks already active.

Upvotes: 1

Related Questions