2017-04-20 74 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