2011-05-07 60 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; 
     } 
    }