[英]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.