C# 硒在铬中找不到元素

C# 硒在铬中找不到元素,c#,google-chrome,selenium,C#,Google Chrome,Selenium,设置测试: public void SetupTest() { IWebDriver driver = new ChromeDriver(); selenium = new DefaultSelenium( "localhost", 4444, "*googlechrome C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe", "ht

设置测试:

public void SetupTest()
{
    IWebDriver driver = new ChromeDriver();

    selenium = new DefaultSelenium(
        "localhost",
        4444,
        "*googlechrome C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe", 
        "http://localhost");
    selenium.Start();
    verificationErrors = new StringBuilder();
}
测试功能:

[Test]
public void LoginTest()
{
    selenium.Open("http://localhost:8085/");

    // login
    for (int second = 0; ; second++)
    {
        if (second >= 60) Assert.Fail("timeout");
        try
        {
            if (IsElementPresent(By.CssSelector("#username"))) break;
        }
        catch (Exception)
        { }
        Thread.Sleep(1000);
    }

    driver.FindElement(By.Id("username")).SendKeys("admin");
    driver.FindElement(By.Id("password")).SendKeys("123456");
    driver.FindElement(By.CssSelector(".btn.btn-primary")).Click();
}

private bool IsElementPresent(By by)
{
    try
    {
        driver.FindElement(by);
        return true;
    }
    catch (NoSuchElementException)
    {
        return false;
    }
}
我通过以下链接下载Chrome驱动程序:

最新版本是2.7

我的chrome版本是31.0.1650.63

问题是,驱动程序无法找到元素,尽管它存在于视图中


如何使其工作?

我建议使用Selenium IDE并在Selenium IDE中录制时手动登录。它将帮助您确定要选择的元素的正确名称

我用那种东西

using Selenium;

ISelenium sel = new DefaultSelenium("localhost", 4444, "*firefox", "");
sel.Start();
sel.Open("www.whateveryourwebsideis.com");

sel.Type("id=user_email", "username");
sel.Type("id=user_password", "password");
sel.Click("name=commit");
更新: 在我看来,好像你没有使用IDriver导航

你有

selenium.Open("http://localhost:8085/");
但我想你应该用

driver.Navigate().GoToUrl("http://localhost:8085/");
试一试

加载页面后,检查是否确实有任何HTML可用于搜索元素


我刚刚试着安装ChromeDriver,但它实际上不起作用,我实际上也不需要它,所以我恐怕只能让你自己来寻找解决方案了……祝你好运。

成功了!!!请帮助我使用
驱动程序
:)为什么驱动程序无法识别正确的元素?由于某些原因,驱动程序从未为我工作过,所以我只使用ISelenium类。不知道两者的区别是什么,为什么两者都存在…但由于ISelenium对我来说很好,我从未尝试过驱动程序…所以对不起,伙计,我帮不了你。使用RC或WebDriver-停止混合它们。我强烈建议使用ChromeDriver“从不工作”的原因是因为您正在这样做。@skhro87:
driver.Navigate().gotour('http://localhost:8085/");不客气。希望你能读到Arran写的东西…不要把RC和WebDriver搞混了。你似乎是在混合技术。为什么要一起使用Selenium RC和WebDriver?我并不奇怪这不起作用。重新开始。阅读文档。我使用Selenium IDE作为chrome插件,如果我使用“验证元素存在”,它也找不到元素。但是,如果我点击放大镜图标,它会显示页面上的元素。。。不知道该怎么办。
string htmlSource = driver.PageSource;