FindElement(By.Id) 不起作用
这是我第一次尝试使用 Selenium,我在使用“FindElement(By.Id”) 时遇到了麻烦。我的脚本使用了几个 By.Name 方法,并且它们工作正常。
这是我的代码:
// 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;
}
问题是 FindElement( By.Id("lblUserName"))
这个元素从未被发现。我在源代码中仔细检查了一遍,它肯定是 'lblUserName'
这是 Selenium 中的错误吗? ?
[编辑]
我现在已经在 Firefox 和 IE 驱动程序中尝试了这段代码,两者都显示出相同的行为,我一定缺少一些基本的东西?我在尝试查找元素之前尝试刷新浏览器,但结果相同。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
回答我自己的问题:这当然是一些简单的事情,只有初学者才会跳过:我试图找到的元素在框架中。
这一行
FF_Browser.SwitchTo().Frame("ApplicationHeaderFrame");
解决了这个问题。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.