使用 Selenium for C# 登录 Facebook

2024-05-12

我一直在使用 Selenium C# 框架并尝试进行 facebook 登录,但没有任何运气。

这是我到目前为止得到的(基于这篇文章:使用 Selenium 测试 Facebook Connect 应用程序? https://stackoverflow.com/questions/8845614/testing-a-facebook-connect-application-using-selenium/8867886#8867886)。但我无法让它工作。

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");
}

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

Resolved

通过使用 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();

This link http://blog.stuartfleming.co.uk/selenium/getting-started-with-selenium-2-0-webdriver-and-csharp/可以帮助您开始。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Selenium for C# 登录 Facebook 的相关文章

随机推荐