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

前端 未结 7 794
星月不相逢
星月不相逢 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:48

    Here is my Java code for Selenium WebDriver. Write the following method and call it during assertion:

    protected boolean isElementPresent(By by){
            try{
                driver.findElement(by);
                return true;
            }
            catch(NoSuchElementException e){
                return false;
            }
        }
    
    0 讨论(0)
  • 2020-11-29 18:50
    webDriver.findElement(By.xpath("//*[@id='element']")).isDisplayed();
    
    0 讨论(0)
  • 2020-11-29 18:51

    I used java print statements for easy understanding.

    1. To check Element Present:

      if(driver.findElements(By.xpath("value")).size() != 0){
      System.out.println("Element is Present");
      }else{
      System.out.println("Element is Absent");
      }
      

      Or

      if(driver.findElement(By.xpath("value"))!= null){
      System.out.println("Element is Present");
      }else{
      System.out.println("Element is Absent");
      }
      
    2. To check Visible:

      if( driver.findElement(By.cssSelector("a > font")).isDisplayed()){
      System.out.println("Element is Visible");
      }else{
      System.out.println("Element is InVisible");
      }
      
    3. To check Enable:

      if( driver.findElement(By.cssSelector("a > font")).isEnabled()){
      System.out.println("Element is Enable");
      }else{
      System.out.println("Element is Disabled");
      }
      
    4. To check text present

      if(driver.getPageSource().contains("Text to check")){
      System.out.println("Text is present");
      }else{
      System.out.println("Text is absent");
      }
      
    0 讨论(0)
  • 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;
            }
        }
    
    0 讨论(0)
  • 2020-11-29 18:55

    To make sure that an element is present you can do the following:

    driver.findElements(By.id("id"));
    

    That will return an array, if that array size is > 0 then the element/s is present.

    Also, you need to provide more information, such as language and what have you tried before asking,

    Good luck

    0 讨论(0)
  • 2020-11-29 18:57

    To check if element is visible we need to use element.isDisplayed(); But if we need to check for presence of element anywhere in Dom we can use following method

    public boolean isElementPresentCheckUsingJavaScriptExecutor(WebElement element) {
            JavascriptExecutor jse=(JavascriptExecutor) driver;
            try {
                Object obj = jse.execute("return typeof(arguments[0]) != 'undefined' && arguments[0] != null;",
                        element);
                if (obj.toString().contains("true")) {
                    System.out.println("isElementPresentCheckUsingJavaScriptExecutor: SUCCESS");
                    return true;
                } else {
                    System.out.println("isElementPresentCheckUsingJavaScriptExecutor: FAIL");
                }
    
            } catch (NoSuchElementException e) {
                System.out.println("isElementPresentCheckUsingJavaScriptExecutor: FAIL");
            }
            return false;
        }
    
    0 讨论(0)
提交回复
热议问题