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

时间:2015-06-22 11:39:47

标签: selenium selenium-webdriver webdriver

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

请指教 提前谢谢

1 个答案:

答案 0 :(得分:1)

试试这个:

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秒钟来搜索"错误"在抛出异常之前的元素。即使发生异常,它也会重置隐式等待。