java - Selenium 自动化 - 如何从此列表中选择一个下拉菜单项而不会出现异常错误?

标签 java eclipse selenium selenium-webdriver automation

我想从网页下面的下拉菜单中选择一个下拉菜单项:

Drop menu with menu items

HTML code for the drop menu

HTML code for menu items

我尝试了以下 3 种方法来选择带有文本“Test”(索引[1])的项目:

  • 案例 1:
   driver.findElement(By.id("combo-1781-inputEl")).click();

   driver.findElement(By.id("combo-1781-inputEl")).sendKeys("Test");

没有出现错误(测试通过)。但是“测试”项文本未显示在所选的下拉菜单字段中。

  • 案例 2:
     driver.findElement(By.id("combo-1781-inputEl")).click();

     Select drp = new Select(driver.findElement(By.id("boundlist-1930-listEl")));
     drp.selectByVisibleText("Test");

org.openqa.selenium.support.ui.UnexpectedTagNameException 被抛出。测试失败。

  • 案例 3:
    Select drp = new Select(driver.findElement(By.id("boundlist-1930-listEl")));
    drp.selectByIndex(1);

org.openqa.selenium.support.ui.UnexpectedTagNameException 被抛出。测试失败。

最佳答案

您可以先使用以下方法点击下拉元素:

driver.findElement(By.id("combo-1781-inputEl")).click();

然后您可以使用以下方法单击“测试”元素:

driver.findElement(By.xpath("//div[@id='boundlist-1930-listEl']//li[text()='Test']")).click();

您还可以在此处参数化“测试”文本,以便将来如果您想单击任何其他下拉值,您只需在 xpath 中发送该文本然后单击它,您可以这样做:

String text="Test";
driver.findElement(By.xpath("//div[@id='boundlist-1930-listEl']//li[text()="+text+"]")).click();

关于java - Selenium 自动化 - 如何从此列表中选择一个下拉菜单项而不会出现异常错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55899396/

相关文章:

java - 如何扫描 hh 中的时间 :mm format as string and get the value of hh and min in integer?

java - IClasspathEntry 的绝对文件系统路径

regex - Eclipse:如何搜索字符串,但不在注释中

java - Selenium Webdriver - 取消选中复杂表格中的所有复选框

java - 使用 Jackson 在 Java 中使用不同的 POJO 反序列化 JSON 数组

java - jp2launcher在小程序程序中的作用是什么?

java - 如何在不使用 collections.swap 方法的情况下交换数组列表中的两个元素

PHP调试preg-replace回调函数

javascript - Selenium (Python): How to insert value on a hidden input?

selenium - 扩展 Selenium : How to call commands?