Selenium 2 WebDriver - Chrome - getting value from a text field set using JavaScript

I am using Selenium 2 (latest release from Googlecode), and I have the ability to launch Chrome and navigate to the URL.

When the page loaded some javascript to set the value of the text field.

I tell him to find the text field by id, which it does, but it does not matter inside it (if I rigidly set its value).

Looking at a PageSource, for example. pe (driver.PageSource); shows html and the text box is empty.

I tried using:

driver.FindElement (By.Id ("txtBoxId") to get the item, and this also does not retrieve the value.

I also tried ChromeWebElement cwe = new ChromeWebElement (driver, "txtBoxId"); (who complains about Stale data).

Any thoughts?

John

+3
source share
3 answers

Selenium 2 does not have wait functions built-in for elements in the DOM. It was the same as in Selenium 1.

If you need to wait for something, you can do it like

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

Or something along these lines

+2
source
Finally I found the answer! This is the code that works for me
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);

Here is my source! http://code.google.com/p/selenium/issues/detail?id=1142

+4
source

webdriver ruby ​​(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 :

    retry_loop do #account for that javascript might fill out values
      assert_contain text, element
    end

As you will notice, there is no penalty for performance if it already exists. Obviously, the opposite case (checking that something is NOT there) should always reach a timeout. I like the way that data is stored in the method and in the test code.

Perhaps you could use something similar in C ++?

+1
source

Source: https://habr.com/ru/post/1765954/


All Articles