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”

4

3 回答 3

0

正如你提到的:

...但在 html 中他们使用了<img>标签。

您的错误证实了这一点。因此,根据错误,您显然不能使用Select().

您没有提供足够的信息来给出完整的答案!但是想想用户会做什么。首先他们会点击外部元素:

driver.findElement(By.id(country)).click();  // replace country with appropriate ID, or other locator!

在此之后,用户将不得不等待菜单完全出现。你的代码也一样!

接下来,用户可以单击所需的选项:

driver.findElement(By.id("Canada")).click();   // again, replace "Canada" with appropriate locator!

结果菜单很可能很长。.click()您可能需要根据某个定位器获取所有菜单项,而不是上面的,然后遍历所有菜单项:

List<WebElement> items = driver.findElements(By....);
for(WebElement item : items) {
    if(item.getText().equals("Canada"))
        item.click();
}
于 2021-10-28T18:43:30.847 回答
-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");
于 2013-11-13T05:39:35.167 回答
-1

使用以下代码:

List<WebElement> lstOptions=Country1.getoptions();
for(WebElement lstOption:lstOptions){
if(lstOption.gettext().tolowercase().equals("canada"))
lstOption.click();
}
于 2013-09-23T01:52:48.073 回答