我一直在使用 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");
}
最佳答案
尝试使用 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();
这link可以帮助您入门。
关于c# - 使用 Selenium for C# 登录 Facebook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10978912/