简体   繁体   English

如何根据Selenium和Java的HTML从下拉菜单中选择一个选项

[英]How to select an option from the dropdown as per the HTML through Selenium and Java

This is My HTML: 这是我的HTML:

<tbody>
  <tr>
    <td colspan="2" style="text-align:center;font-weight:bold;padding-bottom:10px;">You     have Statewide Access<br>Please select the district in which you want to    operate</td>
  </tr> 
  <tr> 
    <td style="text-align:right;width:50%;font-weight:bold;padding-right:10px;"><label      for="districtOption">Select District</label></td>
    <td style="text-align:left;width:50%;">
      <select id=`district Option` name="district Option" size="0"                  alt="Select District" tab index="1">
        <option value="00" selected="">-- SELECT --</option>
        <option value="01"> A1 </option>
        <option value="02"> A2 </option>
      </select>
    </td>
  </tr>

My First Try: 我的第一次尝试:

Select select = new Select(driver.findElement(By.xpath("//select[@id='distrctOption']")));
select.selectByVisibleText("A1");

Second Try: 第二次尝试:

Select dropdown = new Select(driver.findElement(By.id("districtOption")));
dropdown.selectByIndex(01);
driver.findElement(By.xpath("//[@value='Select']")).click();

Error Message in Console for every executing one Error "Exception in thread "main" 控制台中针对每个执行一个错误的错误消息“线程“主”中的异常”

org.openqa.selenium.NoSuchElementException Unable to locate element"

*** Element info: {Using=xpath, value=//select[@id='districtOption']}

I tried all possible ways i can ? 我尝试了所有可能的方式?

There are no Frames in Page, but still not able to Select it. 页面中没有框架,但仍然无法选择它。

Use 采用

Select select = new Select(driver.findElement(By.xpath("//select[@id='district Option']")));

You are missing the space in your xpath identification. 您在xpath标识中缺少空格。

As per the HTML you have shared to select an option from the dropdown you have to use the <select> class and the associated methods and you can use either of the following solutions: 按照您共享的HTML格式从下拉菜单中选择一个选项,您必须使用<select>类和关联的方法,并且可以使用以下任一解决方案:

Select dropdown = new Select(driver.findElement(By.cssSelector("select[name='district Option'][alt='Select District']")));
//Select dropdown = new Select(driver.findElement(By.xpath("//select[@name='district Option' and @alt='Select District']")));
dropdown.selectByValue("01"); //to select the option A1
dropdown.selectByValue("02"); //to select the option A2

暂无
暂无

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

相关问题 如何单击一个下拉列表并通过Selenium和Java选择一个选项? - How to click on a dropdown and select an option through Selenium and Java? 如何通过Selenium WebDriver从下拉菜单中选择一个选项 - How to select an option from a dropdown through Selenium WebDriver 如何 select Selenium Java 中的列表中的选项(不在下拉列表中) - How to select an option from a list (not in dropdown) in Selenium Java 如何使用 selenium webdriver 和 Java 从剑道下拉菜单中选择一个选项 - How to select an option from the kendo dropdown using selenium webdriver and Java 如何使用 Selenium 从下拉列表中选择一个选项 select - How to select an option from a dropdown using Selenium 当相应的下拉列表不是“多选”/ java和Selenium时,如何从下拉列表中取消选择一个选项 - How can i deselect an option from a dropdown, when that respective dropdown is not a “multi-select” / java & Selenium Selenium Webdriver JAVA-无法从下拉菜单中选择选项(JAVA) - Selenium Webdriver JAVA - Unable to select option from dropdown menu (JAVA) 如何在具有跨度HTML的Java中使用Selenium在下拉菜单中选择一个选项 - How to select an option in dropdown using selenium in java which has span html 如何使用 Selenium 和 Java 从非 select 下拉菜单中单击和 select 选项 - How to click and select an option from a non select dropdown using Selenium and Java 无法从 Selenium 和 java 的下拉列表中选择 select - Cannot select option from dropdown list in Selenium & java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM