Skip to content
Advertisement

How to get dynamic numbers using xpath?

How do I get dynamically changing numbers in the span block ?

<div class="main-page-exchange__indicator">
   <span class="main-page-exchange__rate">72,54</span></div>
</div>

Method:

@Test
public void first(){
    chromeDriver.get("https://www.open.ru/");
    WebElement buyRateUSD = chromeDriver.findElement(By.xpath("//span[@class='main-page-exchange__rate']"));
    System.out.println("out" + buyRateUSD);
}

I can’t figure out how to do this. My method xpatch which returns the value:

"out[[ChromeDriver: chrome on WINDOWS (7ec6711c86cc8089e3bd06c161cdebf8)] -> xpath: //span[@class='main-page-exchange__rate']]" 

How can I get dynamically changing numbers in such blocks for further comparison?

Advertisement

Answer

chromeDriver.get("https://www.open.ru/");
WebElement buyRateUSD = chromeDriver.findElement(By.xpath("//span[@class='main-page-exchange__rate']"));
System.out.println("out" + buyRateUSD.getText());

byRaetUSD is a webelement you should call getText to get the text or you can call

  buyRateUSD.getAttribute("textContent") 

if element is not in the view port as getText finds inner test and will give consideration to isdisplayed . if isdisplayed is falls getText will return empty string. getAttribute(“textContent”) doesn’t care about isDisplayed so it will return text if element is present

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement