[英]How to scroll the dropdown and select the invisible/hidden element using selenium webdriver?
I am using the sample site Redbus.in site,in which I need to select the random travels checkbox. 我正在使用示例站点Redbus.in网站,我需要在其中选择随机行驶复选框。 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.
但是,在选中随机复选框时会发生以下异常。在单击行程下拉列表中,如果随机数在4之内,则会选择第一个可见的4个项目。如果该随机数在中间或最后一个项目中,则被隐藏,因此发生ElementNotVisibleException 。
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 su
线程“主”中的异常org.openqa.selenium.ElementNotVisibleException:元素当前不可见,因此可能无法与命令持续时间或超时进行交互:10.04秒构建信息:版本:'2.39.0',修订版:'ff23eac',时间:'2013-12-16 16:11:15'系统信息:主机:'Dhivya',ip:'192.168.1.2',操作系统名称:'Windows 7',os.arch:'x86',操作系统版本:“ 6.1”,java.version:“ 1.7.0_10”会话ID:32793b83-0e45-446c-bf8d-7cd1a30c2dbf驱动程序信息:org.openqa.selenium.firefox.FirefoxDriver功能[{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}],位于su的sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法),位于su的sun.reflect.NativeConstructorAccessorImpl.newInstance(未知源) n.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
org.openqa.selenium上的java.lang.reflect.Constructor.newInstance(未知源)上的org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193)上的n.reflect.DelegatingConstructorAccessorImpl.newInstance(未知源) org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)处的.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)在org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:268 ),位于org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:79),原因:org.openqa.selenium.remote.ErrorHandler $ UnknownServerException:元素当前不可见,因此可能无法与之交互
Please anyone suggest me ? 请有人建议我吗?
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();
}
}
I tried below and it worked fine for me in C#: 我在下面尝试过,在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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.