【问题标题】:How can I verify the selected item in the list?如何验证列表中的选定项目?
【发布时间】:2015-01-15 22:21:37
【问题描述】:

我需要验证列表中的选定项目是“最新的”。当我尝试使用 getAttribute 时,其他项目的输出为 null,所选项目的输出为 true,但我没有在控制台中看到它们的名称。列表中有 4 项,我看到的只是 null、null、null、true。但我的主要目标是验证列表中的选定项目。感谢您的帮助!

HTML 页面:

下面是我的代码:

WebElement defaultItem = driver.findElement(By.xpath("//*[@id='myAddNewCampaignForm']/ol/li[16]/select"));
List<WebElement> allOptions = defaultItem.findElements(By.tagName("option"));
System.out.println("total items " + allOptions.size());

for (int i=0; i<allOptions.size(); i++) {
    System.out.println(allOptions.get(i).getText());
    System.out.println(allOptions.get(i).getAttribute("selected"));
}

【问题讨论】:

    标签: java selenium selenium-webdriver


    【解决方案1】:

    导入这个

    import org.openqa.selenium.support.ui.Select 
    

    使用 Select 类控制下拉对象。

    WebElement defaultItem = driver.findElement(By.xpath("//*[@id='myAddNewCampaignForm']/ol/li[16]/select"));
    Select select = new Select(defaultItem);
    List<WebElement> selectedOptions = select.getAllSelectedOptions();
    
    
    for(WebElement option : selectedOptions){
       System.out.println(option.getText());
    }
    

    【讨论】:

      猜你喜欢
      • 2013-09-18
      • 2017-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-23
      • 2019-06-08
      相关资源
      最近更新 更多