This might not be the best solution, but it should work.
Note that I’m making two assumptions about your test target:
1) That the first you are trying to click is a link, otherwise change the ‘a’ in the XPath to the appropriate HTML tag for your target .
2) The elements in your list do not share the same direct parent, in that case, you could use the XPath: "//a[@class='store-name']["+i+"]"

List<WebElement> d=driver.findElements(By.className("store-name"));
System.out.println(d.size());
for(int i=1;i<=d.size();i++) {
    WebElement d1=driver.findElement(By.xpath("(//a[@class='store-name'])["+i+"]"));
    System.out.println(d1.getText());
    d1.click();

    WebElement e=driver.findElement(By.id("comp-contact"));
    e.getText();
    System.out.println(e.getText());
    Thread.sleep(500);
    JavascriptExecutor js = (JavascriptExecutor) driver; 
    js.executeScript("window.history.go(-1)");
}

I couldn’t think of a good way to loop through the elements without using xpath or rebuilding the list. If you’re against using xpath you could also remake the list and get the relevant element from it:

List<WebElement> dLoop=driver.findElements(By.className("store-name"));
WebElement d1 = dLoop.get(i);



Source link https://sqa.stackexchange.com/questions/29559/org-openqa-selenium-staleelementreferenceexception--either-the-element-is

LEAVE A REPLY

Please enter your comment!
Please enter your name here