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

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

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

请问有人推荐我吗?

最佳答案

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

关于java - 如何使用 selenium webdriver 滚动下拉列表并选择不可见/隐藏元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26145710/

相关文章:

java - JFrame 输出结果

java - 如何使用 javascript 打印选择标签的下拉值?

在继续之前要渲染 Java Swing JFrame

wordpress - 是否有可能使用 Selenium 自动执行在 Wordpress Admin 中删除用户的过程?

python - 如何通过类名找到 btn?

java - WebSocket 数据一致性与延迟

java - 如何根据值查找枚举?

java - 无法使用 TestNG 在 Selenium Eclipse 中实例化类

selenium - 如何处理使用 WebDriver 进行页面导航时出现的警报?

javascript - 无法在 Perl 脚本中使用 selenium 远程驱动程序从下拉列表中选择选项