Search code examples
pythonseleniumweb-scrapingscreen-scraping

get number from "value" from a web page using python


So I used python to open a web page in which python would enter a number in a box (amount) like 100 then the website generates a number in another box (quantity) or vice versa, the price changes every day.

the webpage code for the quantity box after it generated the value (quantity) is shown below:

<div class="sc-62mpio-0 sc-1c2873k-4 jclRvn">
<input type="number" id="FormRow-BUY-quantity" name="quantity" step="0.00001" min="0.00001" class="sc-1c2873k-1 fIfOtX" value="2.37812">
<span class="sc-1c2873k-7 dmFXTy">LTC</span>
</div>

I want to save the value generated in the code above (2.37812).

the XPath for the quantity box mentioned above is:@id="FormRow-BUY-quantity

I used the following code to send the amount of 100 into the amount box

  driver.find_element_by_xpath('(//input[@name="total"])[1]').send_keys("100", Keys.ENTER)

I would like to store the value in a string to do some math with it.


Solution

  • Ideally you should be using get_attribute which can read html attribute value for any tag

    text1=driver.find_element_by_xpath('//*[@id="FormRow-BUY-quantity"]').get_attribute("value")
    print(text1)
    

    If you want to convert read attribute from String to Float then you can use it like

    text1=driver.find_element_by_xpath('//*[@id="FormRow-BUY-quantity"]').get_attribute("value")
    print(float(text1))