0

I have same Skip button types kept for 99 products. When I need to click that, it will bring a popup. In the popup I need to click the Cancel button. Each time when I click on the Skip buton it will give a popup like this.

popup

So I used this code. it is working up to a point of time and throwing an error.

My code is

package Backendsite;

import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;

public class SkipTest {
    @Test
    public void f() throws InterruptedException {
        System.setProperty("webdriver.chrome.driver",
                "F:\\New folder\\chromedriver.exe");

        // Setting To Open Incoginoto Window
        ChromeOptions options = new ChromeOptions();
        options.addArguments("-incognito");
        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        capabilities.setCapability(ChromeOptions.CAPABILITY, options);
        WebDriver chromedriver = new ChromeDriver(capabilities);

        chromedriver.manage().window().maximize();

        // Opening The WebSite
        chromedriver.get("xxxxxxxxxxx");

        chromedriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        Pomsite p1 = PageFactory.initElements(chromedriver, Pomsite.class);

        Select s1 = new Select(p1.getE1());
        s1.selectByVisibleText("xxxxxxxxxxxx");


        Thread.sleep(3000);

        List<WebElement> elements = chromedriver.findElements(By.xpath("//input[contains(@id, 'CustomPaging_GridView_gv_edit1_')]"));


        Thread.sleep(2000);

        for (Iterator<WebElement> iterator = elements.iterator(); iterator.hasNext();)
        {


            WebElement webElement = (WebElement) iterator.next();

            Thread.sleep(2000);

            webElement.click();

            Thread.sleep(2000);

            chromedriver.findElement(By.xpath("//button[text()='Cancel']")).click();
        }  

The Exception error is ,

org.openqa.selenium.ElementNotVisibleException: element not visible

PS: Sometimes I see Stale Element Reference Error.

3 Answers 3

0

Add a break inside the for loop. This will resolve you from Stale element Reference exception. Try the below code

 for (Iterator<WebElement> iterator = elements.iterator(); iterator.hasNext();)
    {


        WebElement webElement = (WebElement) iterator.next();

        Thread.sleep(2000);

        webElement.click();

        Thread.sleep(2000);

        chromedriver.findElement(By.xpath("//button[text()='Cancel']")).click();

    break;

    }  
1
  • 1
    Please edit your answer to explain how a break in a for loop will solve the OP's problems
    – Kate Paulk
    Jun 29, 2018 at 11:46
1

This is pretty simple. There is definitely synchronization issue. This is primarily due to the Component or page load strategy. The elegant solution to this could be have a explicit wait to check for the intractability of the element.

0

Try to skip clicking on currently invisible elements (may be they have become invisible during loop execution) with try/catch block:

try {
    webElement.click();
    Thread.sleep(2000);
    chromedriver.findElement(By.xpath("//button[text()='Cancel']")).click();
catch (ElementNotVisibleException ex) {
    // do logging
}

Or add second catch for Stale Element Reference Error.

BTW: It would be better if you point out the line of code where these errors occure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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