Ral*_*lph 121 java testing webdriver selenium-webdriver
如何检查Web驱动程序是否存在元素?
真正使用try catch是唯一可行的方法吗?
boolean present;
try {
driver.findElement(By.id("logoutLink"));
present = true;
} catch (NoSuchElementException e) {
present = false;
}
Run Code Online (Sandbox Code Playgroud)
Mik*_*wan 229
你也可以这样做:
driver.findElements( By.id("...") ).size() != 0
Run Code Online (Sandbox Code Playgroud)
这节省了令人讨厌的尝试/捕获
Edd*_*Edd 50
我同意迈克的回答,但是如果没有找到可以打开/关闭的元素,则会有一个隐含的3秒等待,如果您执行此操作很多很有用:
driver.manage().timeouts().implicitlyWait(0, TimeUnit.MILLISECONDS);
boolean exists = driver.findElements( By.id("...") ).size() != 0
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
Run Code Online (Sandbox Code Playgroud)
如果您正在运行大量测试,那么将它放入实用程序方法应该可以提高性能
正如评论所述,这是在C#而不是Java,但想法是一样的.我已经广泛研究了这个问题,最终问题是,当元素不存在时,FindElement总是返回异常.没有重载选项允许您获取null或其他任何内容.这就是我更喜欢这种解决方案的原因.
一旦创建了方法,它实际上是非常简单和优雅的.通过使用FindElementSafe而不是FindElement,我没有"看到"丑陋的try/catch块,我可以使用简单的Exists方法.这看起来像这样:
IWebElement myLink = driver.FindElementSafe(By.Id("myId"));
if (myLink.Exists)
{
myLink.Click();
}
Run Code Online (Sandbox Code Playgroud)以下是扩展IWebElement和IWebDriver的方法
IWebDriver.FindElementSafe
/// <summary>
/// Same as FindElement only returns null when not found instead of an exception.
/// </summary>
/// <param name="driver">current browser instance</param>
/// <param name="by">The search string for finding element</param>
/// <returns>Returns element or null if not found</returns>
public static IWebElement FindElementSafe(this IWebDriver driver, By by)
{
try
{
return driver.FindElement(by);
}
catch (NoSuchElementException)
{
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
IWebElement.Exists
/// <summary>
/// Requires finding element by FindElementSafe(By).
/// Returns T/F depending on if element is defined or null.
/// </summary>
/// <param name="element">Current element</param>
/// <returns>Returns T/F depending on if element is defined or null.</returns>
public static bool Exists(this IWebElement element)
{
if (element == null)
{ return false; }
return true;
}
Run Code Online (Sandbox Code Playgroud)
您可以使用多态来修改FindElement的IWebDriver类实例,但从维护的角度来看这是个坏主意.
我扩展了 Selenium WebDriver 实现,在我的例子中 HtmlUnitDriver 公开了一个方法,
public boolean isElementPresent(By by){}
Run Code Online (Sandbox Code Playgroud)
像这样:
这是我的代码:
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
public class CustomHtmlUnitDriver extends HtmlUnitDriver {
public static final long DEFAULT_TIMEOUT_SECONDS = 30;
private long timeout = DEFAULT_TIMEOUT_SECONDS;
public long getTimeout() {
return timeout;
}
public void setTimeout(long timeout) {
this.timeout = timeout;
}
public boolean isElementPresent(By by) {
boolean isPresent = true;
waitForLoad();
// Search for elements and check if list is empty
if (this.findElements(by).isEmpty()) {
isPresent = false;
}
// Rise back implicitly wait time
this.manage().timeouts().implicitlyWait(timeout, TimeUnit.SECONDS);
return isPresent;
}
public void waitForLoad() {
ExpectedCondition<Boolean> pageLoadCondition = new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver wd) {
// This will tel if page is loaded
return "complete".equals(((JavascriptExecutor) wd).executeScript("return document.readyState"));
}
};
WebDriverWait wait = new WebDriverWait(this, timeout);
// Wait for page complete
wait.until(pageLoadCondition);
// Lower implicitly wait time
this.manage().timeouts().implicitlyWait(100, TimeUnit.MILLISECONDS);
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
CustomHtmlUnitDriver wd = new CustomHtmlUnitDriver();
wd.get("http://example.org");
if (wd.isElementPresent(By.id("Accept"))) {
wd.findElement(By.id("Accept")).click();
}
else {
System.out.println("Accept button not found on page");
}
Run Code Online (Sandbox Code Playgroud)
小智 5
每次都对我有用:
if(!driver.findElements(By.xpath("//*[@id='submit']")).isEmpty()){
//THEN CLICK ON THE SUBMIT BUTTON
}else{
//DO SOMETHING ELSE AS SUBMIT BUTTON IS NOT THERE
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
314416 次 |
最近记录: |