user973718
user973718

Reputation: 245

FindElement(By.Id) not working

This is my first attempt at using Selenium, and I am having trouble with the "FindElement(By.Id". My script uses several By.Name methods, and they work fine.

Here's my code:

// Enter the user name and password 
IWebElement username = FF_Browser.FindElement(By.Name("txtUserName"));
username.SendKeys("user1");
IWebElement password = FF_Browser.FindElement(By.Name("txtPassword"));
password.SendKeys("pw1");
// Click the Login button
FF_Browser.FindElement(By.Name("btnLogon")).Click();

try 
{
bool loginResult = FF_Browser.FindElement(By.Id("lblUserName")).Text == "user1";
return loginResult;   
}
catch (NoSuchElementException)
{
return false;
}

The problem is FindElement(By.Id("lblUserName")) This element is never found. I double and triple checked the in in the source, and it is definitely 'lblUserName'

Is this a bug in Selenium?

[edit]

I have now tried this code with the Firefox and IE driver, and both show the same behavior. I must be missing something basic? I tried refreshing the browser before trying to find the elements, but same result.

Upvotes: 0

Views: 3130

Answers (1)

user973718
user973718

Reputation: 245

Answering my own question: It was of course something simple only a beginner would skip: the element I was trying to find was in a frame.

This one line FF_Browser.SwitchTo().Frame("ApplicationHeaderFrame"); took care of the problem.

Upvotes: 5

Related Questions