isElementPresent in selenium 2.0

前端 未结 6 1397
太阳男子
太阳男子 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: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;
        }
    

提交回复
热议问题