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

会有一股神秘感。 提交于 2019-12-13 04:34:05

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/30979069/webdriver-taking-too-much-time-to-execute-the-script-in-if-block-using-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!