首页 > 解决方案 > 来自选择框的 XML 过滤器

问题描述

也许有人知道如何为这个选定的值过滤 XML 树?我需要“B3”字符串在 Selenium 中另作他用。

如果有人可以帮助我,那就太好了。:)

提前非常感谢!

例子

标签: xmlselenium

解决方案


这是一个静态下拉列表,值不会改变。

请用

//option[@value='1' and @selected]

另外,你可以使用

//option[@value='1' and @selected and text()='B3']

PS:如果我们有唯一的条目,请检查dev tools(谷歌浏览器)。HTML DOM

检查步骤:

Press F12 in Chrome-> 转到element部分 -> 执行CTRL + F-> 然后粘贴xpath并查看,如果您想要element的是否使用匹配节点突出显示。1/1

更新 :

List<WebElement> dropDown = driver.findElements(By.xpath("//select[@name='wert_1']//option");
try{
    for (WebElement option  : dropDown)
        if (option.getAttribute('selected').contains('selected')){
            System.out.println("Selected option is "+ option.getAttribute('innerText'))
        }
        else
            System.out.println("none of the options are selected")
catch(Exception e){
    System.out.println("something went wrong")
    }

推荐阅读