简体   繁体   English

Selenium WebDriver:无法使用webdriver从下拉列表中选择元素

[英]Selenium WebDriver: Not able to select the element from the drop down list using webdriver

I have two dropdown list in my application, Second dropdown becomes enable after selecting the 1st dropdown. 我的应用程序中有两个下拉列表,选择第一个下拉列表后,第二个下拉列表变为启用状态。 Problem i am facing is my code is able to select the value from the 1st drop down but it is not selecting the value from the 2nd drop down. 我面临的问题是我的代码能够从第一个下拉列表中选择值,但无法从第二个下拉列表中选择值。 I am getting error as : 我收到以下错误消息:

Exception in thread "main" org.openqa.selenium.InvalidElementStateException: Element must not be hidden, disabled or read-only (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 138 milliseconds 线程“主”中的异常org.openqa.selenium.InvalidElementStateException:元素不得隐藏,禁用或只读(警告:服务器未提供任何堆栈跟踪信息)命令持续时间或超时:138毫秒

I have tried wait as well after selecting the 1st dropdown but still getting this error. 选择第一个下拉菜单后,我也尝试等待,但仍然收到此错误。

My Java code: 我的Java代码:

Code for 1st dropdown: 第一个下拉代码:

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

Code for 2nd drop down: 第二个下拉代码:

   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 for the 2nd dropdown: 第二个下拉列表的HTML:

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

Error: Exception in thread "main" org.openqa.selenium.InvalidElementStateException: Element must not be hidden, disabled or read-only (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 138 milliseconds 错误:线程“主”中的异常org.openqa.selenium.InvalidElementStateException:元素不得隐藏,禁用或只读(警告:服务器未提供任何堆栈跟踪信息)命令持续时间或超时:138毫秒

Try using the Select Class for the second dropdown. 尝试将Select Class用于第二个下拉列表。

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

finally i got the work around to my problem. 终于我解决了我的问题。 sharing it so that it will help others as well. 分享它,以便对他人也有帮助。 following code is working for me. 以下代码为我工作。

              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");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从Selenium Webdriver的下拉列表中选择隐藏元素 - Not able to select hidden element from drop down list in Selenium Webdriver 无法使用Java从Selenium Webdriver的下拉列表中选择选项 - Not able to select option from drop down list in selenium webdriver with java 使用以下命令从Selenium Java Webdriver下拉菜单中选择一个值 - select a value from drop down in selenium java webdriver using 如何使用selenium webdriver java从城市字段的GoIbibo中的自动选择下拉列表中选择一个元素 - how to select an element from autoselect drop down in GoIbibo from city field using selenium webdriver java 如何使用Java从Selenium WebDriver中的不可见下拉元素中选择选项 - How to select option from invisible drop-down element in Selenium WebDriver using Java 如何使用带有 Java 的 Selenium WebDriver 从下拉列表中选择项目? - How to select item from drop down list using Selenium WebDriver with Java? 如何从使用Selenium Webdriver中的api响应动态启动的下拉列表中选择特定位置? - How to select particular location from the drop down list which is initiated dynamically using api response in selenium webdriver? 无法使用 webdriver 中的操作 class 从下拉列表中 select 元素 - Unable to select element from drop down using Actions class in webdriver 如何从下拉列表中随机选择Selenium Webdriver(java)中的元素在html中? - How to select element from drop down Randomly in Selenium webdriver (java) drop down is in html? 如何从 selenium webdriver 的下拉列表中选择一个选项。 该元素不是“选择”元素 - How to select an option from drop down in selenium webdriver. The element is not a 'select' element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM