使用webdriver,我能够成功运行第一个用户&对于下一个用户,它在if条件下失败。如果条件不正常

时间:2012-12-12 09:45:23

标签: webdriver selenium-webdriver

如果情况不正常。我有一些用户ID登录我的应用程序,使用webdriver,我能够成功运行第一个用户&对于下一个用户,它在if条件下失败。请找到下面的代码,如果条件成功运行,则必须检查更多代码。

for (int i = 1; i < sh.getRows(); i++)
{           
  while(iter.hasNext())
  {
   System.out.println("Main Window ID :"+iter.next());
  }
 driver.findElement(By.id("lgnLogin_UserName")).clear();
    driver.findElement(By.id("lgnLogin_UserName")).sendKeys(sh.getCell(0,   
  i).getContents());
    driver.findElement(By.id("lgnLogin_Password")).clear();
    driver.findElement(By.id("lgnLogin_Password")).sendKeys(sh.getCell(1, 
  i).getContents());
    driver.findElement(By.id("lgnLogin_LoginButton")).click();
    Thread.sleep(5000L);

    if(driver.findElements(By.linkText("Logout")) != null)
        {

  driver.findElement(By.id("ctl00_Header_Lbtn_Logout")).click();
            msg ="Valid User Login";
            System.out.println(msg);
        }
else
    if(driver.getTitle().contains("700Dealers Inc."))
        {
            driver.findElement(By.xpath("//table[@id='lgnLogin']/tbody
  /tr/td/table/tbody/tr[4]/td")).getText();
            System.out.println(msg);
        }
        else
        if(driver.getTitle().contains("Security Question And Answers"))
        {
            driver.findElement(By.xpath("//table[@id='Table_01']/tbody
 /tr[5]/td/table/tbody/tr/td/table/tbody/tr/td/span/span[1]")).getText();
            System.out.println(msg);
        }
        else
        if(driver.getTitle().contains("700 credit Change Password"))
        {
            driver.findElement(By.xpath("//div[@id='panelscreen']/table
 /tbody/tr/th/span")).getText();
            System.out.println(msg);
        }

请帮我解决这个问题。将不胜感激。

1 个答案:

答案 0 :(得分:1)

Thread.sleep(5000L);可能是你问题的根源。

所以,您可能想要替换它:

Thread.sleep(5000L);
if(driver.findElements(By.linkText("Logout")) != null)

明确等待:

try {
  WebElement logout = (new WebDriverWait(driver, 5))
    .until(new ExpectedCondition<WebElement>(){
       @Override
       public WebElement apply(WebDriver d) {
        return d.findElement(By.linkText("Logout"));
     }});

  //Logout found, do stuff
} catch(TimeoutException e) {

  //No logout element, do stuff
}