0

如果条件不能正常工作。我有一组用户 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);
        }

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

4

1 回答 1

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
}
于 2012-12-12T15:58:30.240 回答