Selenium等待用户验证码输入C#

时间:2014-11-26 07:55:58

标签: c# selenium

我是Selenium的新手,并使用C#作为登录页面,其中包含用户名,密码和验证码。 问题是,对于验证码,它必须等待用户输入并手动提交表单。 我不知道在这个问题上哪种解决方案是正确的。 请问有更好的方法吗?

这是我的代码。

        var driver = new FirefoxDriver();
        driver.Navigate().GoToUrl("http://someonewebite.com/");

        IWebElement usernameInput = driver.FindElement(By.Name("txtUserName"));
        IWebElement passwordInput = driver.FindElement(By.Name("txtPassword"));

        usernameInput.SendKeys("username");
        passwordInput.SendKeys("password");

        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromMinutes(1));
        wait.Until((d) =>
        {
            return (bool)((RemoteWebDriver)d).ExecuteScript("return document.body.className.match(/\submittedpage\b/).length");
        });

2 个答案:

答案 0 :(得分:4)

我知道,我们无法在硒中自动化Captcha ...验证码的主要优点是它不会被任何自动化语言破坏,这就是它的用途。

答案 1 :(得分:1)

我认为如果您无法在没有人工交互的情况下登录,那么使用WebDriverWait的方法很好 - 为什么要更改它? 但是,您不必使用JavaScript来确定您是否已成功登录。也可以在传递给wait.Until()的谓词中使用任何WebDriver方法。 因此,您还可以执行以下操作:

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromMinutes(1));
wait.Until((d) =>
{
    return d.FindElement(…); // here you can use any locator that identifies a successful / failed login
});
相关问题