【问题标题】:Select a value from drop down in selenium webdriver从 selenium webdriver 的下拉列表中选择一个值
【发布时间】:2014-12-02 13:14:28
【问题描述】:

代码如下:

 <div class="default_input_control Dropdown_Options single">
  <div class="default_input_field items custom_dropdown dropdown_arrow input_dropdown not-full has-options focus input-active dropdown-active">
    <div class="option" data-selectable="" data-value="10">option1</div>
    <div class="option" data-selectable="" data-value="20">option2</div>
    <div class="option" data-selectable="" data-value="30">option3</div>
    <div class="option" data-selectable="" data-value="40">option4</div>

目前我正在使用 xpath 来选择下拉菜单并单击选项。请让我知道我可以选择下拉菜单并单击选项“option2”的任何其他方式吗?

【问题讨论】:

    标签: selenium selenium-webdriver


    【解决方案1】:

    因为这不是 "Select-type dropdown" 的情况,您可以使用 "org.openqa 的 "Select class" 的帮助.selenium.support.ui" 包,我认为您最好使用单击下拉菜单,然后选择相关选项。但是,您也可以在下拉列表中使用 "org.openqa.selenium.interactions" 包的 "Action class",如下所示:

    Actions act = new Actions(driver);
    act.moveToElement(driver.findElement(By.xpath("//xpath of dropdown"))).click().perform();
    act.moveToElement(driver.findElement(By.xpath("//xpath of option in the dropdown"))).click().perform();
    

    注意:- 您可以使用“id、css 等”作为定位器。我刚刚使用 xpath 来展示它是如何工作的。

    【讨论】:

    • @Gurunathan:很高兴它起作用了。如果这对你有帮助,请将其标记为答案。干杯。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-08
    • 2011-07-25
    • 2020-05-20
    • 2018-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多