java - 如何在 Selenium webdriver 中选择 "Span Type dropdown"中的值

标签 java selenium selenium-webdriver

如何从 Selenium webdriver 中的“Span Type 下拉列表”中选择值

我可以使用 XPath 单击下拉列表,但无法从下拉列表中选择值。 我点击下拉菜单的 XPath 是:

driver.findElement(By.xpath(".//*[@id='minexpButton']/span")).click();

当我在 Selenium 中使用上述代码时,下拉列表会展开,但我无法从下拉列表中选择值

我的 HTML 代码如下:

<span id="minexpButton" class="yui-button yui-menu-button yui-button-active yui-menu-button-active" style="background-color: rgb(255, 255, 255); display: -moz-inline-box;">

<div id="minexpSelectionMenu" class="yui-module yui-overlay yui-button-menu yui-menu-button-menu" style="z-index: 1003; visibility: visible; left: 367.683px; top: 1050.6px;">

<div class="bd">
<div class="selectionMenu">
<div class="ulDiv" style="overflow: auto; width: 64px; height: 210px;">
<div class="liDiv selected">

<a class="txt_black heading_4" href="#" tabindex="-1" target="_self">- Min -</a>
</div>

<div class="liDiv">
<a class="txt_black heading_4" href="#" tabindex="-1" target="_self">0</a>
</div>

<div class="liDiv">
<a class="txt_black heading_4" href="#" tabindex="-1" target="_self">1</a>
</div>

如何从下拉列表中选择值?

最佳答案

您可以使用以下函数从下拉列表中选择值
下面的函数将从下拉列表中选择 0 值,您可以参数化以下行 (temp.equals("0") 并传递您要选择的值

 List<WebElement> element = driver.findElements(By.cssSelector(".txt_black.heading_4"));
    for (int i = 0; i < element.size(); i++) {
        String temp = element.get(i).getText();
        if (temp.equals("0")) {
            element.get(i).click();             
            break;
        }
    }

关于java - 如何在 Selenium webdriver 中选择 "Span Type dropdown"中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26689428/

相关文章:

Selenium 1 与 2

java - 在文本字段中输入明天的日期

python - Selenium:FirefoxProfile 异常无法加载配置文件

python - 无法从网页获取少量项目

java - 将 Python 或 Ruby 或 PHP 重新编码为 Java Chatango 聊天室连接

java - 如何以编程方式为 JMeter 设置代理设置,而无需使用命令行运行它

java - 将 math.random 值分配给数组

java - 创建一个在激活 actionListener 时在小程序上绘制的方法

java - 驱动程序无法通过 Jenkins 上运行的 BrowserStack 进行初始化

javascript - 动态生成的元素 Protractor id