【问题标题】:Select dropdown using selenium使用 selenium 选择下拉菜单
【发布时间】:2015-09-29 10:52:59
【问题描述】:

通常我像这样使用 selenium Select 对象:

val selectCode = new Select(driver.findElement(By.id("someID")))
selectCode.selectByValue("4")

我的问题是我在 html 代码中没有价值,这是按钮 html:

<button type="button" class="btn dropdown-toggle btn-default" data-toggle="dropdown" data-id="admin-select-page" title="Select..."><span class="filter-option pull-left">Select...</span>&nbsp;<span class="bs-caret"><span class="caret"></span></span></button>

这是我需要选择的值:

<li data-original-index="4" data-optgroup="2"><a tabindex="0" class="opt  " style="" data-tokens="null"><span class="text">
Invoices
</span><span class="glyphicon glyphicon-ok check-mark"></span></a></li>

但正如你在上面看到的,我通常有 id 和 value 所以 By.id 或 By.value 会起作用...

在这种情况下如何选择它..?

谢谢!

【问题讨论】:

  • 最好的方法是通过 xpath 或 cssSelector 进行选择。但是我不能给你 xpath 的例子,因为没有足够的 hmtl 代码给你正确的 xpath...

标签: java html selenium


【解决方案1】:

您可以使用 xpath 来定位元素。

driver.findElement(By.(xpath("//button[contains(text(),'Select...')]/li[contains(text(),'Invoices')]")

这里,text() 函数用于查找标签内包含一些文本的元素。

【讨论】:

    【解决方案2】:

    对我来说总是使用这个代码(它小而有用):

    new Select(driver.findElement(yourElement)).selectByVisibleText("Txt");
    

    在页面对象(+PageFactory)中是这样的:

    @FindBy(xpath = "//select[@name='filter_state']") //for example
    private WebElement dropdownList;
     private void selectStatus(String selectText){
        new Select(dropdownList).selectByVisibleText(selectText);
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-25
      • 1970-01-01
      • 2022-11-11
      • 1970-01-01
      • 1970-01-01
      • 2018-02-17
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      相关资源
      最近更新 更多