首页 文章

使用selenium web驱动程序从下拉列表中选择元素

提问于
浏览
0

我想从下拉列表中选择元素,但在html中他们使用了 <img> 标记 . 我怎样才能实现目标?

这是我的代码中的东西:

public void country() {
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    Select country1 = new Select(country);
    country1.selectByVisibleText("Canada");
}

运行testNg测试时出现此错误

org.openqa.selenium.support.ui.UnexpectedTagNameException:元素应该是“select”但是“img”

2 回答

  • 1

    如何找到下拉列表值.....使用此代码确定它的帮助你!

    driver.findElement(By.xpath("ur xpath")).click();
    
     new Select(driver.findElement(By.name("EventType"))).selectByVisibleText("Birthday");
    

    要么

    new Select(driver.findElement(By.id("city"))).selectByValue("Op3");
    
  • 0

    使用以下代码:

    List<WebElement> lstOptions=Country1.getoptions();
    for(WebElement lstOption:lstOptions){
    if(lstOption.gettext().tolowercase().equals("canada"))
    lstOption.click();
    }
    

相关问题