gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 14:30:59 25 4
gpt4 key购买 nike

我正在使用示例网站 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com