Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Selenium 2 WebDriver-Chrome-从通过JavaScript设置的文本框中获取值_C#_Selenium_Selenium Webdriver - Fatal编程技术网

C# Selenium 2 WebDriver-Chrome-从通过JavaScript设置的文本框中获取值

C# Selenium 2 WebDriver-Chrome-从通过JavaScript设置的文本框中获取值,c#,selenium,selenium-webdriver,C#,Selenium,Selenium Webdriver,我使用的是Selenium 2(谷歌代码的最新版本),我让它启动了Chrome并转到一个url 当页面加载了一些javascript时,会执行以设置文本框的值 我告诉它按id查找一个文本框,但它没有这个值(如果我硬编码一个值,它就会找到它) 查看PageSource,例如Console.WriteLine(driver.PageSource);显示html,文本框为空 我试过使用: FindElement(By.Id(“txtBoxId”)来获取元素,而这也不会获取值 我还尝试了ChromeWe

我使用的是Selenium 2(谷歌代码的最新版本),我让它启动了Chrome并转到一个url

当页面加载了一些javascript时,会执行以设置文本框的值

我告诉它按id查找一个文本框,但它没有这个值(如果我硬编码一个值,它就会找到它)

查看PageSource,例如Console.WriteLine(driver.PageSource);显示html,文本框为空

我试过使用:

FindElement(By.Id(“txtBoxId”)来获取元素,而这也不会获取值

我还尝试了ChromeWebElement cwe=newchromewebelement(driver,“txtBoxId”);(它抱怨陈旧的数据)

有什么想法吗


John

Selenium 2没有为DOM中的元素内置等待函数。这与Selenium 1中的情况相同

如果你不得不等待什么,你可以按自己的意愿去做

  public string TextInABox(By by)
  {
    string valueInBox = string.Empty;
    for (int second = 0;; second++) {
      if (second >= 60) Assert.Fail("timeout");
      try
      {
        valueInBox = driver.FindElement(by).value;
        if (string.IsNullOrEmpty(valueInBox) break;
      }
      catch (WebDriverException)
      {}
      Thread.Sleep(1000);
    }
    return valueInBox;
  }

或者类似的东西

我通过ruby使用webdriver(实际上是cucumber watir webdriver),我倾向于这样做:

  def retry_loop(interval = 0.2, times_to_try = 4, &block)
    begin
      return yield
    rescue
      sleep(interval)
      if (times_to_try -= 1) > 0
        retry
      end
    end
    yield
  end
然后,每当由于javascript写入或其他原因出现内容时,我都会将其包装在重试循环中,如下所示:

    retry_loop do #account for that javascript might fill out values
      assert_contain text, element
    end
正如您所注意到的,如果它已经存在,则不会有性能损失。相反的情况(检查是否有东西不存在)显然总是需要达到超时。 我喜欢在方法和测试代码中保持细节清晰的方式


也许你可以用C++中类似的东西?

最后我找到答案!这是我工作的代码

WebDriverWait wait = new WebDriverWait(_driver, new TimeSpan(0,0,60));
wait.Until(driver1 => _driver.FindElement(By.Id("ctl00_Content_txtAdminFind")));
Assert.AreEqual("Home - My Housing Account", _driver.Title);
这是我的消息来源!

似乎它检查结果的速度太快了。添加Thread.Sleep(300);意味着结果被检索到了(尽管我相信他们的selenium方法更好,可以调用它来表示等待结果)。