Webdriver 在使用 java 的 if 块中执行脚本花费太多时间

Webdriver taking too much time to execute the script in if block using java

当 webelement "err" 为 null 时执行代码时,webdriver 花费太多时间执行 if 块但 "err" 不是 null webdriver 转到 else 块,然后驱动程序关闭好的

driver.findElement(By.id("UHID")).sendKeys("1234440");
driver.findElement(By.id("btnSubmit")).click();
Thread.sleep(100);
WebElement err=null;
try
{
    err=driver.findElement(By.xpath("//*[@id='Error']/div/p"));
}
catch(NoSuchElementException e)
{
    System.out.println("No Such Element Exception.");
}
if(!(err != null && err.isDisplayed()))
{
    Thread.sleep(100);
    Select policytype=new Select(driver.findElement(By.id("PolicyType")));
    policytype.selectByVisibleText("Corporate");                                                                
    //Select Payer
    Thread.sleep(200);
    driver.findElement(By.id("Payer")).sendKeys(Keys.TAB);
    //Payer
    Select Payer=new Select(driver.findElement(By.id("Payer")));
    Payer.selectByIndex(1); 
    driver.findElement(By.id("Submit")).click();
}
else
{
    System.out.println("UHID Not Exist");
    driver.close();
}   

请指教 提前致谢

试试这个:

try
{
    driver.manage().timeouts().implicitlyWait(1000, TimeUnit.MILLISECONDS);
    err=driver.findElement(By.xpath("//*[@id='Error']/div/p"));
}
catch(NoSuchElementException e)
{
    //Log your error
}
finally
{
    driver.manage().timeouts().implicitlyWait(15000, TimeUnit.MILLISECONDS);
}

这将告诉驱动程序在抛出异常之前只需要 1 秒来搜索 "err" 元素。它还会重置隐式等待,即使在发生异常时也是如此。