如何为几个标题的下拉列表执行selectByValue?

时间:2015-10-15 14:28:10

标签: selenium selenium-webdriver automation

通常我不得不处理没有标题/标题的下拉列表,所以我只是这样执行它们:

val selectDocTypeDropDown = new Select(driver.findElement(By.id("admin-select-page")))
selectDocTypeDropDown.selectByValue("245")

但现在我需要处理带有标题的下拉列表,其HTML / CSS看起来像这样:

enter image description here

但它不接受我在上面做的两条线。我该怎么做才能按价值选择?

1 个答案:

答案 0 :(得分:0)

// find the select 
val selectDocTypeDropDown = driver.findElement(By.id("admin-select-page"))

// find the option to be selected
val optionToSelect = driver.findElement(By.xpath("//select[@id='admin-select-page']//option[@value='245']"))

selectDocTypeDropDown.click()
optionToSelect.click()

我更喜欢一起选择选择和选项,因为点击选择然后找到选项有时可能会导致选择框失去焦点。