我为我的selenium脚本编写了一个独立的函数,这样当我想与一个对象交互时,我首先检查它是否存在,为此我已经指定了一些东西,例如识别它的方法和数据使用等,并有一些像这样的方法:-
public WebElement waitforElement(individualThreadSession threadSesh) {
String IDString = threadSesh.objLocVal; //This will be the string value to use to identify
String IDType = threadSesh.objLocType; //This will be something like "CSS"/"XPATH" etc
WebElement returnedElement = null;
for (int second = 0; second < threadSesh.sessionWait; second++) {
Action tempAction = new Action();
tempAction.simpleWait(1);
try {
if(IDType.toString().equals("CSS")){
if(isElementPresent(By.cssSelector(IDString), threadSesh)){
returnedElement = threadSesh.driver.findElement(By.cssSelector(IDString));
break;
}
}
else if(IDType.toString().equals("XPATH")){
if(isElementPresent(By.xpath(IDString), threadSesh)) {
returnedElement = threadSesh.driver.findElement(By.xpath(IDString));
break;
}
}
else if(IDType.toString().equals("ID")){
if(isElementPresent(By.id(IDString), threadSesh)) {
returnedElement = threadSesh.driver.findElement(By.id(IDString));
break;
}
}
else if(IDType.toString().equals("NAME")){
if(isElementPresent(By.name(IDString), threadSesh)){
returnedElement = threadSesh.driver.findElement(By.name(IDString));
break;
}
}
else if(IDType.toString().equals("PARTIALLINKTEXT")){
if(isElementPresent(By.partialLinkText(IDString), threadSesh)) {
returnedElement = threadSesh.driver.findElement(By.partialLinkText(IDString));
break;
}
}
else if(IDType.toString().equals("LINKTEXT")){
if(isElementPresent(By.linkText(IDString), threadSesh)) {
returnedElement = threadSesh.driver.findElement(By.linkText(IDString));
break;
}
}
} catch (Exception e) {System.out.println("When trying to find the obj error encountered - " + e);}
}
return (returnedElement);
}
还有这个:-
public boolean isElementPresent(By myObject, individualThreadSession threadSesh) {
try{
System.out.println("actually check!");
if (threadSesh.driver.findElement(myObject).isEnabled() && threadSesh.driver.findElement(myObject).isDisplayed()) {
return true;
} else {
return false;
}
} catch (NoSuchElementException e){
return(false);
}
}
但是每次我运行它时都会出现以下错误并退出测试:-
Test failed with the following error: - org.openqa.selenium.NoSuchElementException:
Unable to find element with xpath == .//*[@id='main']/div[1]/div[1]/h1/span
这不应该起作用吗?
我认为这可能是因为我还运行了一个 WebDriverEventListener,我猜测它正在捕获此错误并退出测试等,有什么方法可以阻止此事件监听器在此 waitforelement 过程中监听?
如果没有,有没有办法可以检查对象是否启用且可见而不引发异常?
事件监听器如下所示:-
WebDriverEventListener listener = new AbstractWebDriverEventListener() {
@Override
public void onException(Throwable t, WebDriver driver) {
//Add in take screenshot here at some stage!
if(!errorsCaught){
errorsCaught=true;
try{
driver.quit();
}catch(WebDriverException theError){}
System.out.println();
System.out.println(Thread.currentThread().getName() + " has encountered an error ");
individualThreadSession.this.endSession(individualThreadSession.this, t);
}
}
};
更多的挖掘表明,看起来是监听器在 catch 对其执行任何操作之前拾取了东西,所以现在我需要以某种方式找出如何让它忽略错误!有什么想法吗?
最佳答案
我未能在我的事件监听器中发现一个关键行,@Override!这导致我的监听器在我可能也放入的任何捕获之前拾取所有异常,并且我的监听器将终止 session 并按应有的方式关闭运行(即使我不希望它在其中执行某些操作)案例)。
为了解决这个问题,因为我总是在与对象交互之前通过上面相同的路线来检查对象,所以我可以相当确信在尝试任何操作或超时并优雅退出之前该对象将始终存在。
所以我插入了一个基本检查,如下所示:-
t.getClass().getSimpleName().toLowerCase().equals("nosuchelementexception")
因此,如果错误是 nosuchelementexception,我不会费心对其执行任何操作,只需返回到我的循环即可,很简单! :)
关于java - 检查对象是否存在被 webdrivereventlistener 捕获时,Selenium webdriver 出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20118374/