How to verify an attribute is present in an element using Selenium WebDriver?

前端 未结 6 2109
旧巷少年郎
旧巷少年郎 2020-12-31 02:14

I have many radio buttons on my screen. When a radio button is selected, it has an attribute of checked. When the radio button is not selected, the checked attribute is not

6条回答
  •  灰色年华
    2020-12-31 02:27

    You can create a method to handle it properly. Note this following is in C#/Java mixed style, you need to tweak a bit to compile.

    private boolean isAttribtuePresent(WebElement element, String attribute) {
        Boolean result = false;
        try {
            String value = element.getAttribute(attribute);
            if (value != null){
                result = true;
            }
        } catch (Exception e) {}
    
        return result;
    }
    

    How to use it:

    WebElement input = driver.findElement(By.cssSelector("input[name*='response']"));
    Boolean checked = isAttribtuePresent(input, "checked");
    // do your assertion here
    

提交回复
热议问题