Selenium 如何检查下拉列表项是否被选中
Selenium How to check the drop down list item is selected
public static WebElement drpdwn_selectMonth() throws Exception{
try{
WebElement monthSelector = driver.findElement(By.id("monthID"));
monthSelector.click();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
monthSelector = driver.findElement(By.xpath("//*[@id='monthID']/option[2]"));
monthSelector.click();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
}catch (Exception e){
throw(e);
}
return element;
}
如何进行布尔值检查是否选中了下拉列表中的值?
如何打印并获取在下拉列表中选择的值
HTML 剪辑会有所帮助,但这是我的看法。如果您的菜单元素是 <select>
元素,您可以使用 Select API.
用表示菜单根定位器的 WebElement 实例化后,您可以使用 getAllSelectedOptions()
或 getFirstSelectedOption()
方法检索所选选项的文本。从这里,您可以打印值,或验证您的断言语句中的选定选项。
这只是高级概念,但如果您通读 API 文档,您应该能够找到适合您需要的解决方案。
根据您给出的小细节,可以通过以下方式完成:
WebElement monthSelector = driver.findElement(By.id("monthID"));
monthSelector.click();
if(monthSelector.isSelected())
{
Select sel = new Select(driver.findElement(By.id("monthID")));
sel.selectByVisibleText("Your-dropdown-value");
}
else
{
System.out.println("Sorry , Dropdown not selected yet");
}
请将 Your-dropdown-value 替换为您的下拉列表实际值,例如 "January".
最好你也分享你的 HTML 代码,如果以上不适合你。
public static WebElement drpdwn_selectMonth() throws Exception{
try{
WebElement monthSelector = driver.findElement(By.id("monthID"));
monthSelector.click();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
monthSelector = driver.findElement(By.xpath("//*[@id='monthID']/option[2]"));
monthSelector.click();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
}catch (Exception e){
throw(e);
}
return element;
}
如何进行布尔值检查是否选中了下拉列表中的值?
如何打印并获取在下拉列表中选择的值
HTML 剪辑会有所帮助,但这是我的看法。如果您的菜单元素是 <select>
元素,您可以使用 Select API.
用表示菜单根定位器的 WebElement 实例化后,您可以使用 getAllSelectedOptions()
或 getFirstSelectedOption()
方法检索所选选项的文本。从这里,您可以打印值,或验证您的断言语句中的选定选项。
这只是高级概念,但如果您通读 API 文档,您应该能够找到适合您需要的解决方案。
根据您给出的小细节,可以通过以下方式完成:
WebElement monthSelector = driver.findElement(By.id("monthID"));
monthSelector.click();
if(monthSelector.isSelected())
{
Select sel = new Select(driver.findElement(By.id("monthID")));
sel.selectByVisibleText("Your-dropdown-value");
}
else
{
System.out.println("Sorry , Dropdown not selected yet");
}
请将 Your-dropdown-value 替换为您的下拉列表实际值,例如 "January".
最好你也分享你的 HTML 代码,如果以上不适合你。