【问题标题】:Selenium web driver, C#Selenium 网络驱动程序,C#
【发布时间】:2012-11-23 10:21:49
【问题描述】:

我是硒和网络驱动程序的新手。我的问题很简单。抱歉,如果您发现这个问题非常基本。 我正在使用带有 selenium 2 的 Visual Studio 2010 Ultimate,并在浏览器 IE 9 中使用语言 C#。我尝试执行以下简单代码。

IWebDriver driver = new InternetExplorerDriver(@"D:\IEDriverServer\");
driver.Navigate().GoToUrl("http://www.google.com");


System.Console.WriteLine("Page title is: " + driver.Title);

Console.WriteLine(driver.Title);
Console.WriteLine("Waiting to find element");

IWebElement returnedValue = driver.FindElement(By.Name("q"));

Console.WriteLine("Element Found");

上面的代码抛出Unable to find the element with name==... 我认为这可能是浏览器加载问题。浏览器没有完全加载,因此导致它显示错误。然后我在 driver.navigate 行之后添加了下面的代码,让它等到浏览器完全加载。

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
wait.Until(ExpectedConditions.TitleIs("Google"));

奇怪的是它再次忽略了这个 webdriverwait 行并直接跳转到“wait.until”行,导致显示元素未找到错误。我应该怎么办。我在这里错过了什么吗??

【问题讨论】:

  • 我不使用 c# for selenium 但它不应该是 driver.FindElementByName("q") 在 python 中是这样的。
  • 谷歌网站加载了吗?

标签: selenium element


【解决方案1】:

您在定义等待方面几乎是正确的......但在 C# 中,有不同的方法可以启用 Webdriver 等待,直到元素可见,直到页面加载。

        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
        IWebElement myDynamicElement = wait.Until<IWebElement>((d) =>
        {
            return d.FindElement(By.Name("q"));
        });

希望这会有所帮助...所有最好的伙伴 :)

【讨论】:

    【解决方案2】:

    尝试使用driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10)); 设置隐式等待。

    根据Selenium documentation,默认值为 0 - 也就是说,如果没有立即找到该元素,Selenium 会抛出“无法找到具有...的元素”错误。

    【讨论】:

      【解决方案3】:

      更改wait.Until(ExpectedConditions.TitleIs("Google"));

      wait.Until(ExpectedConditions.visibilityOfElementLocated(By.name("q")))

      可能会出现页面标题已更改为google但未完全加载完整页面的情况

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-20
        • 2011-07-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多