如何滚动下拉列表并使用硒的webdriver选择无形/隐藏的元素?

问题描述 投票:1回答:2

我使用的示例站点Redbus.in现场,我需要选择随机旅行复选框。我能得到复选框的数量,我已经编码选择随机复选框。但是,选择随机checkbox.on点击下拉行驶时出现以下异常,越来越选中第一个可见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();
}

异常在线程“主要” org.openqa.selenium.ElementNotVisibleException:元素当前不可见,所以可能不会命令持续时间或超时进行交互:10.04秒生成信息:版本:“2.39.0”,修订版:“ff23eac”,时间: '二零一三年十二月十六日16时11分15秒' 系统信息:主机: 'Dhivya',IP: '192.168.1.2',os.name: 'Windows 7的',os.arch: '86',OS。版本: '6.1',java.version: '1.7.0_10' 会话ID:32793b83-0e45-446c-bf8d-7cd1a30c2dbf驱动信息:org.openqa.selenium.firefox.FirefoxDriver能力[{平台= XP,acceptSslCerts =真, javascriptEnabled = TRUE,cssSelectorsEnabled = TRUE,databaseEnabled = TRUE,browserName =火狐,handlesAlerts = TRUE,browserConnectionEnabled = TRUE,webStorageEnabled = TRUE,nativeEvents =假,可旋转=假,locationContextEnabled = TRUE,applicationCacheEnabled = TRUE,takesScreenshot = TRUE,版本= 30.0}]在sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)在sun.reflect.NativeConstructorAccessorImpl.newInstance(未知来源)在苏n.reflect.DelegatingConstructorAccessorImpl.newInstance(未知来源)在java.lang.reflect.Constructor.newInstance(未知来源)在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:元素是不可见的,因此可能无法与之交互

请任何人建议我?

java selenium
2个回答
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();
    }
}

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);
© www.soinside.com 2019 - 2024. All rights reserved.