Jacek Góraj Jacek Góraj - 2 months ago 7
HTML Question

how to get float value from element?

I can't find answer to my problem. I need to get value 0.01 from below and then operate with it, eg. add some value etc.

Let's say we have an HTML:

<html>
<body>
<div>Exchange rate</div>
<span>0.01</span>
</body>
</html>


So the xpath is

„//div[text() = ‘Exchange rate’]/../span”


In protractor I manage to create element variable:

var element = element(by.xpath(„//div[text() = ‘Exchange rate’]/../span”));


Now I need to get float value from it and use it later in code.

How can I do it?

Answer

To start with, your html is non-angular and using 'element' will throw you errors. I am not sure if the code snippet is just an example and if you are actually operating on angular pages, so I will answer both cases

For Non-Angular case:

browser.driver.get("<<>>/test.html"); browser.driver.findElement(by.xpath('//span')).getText().then(function(value){ console.log("The Float value is " + parseFloat(value)) });

For Angular pages:

    browser.get("<<>>/test.html");
    element(by.xpath('//span')).getText().then(function(value){
        console.log("The Float value is " + parseFloat(value))
    });
Comments