srinuvasareddy marri
srinuvasareddy marri

Reputation: 69

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

While executing the code when the webelement "err" is null then webdriver taking too much time for executing the if block but "err" is not null webdriver going to the else block and driver getting closed then ok

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();
}   

please advise thanks in advance

Upvotes: 0

Views: 2125

Answers (1)

aholt
aholt

Reputation: 2981

try this:

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);
}

This will tell the driver to only take 1 second to search for the "err" element before throwing an exception. It will also reset the implicit wait, even in the event of an exception.

Upvotes: 2

Related Questions