2017-04-20 66 views
0

我正在寫一個自動測試。 我做了感謝這個指南: sukesh15.gitbooks.io隨着黃瓜硒webdriver爪哇,如何檢查WebElement presenceOfElement

這裏是我的Page類的摘錄:

public class TestCuCumBerPage { 

WebDriver driver; 

@FindBy(id = "display") 
private WebElement buttonDisplay; 

public void buttonDisplayClick() throws Throwable { 
    WebElement myDynamicElement = (new WebDriverWait(driver, 30))   
     .until(ExpectedConditions.presenceOfElementLocated(By.id("display"))); 

    buttonDisplay.click(); 
} 
} 

我已經使用ID 「顯示」:

  • 在@FindBy聲明中(給WebElement「buttonDisplay」)
  • in WebDriverWait

爲了避免這種情況,我創建了一個恆定的BUTTON_DISPLAY_ID與編號:

public class TestCuCumBerPage { 

WebDriver driver; 

private final String BUTTON_DISPLAY_ID = "display"; 
@FindBy(id = BUTTON_DISPLAY_ID) 
private WebElement buttonDisplay; 

public void buttonDisplayClick() throws Throwable { 
    WebElement myDynamicElement = (new WebDriverWait(driver, 30))   
     .until(ExpectedConditions.presenceOfElementLocated(By.id(BUTTON_DISPLAY_ID))); 

    buttonDisplay.click(); 
} 
} 

有沒有更好的辦法,例如像這樣 WebElement myDynamicElement =(新WebDriverWait(驅動程序,30) )
.until(ExpectedConditions。 presenceOfWebElement(buttonDisplay));

(我發現了一個迎接解釋here

感謝您的幫助

回答

0
public boolean isElementPresent(WebElement element){ 
    try{ 

    WebElement; 

    return true; 

} 
catch(Excception e){ 

System.out.println("Element not present!!"); 
return false; 

} 
} 
+0

請編輯您的答案,包括一些解釋。僅有代碼的答案對未來SO讀者的教育很少。您的回答是在低質量的審覈隊列中。 – mickmackusa