开发者

isElementPresent in selenium 2.0

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.openqa.sel开发者_如何学Cenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class new {
 private static void one_sec() {
  Thread.sleep(4000);
 }
 public static void main(String[] args) {    
  WebDriver driver = new FirefoxDriver();
  driver.get(something1);
  Selenium selenium = new WebDriverBackedSelenium(driver, something1); 
  selenium.click("//html...");
  one_sec();
  System.out.println(selenium.isElementPresent("text"));
  WebDriver driverInstance = ((WebDriverBackedSelenium) selenium).getWrappedDriver();
  ...
  }

and I always get false as result of isElementPresent and of course element "text" is on the web (which is using GWT).


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)


You can implement it yourself using pure webdriver:

private boolean isElementPresent(By by) {
    try {
        driver.findElement(by);
        return true;
    } catch (NoSuchElementException e) {
        return false;
    }
}


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
}


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;
    }


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;
        }
    }

 


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"));


This code works for me. It may not be the most elegant, but it is functional.

Boolean elementExists=true;
    try {
        SoftAssert sAssert = new SoftAssert();
        sAssert.assertTrue (element.isDisplayed());
    }catch (NoSuchElementException e){
        elementExists = false;
    }
    Assert.assertFalse(elementExists);

You have to be careful that "NoSuchElementException" is "org.openqa.selenium" not "java.util" because otherwise, it won't work

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜