2011-05-07 61 views
12

您好所有 所以如果我想使用硒我使用webdriver的; S RC功能isElementPresent我不得不仿效硒RC,所以我做这样的事情:isElementPresent硒2.0

import org.openqa.selenium.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(); 
    ... 
    } 

,我总是得到isElementPresent的结果为false,当然元素“text”在web上(使用GWT)。

+1

是否文本元素有它的ID为“文本”?你没有提到任何定位符前缀来表示它的xpath或css或dom。 Selenium将寻找@ id ='text' – 2011-05-08 04:04:31

回答

12

我真的很喜欢Rostislav Matl's替代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

我对size()方法感到困惑。有没有名为size()的WebElement的方法? – 2012-08-28 09:46:10

+2

@RiponAlWasim注意,'size()'是一个'List'方法。我所指的'WebDriver'方法是'findElements',而不是'findElement',它返回一个'List '。请参阅签名:[WebDriver.findElements(by by)](http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebDriver.html#findElements%28org.openqa。 selenium.By%29)以及一个示例:[按类名查找UI元素(WebElements)](http://seleniumhq.org/docs/03_webdriver.html#introducing-the-selenium-webdriver-api-by-example#按类名称) – Alberto 2012-08-28 11:49:08

+0

是的,明白了。你是对的。对不起,我错了 – 2012-08-28 12:02:21

3

在Selenium 2世界中,如果您想查找是否存在元素,您只需将try调用包装在try catch中,因为如果它不存在,它将引发错误。

try{ 
    driver.findElement(By.xpath("//div")); 
}catch(ElementNotFound e){ 
    //its not been found 
} 
+2

或'driver.findElements(...).size()!= 0'的元素; ) – 2011-07-12 22:29:25

+0

我想,WebDriver中没有size()方法。有一个getSize()方法,返回Dimension – 2012-08-01 07:53:12

+0

@AutomatedTester:我怎样才能使用assertTrue()通过使用上面的代码 – 2012-08-01 07:54:10

3

不硒2的一部分,你可以做到以下几点:

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

可以使用纯webdriver自行实施:

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

有时候你正在努力寻找被加载元素,S0将抛出使用

findElement(By.xpath(xpathLocator))

因此,我们需要做的德扬维特尼克建议一个例外,这将有助于等到元素已经加载在网页中,我传递硒webdriver的提取,这是有益的柜面你正在使用WebDriverBackedSelenium就像我一样......

private boolean isElementPresent(WebDriverBackedSelenium driver, String id) { 
     try { 
      driver.getWrappedDriver().findElement(By.id(id)); 
      return true; 

     } catch (Exception e) { 
      return false; 
     } 
    }