3

我一直在玩 Selenium C# 框架并尝试登录 Facebook,但没有任何运气。

这是我到目前为止所得到的(基于这篇文章:Testing a Facebook Connect application using Selenium?)。我无法让它工作。

ISelenium sel = new DefaultSelenium("localhost", 4444, "*chrome", "http://facebook.com");
public void LoginWithEmailAndPassword(string email, string pass)
{
    sel.Start();
    sel.Open("http://www.facebook.com");
    sel.ClickAt("//img[\\@alt='Facebook']", "User clicks on Facebook Login");
    sel.WaitForPopUp("", "3000");
    sel.SelectPopUp("");
    sel.Type("email", email);
    sel.Type("pass", pass);
    sel.KeyPress("pass", "\\13");
    sel.SelectWindow("null");
}

因此,如果有人可以为我提供一些如何执行此操作的示例代码,或者指导我朝着正确的方向前进,我们将不胜感激。

解决

通过使用 Firefox Selenium 插件的记录功能,我得到了 id 和我需要让它工作的功能。登录按钮似乎需要一个 XPath 定义,我也是从 Selenium IDE 获得的。

ISelenium sel = new DefaultSelenium("localhost", 4444, "*chrome", "http://facebook.com");
public void LoginWithEmailAndPassword(string email, string pass)
{
    sel.Start();
    sel.Open("/");
    sel.Type("id=email", email);
    sel.Type("id=pass", pass);
    sel.Click("//label[@id='loginbutton']/input");
}
4

1 回答 1

1

尝试使用 WebDriver:

IWebDriver driver = new FireFoxDriver();
driver.GoToUrl("www.facebook.com");

var element = driver.FindElement(By.Id("email"));
element.SendKeys(email);
...
element = driver.FindElement(By.Id("submit button id"));
element.Click();

链接可以帮助您入门。

于 2012-08-31T16:24:01.077 回答