【问题标题】:Selenium WebDriver: Not able to select the element from the drop down list using webdriverSelenium WebDriver:无法使用 webdriver 从下拉列表中选择元素
【发布时间】:2013-10-08 10:45:21
【问题描述】:

我的应用程序中有两个下拉列表,选择第一个下拉列表后,第二个下拉列表变为启用。我面临的问题是我的代码能够从第一个下拉列表中选择值,但它没有从第二个下拉列表中选择值。我收到错误:

线程“主”org.openqa.selenium.InvalidElementStateException 中的异常:元素不得隐藏、禁用或只读(警告:服务器未提供任何堆栈跟踪信息) 命令持续时间或超时:138 毫秒

我在选择第一个下拉菜单后也尝试过等待,但仍然出现此错误。

我的 Java 代码:

第一个下拉菜单的代码:

  WebElement combo= d1.findElement(By.name("ctl00$ContentPlaceHolder1$ddlAgency"));
  System.out.println("proerty name for agent:" +combo);
  combo.sendKeys("huma");
  //d1.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
   Thread.sleep(200);

第二个下拉菜单的代码:

   WebElement combo1=d1.findElement(By.name("ctl00$ContentPlaceHolder1$ddlCountry"));
   System.out.println("proerty name for country:" +combo1);
   combo1.clear();
   combo1.click();
   combo1.sendKeys("Test");

第二个下拉列表的 HTML:

  <select id="ctl00_ContentPlaceHolder1_ddlCountry" style="width:450px;"       onchange="fillval();" name="ctl00$ContentPlaceHolder1$ddlCountry">

错误: 线程“主”org.openqa.selenium.InvalidElementStateException 中的异常:元素不得隐藏、禁用或只读(警告:服务器未提供任何堆栈跟踪信息) 命令持续时间或超时:138 毫秒

【问题讨论】:

    标签: java javascript selenium-webdriver


    【解决方案1】:

    尝试使用Select Class 作为第二个下拉菜单。

    WebElement combo1 = driver.findElement(By.name(ctl00$ContentPlaceHolder1$ddlCountry));
        Select secondDrpDwn = new Select(combo1);
        secondDrpDwn.selectByValue("Test");
    

    【讨论】:

    • 谢谢 Hemche。我尝试了您的代码,现在在线程“main”org.openqa.selenium.NoSuchElementException 中收到以下错误异常:无法找到具有值的选项:测试
    • 请让我知道我还能为此做些什么。
    • 我试过 secondDrpDwn.selectByIndex(4); // secondDrpDwn.selectByVisibleText("Test");也一样,但仍然出现相同的错误“无法使用”定位选项。
    • 第二个下拉菜单中有“测试”选项吗?此外,如果可能,请与选项一起分享第二个下拉菜单的 html 代码。
    • 这是 HTML:
    【解决方案2】:

    终于解决了我的问题。分享它,以便它也能帮助其他人。 以下代码对我有用。

                  WebElement dropDownListBox =d1.findElement(By.cssSelector("option[value=\"Please Select Country\"]"));
                  dropDownListBox.click();
    
                 WebElement    combo1=d1.findElement(By.name("ctl00$ContentPlaceHolder1$ddlCountry"));
                 System.out.println("proerty name for country:" +combo1);
                 combo1.click();
                 Select secondDrpDwn = new Select(combo1);
                 secondDrpDwn.selectByVisibleText("Test");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-19
      • 1970-01-01
      相关资源
      最近更新 更多