簡體   English   中英

如何使用Selenium Webdriver滾動下拉列表並選擇不可見/隱藏元素?

[英]How to scroll the dropdown and select the invisible/hidden element using selenium webdriver?

我正在使用示例站點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();
}

線程“主”中的異常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(未知源) 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:元素當前不可見,因此可能無法與之交互

請有人建議我嗎?

我認為這將幫助您:

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

我在下面嘗試過,在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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM