Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
448 views
in Selenium by
recategorized by
I am getting StaleElementReferenceException when I run my code on Flipkart website for selecting "Buy NOW" .
 
 
public void SelectItemfromPage(){
 
    WebDriver driver = new FirefoxDriver();
 
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.get("http://www.flipkart.com");
    WebElement element = driver.findElement(By.xpath(".//*[@id='fk-top-search-box']"));
    element.sendKeys("moto g");
    element.submit();
 
 
    element.findElement(By.xpath(".//*[@id='products']/div/div[1]/div[1]/div/div[1]/a[1]/img")).click();
    element.findElement(By.xpath(".//*[@id='fk-mainbody-id']/div/div[7]/div/div[3]/div/div/div[6]/div/div[3]/div[1]/div/div[2]/div/div[2]/form/input[9]")).click();
}

1 Answer

0 like 0 dislike
by
selected by
 
Best answer

You are submitting a page and again trying to work on element identified before submitting. Your code should be as below.

 

WebDriver driver = new FirefoxDriver();
 
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.get("http://www.flipkart.com");
    WebElement element = driver.findElement(By.xpath(".//*[@id='fk-top-search-box']"));
    element.sendKeys("moto g");
    element.submit();
 
 
    driver.findElement(By.xpath(".//*[@id='products']/div/div[1]/div[1]/div/div[1]/a[1]/img")).click();
    driver.findElement(By.xpath(".//*[@id='fk-mainbody-id']/div/div[7]/div/div[3]/div/div/div[6]/div/div[3]/div[1]/div/div[2]/div/div[2]/form/input[9]")).click();


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

1.4k questions

1.6k answers

866 comments

1.9k users

...