Java 如何使用Selenium从下拉列表中进行选择

Java 如何使用Selenium从下拉列表中进行选择,java,selenium,Java,Selenium,请参阅下面的代码,我需要单击下拉列表,以便显示该列表 <span class="k-widget k-dropdown k-header" style="" unselectable="on" role="listbox" aria-haspopup="true" aria-expanded="false" tabindex="0" aria-owns="" aria-disabled="false" aria-readonly="false" aria-busy="false">

请参阅下面的代码,我需要单击下拉列表,以便显示该列表

<span class="k-widget k-dropdown k-header" style="" unselectable="on" role="listbox" aria-haspopup="true" aria-expanded="false" tabindex="0" aria-owns="" aria-disabled="false" aria-readonly="false" aria-busy="false">
    <span class="k-dropdown-wrap k-state-default k-state-hover k-state-focused" unselectable="on">
    <span class="k-input" unselectable="on">Is equal to</span>
    <span class="k-select" unselectable="on">
        <span class="k-icon k-i-arrow-s" unselectable="on">select</span>
    </span>
</span>

等于
选择

我认为您必须再次尝试使用
JavascriptExecutor

WebElement element1 = driver.findElement(By.xpath("//span[@class='k-widget k-dropdown k-header' and @role='listbox']"));
JavascriptExecutor executor1 = (JavascriptExecutor) oBrowser;
executor1.executeScript("arguments[0].click();", element1);
如果上述Xpath不起作用,请尝试以下操作:-

//span[@class='k-widget k-dropdown k-header']

希望它能对你有所帮助:)

前几天我遇到了这个问题。问题是您正在引用的剑道UI
元素使用
元素来获取其内部文本,但处理选项更改/选择的是
列表。单击
时,您将无法从弹出/下拉菜单中选择选项,您必须单击包含span引用的文本选项的
  • 元素

    在您的示例中,在HTML文档的其他地方,您会发现一个类似于

    <ul unselectable="on" class="k-list k-reset" tabindex="-1" aria-hidden="true" aria-live="off" data-role="staticlist" role="listbox">
        <li tabindex="-1" role="option" unselectable="on" class="k-item k-state-selected k-state-focused" data-offset-index="0">is equal to</li>
        <li tabindex="-1" role="option" unselectable="on" class="k-item" data-offset-index="1">another option</li>
    </ul>
    
    
    
    使用Selenium 3,如果您想选择“另一个选项”:

    driver.findElement(By.xpath(//li[text()='other option'])。单击()


    您应该会看到您的
    内部文本更改为“另一个选项”,您可以继续您的测试旅程。

    您是否尝试了
    driver.findElement(By.cssSelector(“[class='k-widget k-dropdown k-header']”)。单击()
    还没有?抱歉,代码不起作用。您可以提供堆栈跟踪吗?嗨,实际上,它是你回答的过滤器的延续。单击过滤器后,我们将得到一个弹出窗口,其中包含的下拉列表等于,以开头,包含等。我们是否需要从窗口切换到弹出窗口,然后单击下拉列表,以便显示编辑列表,这取决于它是浏览器弹出窗口还是应用程序弹出窗口。。如果直接单击不起作用,那么您肯定需要切换控件。您可以通过查看上面的给定代码向我提供脚本,该代码是过滤器的延续。在下拉单击后,您希望选择什么值。。单击下拉菜单是否单击工作正常?单击下拉菜单是否工作正常-否。这就是我要做的,要先单击下拉菜单,然后选择所需选项,弹出窗口开始显示选项默认为
    <ul unselectable="on" class="k-list k-reset" tabindex="-1" aria-hidden="true" aria-live="off" data-role="staticlist" role="listbox">
        <li tabindex="-1" role="option" unselectable="on" class="k-item k-state-selected k-state-focused" data-offset-index="0">is equal to</li>
        <li tabindex="-1" role="option" unselectable="on" class="k-item" data-offset-index="1">another option</li>
    </ul>