isElementPresent in selenium 2.0

前端 未结 6 1383
太阳男子
太阳男子 2021-02-19 11:09

Hello all I am using webdriver so if I want to use selenium;s rc function isElementPresent I have to emulate selenium rc so I do something like this:

import org.         


        
相关标签:
6条回答
  • 2021-02-19 11:13

    In the Selenium 2 world, if you want to find if an element is present you would just wrap the find call in a try catch because if it isnt present it will throw an error.

    try{
      driver.findElement(By.xpath("//div"));
    }catch(ElementNotFound e){
      //its not been found
    }
    
    0 讨论(0)
  • 2021-02-19 11:15

    I really like Rostislav Matl's alternative Moving to Selenium 2 on WebDriver, Part No.1:

    driver.findElements(By.className("someclass")).size() > 0;
    

    Javadoc: org.openqa.selenium.WebDriver.findElements(org.openqa.selenium.By by)

    0 讨论(0)
  • 2021-02-19 11:22

    Not a part of Selenium 2, you can do the following:

    // Use Selenium implementation or webdriver implementation 
    Boolean useSel = false;
    
    /**
         * Function to enable us to find out if an element exists or not.
         *
         * @param String An xpath locator
         * @return boolean True if element is found, otherwise false.
         * @throws Exception
         */
        public boolean isElementPresent(String xpathLocator) {
            return isElementPresent(xpathLocator, false, "");
        }
    
    /**
         * Function to enable us to find out if an element exists or not and display a custom message if not found.
         *
         * @param String An xpath locator
         * @param Boolean Display a custom message
         * @param String The custom message you want to display if the locator is not found
         * @return boolean True if element is found, otherwise false.
         * @throws Exception
         */
        public boolean isElementPresent(String xpathLocator, Boolean displayCustomMessage, String customMessage) {
            try {
                if (useSel) {
                    return sel.isElementPresent(xpathLocator);
                } else {
                    driver.findElement(By.xpath(xpathLocator));
                }
            } catch (org.openqa.selenium.NoSuchElementException Ex) {
                if (displayCustomMessage) {
                    if (!customMessage.equals("")) {
                        System.out.print(customMessage);
                    }
                } else {
                    System.out.println("Unable to locate Element: " + xpathLocator);
                }
                return false;
            }
            return true;
        }
    
    0 讨论(0)
  • 2021-02-19 11:26

    Sometimes the element you are trying to find is loading, s0 will throw an exception using

      findElement(By.xpath(xpathLocator))  

    Therefore we would need do what Dejan Veternik has recommended, it will help wait until the ELEMENT has been loaded in the webpage, I am passing Selenium and extracting webdriver, this is helpful incase you are using WebDriverBackedSelenium just like me ...

     private boolean isElementPresent(WebDriverBackedSelenium driver, String id) {
            try {
                driver.getWrappedDriver().findElement(By.id(id));
                return true;
    
            } catch (Exception e) {
                return false;
            }
        }
    
     
    0 讨论(0)
  • 2021-02-19 11:36

    I'm using the Node library selenium-webdriver 3.6.0.

    It has driver.findElements (return zero or more items) and driver.findElement (returns first item found or throw NoSuchElementError).

    So I'd like to recommend this as a check for if at least one element can be found. Perhaps useful in Java.

    driver.findElement(By.className("someclass"));
    
    0 讨论(0)
  • 2021-02-19 11:37

    You can implement it yourself using pure webdriver:

    private boolean isElementPresent(By by) {
        try {
            driver.findElement(by);
            return true;
        } catch (NoSuchElementException e) {
            return false;
        }
    }
    
    0 讨论(0)
提交回复
热议问题