How to verify element present or visible in selenium 2 (Selenium WebDriver)

前端 未结 7 827
星月不相逢
星月不相逢 2020-11-29 18:41

Any one can send me sample code how to verify element

  1. ispresent
  2. isvisible
  3. isenable
  4. textpresent

in Se

7条回答
  •  盖世英雄少女心
    2020-11-29 18:51

    Try using below code:

    private enum ElementStatus{
            VISIBLE,
            NOTVISIBLE,
            ENABLED,
            NOTENABLED,
            PRESENT,
            NOTPRESENT
        }
        private ElementStatus isElementVisible(WebDriver driver, By by,ElementStatus getStatus){
            try{
                if(getStatus.equals(ElementStatus.ENABLED)){
                    if(driver.findElement(by).isEnabled())
                        return ElementStatus.ENABLED;
                    return ElementStatus.NOTENABLED; 
                }
                if(getStatus.equals(ElementStatus.VISIBLE)){
                    if(driver.findElement(by).isDisplayed())
                        return ElementStatus.VISIBLE;
                    return ElementStatus.NOTVISIBLE;
                }
                return ElementStatus.PRESENT;
            }catch(org.openqa.selenium.NoSuchElementException nse){
                return ElementStatus.NOTPRESENT;
            }
        }
    

提交回复
热议问题