0
votes

Trying to get numeric dynamic text value from span element. My test is data driven so for each run it needs to get text (dollar amount) from web page and then compare it to expected value in excel. For some reason my code doesn't work. Please help to solve.

My HTML:

$ 445.87

My Locator: By PRICE = By.xpath("//div[@class='price ng-binding'][@ng-hide='calc.isCalculating']/text()");

My Code:

    WebDriverWait wait = new WebDriverWait(driver, 15); 
    wait.until(ExpectedConditions.presenceOfElementLocated(PRICE));

    WebElement actualPriceElm = driver.findElement(PRICE);
    actualPriceElm.getText();

    Assert.assertEquals(actualPriceElm, strExpectedPrice);

My Error: Timed out after 15 seconds waiting for presence of element located by: By.xpath: //div[@class='price ng-binding'][@ng-hide='calc.isCalculating']/text()

When I use my xpath without "/text()" like this //div[@class='price ng-binding'][@ng-hide='calc.isCalculating'] then I get following error:

Exception in thread "main" java.lang.AssertionError: expected [445.87] but found [[[FirefoxDriver: firefox on WINDOWS (f26e0c00-823f-4d45-8285-014303527524)] -> xpath: //div[@class='price ng-binding'][@ng-hide='calc.isCalculating']]]

3
Here is HTML: <div class="price ng-binding" ng-hide="calc.isCalculating"> <span class="dollarSign">$</span> 445.87 <span class="dollarSign"/> </div> - Russ
prior to the wait, what causes the element to appear? in manual testing, what would you do to make the dynamic element present? - CharlieS
Try using By.CssSelector() - vkrams
Charlie, there is a bunch of radio buttons that I select prior to wait. Their combination will result in different pricing. There is clearly issue with my locator and not with the code. For some reason xpath is not working. I will try css as Vikram suggested. - Russ
using xpathtester.com/xpath, i can extract the value you want from the HTML you provide, using the xpath you have specified - CharlieS

3 Answers

0
votes
Wait<WebDriver> wait = new WebDriverWait(driver, 50);

// DO SOMETHING HERE TO CAUSE ELEMENT TO BE DISPLAYED (ie click, hover, mousemove, etc)

wait.until(new Function<WebDriver, Boolean>() {
    public Boolean apply(WebDriver driver) {
        return (driver.findElement(By.xpath("//div[@class='price ng-binding'][@ng-hide='calc.isCalculating']/text()"))).isDisplayed();
    }
});
0
votes

Your xpath //div[@class='price ng-binding'][@ng-hide='calc.isCalculating']/text() returns text 445.87 while you need to find WebElement and get its text. Did you try to use Assert.assertEquals(actualPriceElm.getText(), "445.87");?

0
votes

Found the solution. As Vikram mentioned in his comment I had to use cssSelector instead of xPath.

By.cssSelector("div.priceLine:nth-child(1) > div:nth-child(4) > div:nth-child(2)"); VS. By.xpath("//div[@class='price ng-binding'][@ng-hide='calc.isCalculating']");

Thanks everyone for your suggestions and input.