如何滚动下拉列表并使用selenium webdriver选择不可见/隐藏元素?

时间:2014-10-01 16:08:48

标签: java selenium

我正在使用示例网站Redbus.in网站,我需要在其中选择随机旅行复选框。 我可以得到复选框的计数,我编码选择随机复选框。但是,选择随机复选框时会发生以下异常。单击旅行下拉列表,如果随机数在4以内,则第一个可见的4个项目将被选中。如果随机数位于中间或最后一项,则隐藏,因此发生ElementNotVisibleException

我为选择随机复选框编写的代码

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();
}
  

线程“main”中的异常org.openqa.selenium.ElementNotVisibleException:元素当前不可见,因此可能无法与之交互   命令持续时间或超时:10.04秒   构建信息:版本:'2.39.0',修订版:'ff23eac',时间:'2013-12-16 16:11:15'   系统信息:主机:'Dhivya',ip:'192.168.1.2',os.name:'Windows 7',os.arch:'x86',os.version:'6.1',java.version:'1.7.0_10 “   会议ID:32793b83-0e45-446c-bf8d-7cd1a30c2dbf   驱动程序信息: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,rotating = false,locationContextEnabled = true ,applicationCacheEnabled = true,takeScreenshot = 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)       在org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193)       在org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)       在org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)       在org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:268)       在org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:79)       引起:org.openqa.selenium.remote.ErrorHandler $ UnknownServerException:元素当前不可见,因此可能无法与

进行交互

有人建议我吗?

2 个答案:

答案 0 :(得分:0)

我认为这会帮助你:

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

你也可以这样做:

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();
    }
}

答案 1 :(得分:0)

我在下面尝试过,在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);