在 Selenium 自动化中,我使用下面的代码检查元素显示状态
public boolean isDisplayed(String xpath){
try{
return driver.findElement(By.xpath(xpath)).isDisplayed();
}catch (Exception e){
return false;
}
}
但问题是,如果元素没有显示,它将转到catch语句,并且执行catch语句需要更多时间。怎样才能让它快点呢。你能帮我一下吗?提前致谢
最佳答案
首先,尝试使用显式等待来等待元素的可用性,然后检查 isDisplayed 属性: 等待的原因是隐式等待元素,因为当元素不可交互时,它会等待元素或达到等待时间。
WebDriverWait wait = new WebDriverWait(WebDriver, timeSpan); wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.xpath(xpath)));
try{
return driver.findElement(By.xpath(xpath)).isDisplayed();
}
catch (Exception e){
return false;
}
关于java - catch 方法执行时间较长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58353880/