1

If I use "Inspect Element" on FireFox, I see this:

<span class="stock-number-value ng-binding">17109 </span>

When I use driver.getSource(), that 17109 is replaced by "vehicle.attributes.stockNumber".

My main goal is to use the driver to get whatever value is stored by vehicle.attributes.stockNumber, but I can't figure out how to get the contents of that variable using Selenium.

1 Answer 1

2

I suspect you are getting the source too early in the process while the angular is not yet ready and the bindings are not yet data-feeded. I'd use a custom wait condition function to wait for the "stock" to have a numeric value:

WebDriverWait wait = new WebDriverWait(webDriver, 510);
WebElement stock = wait.until(waitForStock(By.cssSelector(".stock-number-value")));
System.out.println(stock.getText());

where waitForStock is something along these lines:

public static ExpectedCondition<Boolean> waitForStock(final By locator) {
  return new ExpectedCondition<Boolean>() {
    @Override
    public Boolean apply(WebDriver driver) {
      try {
        WebElement elm = driver.findElement(locator);
        return elm.getText().trim().matches("[0-9]+");
      } catch (NoSuchElementException e) {
        return false;
      } catch (StaleElementReferenceException e) {
        return false;
      }
    }

    @Override
    public String toString() {
      return "stock is not yet loaded";
    }
  };
}
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, I hadn't ever used the WebDriverWait object before. Thank you for defining "elm.getText().trim().matches("[0-9]+");" as well, you saved me some time there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.