-2

I have a selenium Webdriver script that has if else loops. When I execute the script, the code always stops at the first if condition and does not move to the else part at all. And then i get an Exception as the if condition is false. Is there any way out to work around the looping part in selenium web driver? I am using C# for coding. Thanks in Advance.

Following is the looping structure i have used in my script:

    if (driver.FindElement(By.Id("ctl00_ContentMain_Label_Error")).Text.Contains("The username is already registered with Current Brand"))
{
    //take a screen shot
}

else if (driver.FindElement(By.Id("ctl00_Label_WelcomeMessage")).Text == "You are not currently signed in")
{
    //take a screen shot
}
else if (driver.FindElement(By.Id("ctl00_ContentLeftNav_UserNavBar_MenuItemLogOut")).Text == "Log Out")
{

//take a screenshot
}
4

1 回答 1

0

实际上,在发布的代码中,您尝试比较从中检索到的文本driver.FindElement(By.Id("ctl00_Label_WelcomeMessage")).Text和您的own text.

您从代码中获得的文本返回一个String数据类型。因此,您可以使用字符串操作概念来比较字符串数据类型。使用equals,equalsIgnoreCasecontains...

尝试这个:

 else if (driver.FindElement(By.Id("ctl00_Label_WelcomeMessage")).Text.equals("You are not currently signed in"))
{
//take a screen shot
}

代替

else if (driver.FindElement(By.Id("ctl00_Label_WelcomeMessage")).Text == "You are not currently signed in")
{
//take a screen shot
}
于 2013-02-21T11:25:12.630 回答