2
votes

I am using the sample site Redbus.in site,in which I need to select the random travels checkbox. I can get the count of checkbox, and I have coded to select the random checkbox. However, the below exception occurs when selecting the random checkbox.on clicking travels dropdown, the first visible 4 items are getting selected if the random number is within 4. If the random number is in middle or last items,that are hidden so ElementNotVisibleException occurs.

The code that i have written for selecting random checkbox,

public class RedBus
{
public static void main (String args[])
{

driver.findElement(By.cssSelector("a.dpBtn")).click();
Random r=new Random();
WebElement boxes=driver.findElement(By.xpath("//div[@class='filter Travels opened']"));
List<WebElement> checkBoxes=boxes.findElements(By.xpath("//input[@type='checkbox']"));
int no=checkBoxes.size();
System.out.println(no);
WebElement Check=checkBoxes.get(r.nextInt(checkBoxes.size()));
System.out.println(Check);
Check.click();
}

Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with Command duration or timeout: 10.04 seconds Build info: version: '2.39.0', revision: 'ff23eac', time: '2013-12-16 16:11:15' System info: host: 'Dhivya', ip: '192.168.1.2', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_10' Session ID: 32793b83-0e45-446c-bf8d-7cd1a30c2dbf Driver info: org.openqa.selenium.firefox.FirefoxDriver Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, browserConnectionEnabled=true, webStorageEnabled=true, nativeEvents=false, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=30.0}] at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193) at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554) at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:268) at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:79) Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Element is not currently visible and so may not be interacted with

Please anyone suggest me ?

2

2 Answers

2
votes

I tried below and it worked fine for me in C#:

  //click the drop down list 
  IWebElement entityList = driver.FindElement(By.XPath("//input[@id='cbOrganisations_Input']")); 
  entityList.Click();
  //find the invisible element on the list by xpath/id/tag etc. 
  IWebElement selectEnityName = driver.FindElement(By.XPath("//li[@class='rcbItem'][contains(text(),'Manish Test Org')]"));
  //use javascript to  navigate to that element
  (IJavaScriptExecutor)driver).ExecuteScript("arguments[0].scrollIntoView(true);", selectEnityName);
  //use javascript to click that element on the list
  ((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click();", selectEnityName);
0
votes

I think this will help you out:

WebElement Check;    
for(int i = 0; i < no; i++)
{
    System.out.println(no);
    Check = checkBoxes.get(r.nextInt(no));
    if(Check.isDisplayed())
    {
        Check.click();
    }
}

You could also do:

WebElement Check;    
for(int i = 0; i < no; i++)
{
    System.out.println(no);
    Check = checkBoxes.get(r.nextInt(no));
    if(Check.isDisplayed() && Check.isEnabled())
    {
        Check.click();
    }
}