Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当在Selenium WebDriver中找不到元素时,最快杀死NoSuchElementException或任何异常的方法_Java_Selenium_Selenium Webdriver_Exception Handling_Nosuchelementexception - Fatal编程技术网

Java 当在Selenium WebDriver中找不到元素时,最快杀死NoSuchElementException或任何异常的方法

Java 当在Selenium WebDriver中找不到元素时,最快杀死NoSuchElementException或任何异常的方法,java,selenium,selenium-webdriver,exception-handling,nosuchelementexception,Java,Selenium,Selenium Webdriver,Exception Handling,Nosuchelementexception,在使用try/catch时,是否有最快的方法终止catch中的进程?因为在捕获异常后,通常需要1分钟才能使进程继续进行 我有以下代码: public boolean elementExist(WebDriver driver, By locator){ boolean exist = false; try{ exist = driver.findElements(locator).size()>0; } catch (org.openqa.sele

在使用try/catch时,是否有最快的方法终止catch中的进程?因为在捕获异常后,通常需要1分钟才能使进程继续进行

我有以下代码:

public boolean elementExist(WebDriver driver, By locator){
    boolean exist = false;
    try{
        exist = driver.findElements(locator).size()>0;

    } catch (org.openqa.selenium.NoSuchElementException e) {
        return false;
    }

    return exist;
}
每当脚本没有找到元素时,它会等待1分钟继续。我需要把1分钟缩短到至少5-10秒,因为这太浪费时间了

或者如果有其他方法可以更快地处理元素不存在的情况,请提供帮助

尝试设置

driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
紧接着

WebDriver driver = new FirefoxDriver(); //or ChromeDriver 

隐式等待基本上告诉Selenium嘿,您尝试执行的每个操作都应该在3秒后超时

您的隐式超时是多少?driver.manage.timeouts.implicityWait?如果找不到元素,隐式等待是否会停止1分钟缓冲区?可能,但如果已将implicityWait设置为1分钟,这就是selenium在抛出超时之前等待1分钟的原因。那么,您当前的设置是什么?方法中没有隐式等待:|它更像是一个全局设置,而不是每个方法:可以尝试添加driver.manage.timeouts.implicitlyWait10、TimeUnit.SECONDS;在创建WebDriver的新实例后,请告诉我们结果?