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.

Embedded
5102 gold badges5 silver badges16 bronze badges
asked Jun 28, 2018 at 7:54

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;
 } 
answered Jun 28, 2018 at 8:29
1
  • 1
    Please edit your answer to explain how a break in a for loop will solve the OP's problems Commented 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.

answered Jun 28, 2018 at 16:06
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.

answered Jun 28, 2018 at 13:17

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.