Reputation: 158
I am creating a bot that would automate my work and copy particular values from a particular website. Everything works fine but the last lines of my code that says w.text produces an outcome which is text and I need a number. Each element that I need the value of looks like this after inspection:
<span class="good">€25,217.65</span>
How do I get the value as a number instead of as a text? I tried w.value or w.get_attribute('value) but it doesn't work. Here is my program (excluding downloads of libraries and files)
driver = webdriver.Chrome(driver_path)
driver.get('https://seabass-admin.igp.cloud/')
# waiting for login table to load
try:
element = WebDriverWait(driver,10).until(
ec.presence_of_element_located((By.XPATH,'//*[@id="email"]'))
)
except:
driver.quit()
#entering sensitive info
driver.find_element_by_id("email").send_keys(pwx.em) # login details
driver.find_element_by_id("password").send_keys(pwx.pw) # password
details
driver.find_element_by_xpath('//*[@id="appContainer"]/div/form/button').click() # click sign in
# waiting for page to load
try:
element = WebDriverWait(driver,10).until(
ec.presence_of_element_located((By.XPATH,'//*
[@id="testing"]/section/section[4]/div/table/tbody/tr[2]/td[3]/span'))
)
except:
driver.quit()
# getting info from the page
w = driver.find_element_by_xpath('//*
[@id="testing"]/section/section[4]/div/table/tbody/tr[2]/td[3]/span')
cell = outcome['import']
cell[withdrawal_cell].value = w.text
Upvotes: 1
Views: 2663
Reputation: 193208
As per the HTML you have shared:
<span class="good">€25,217.65</span>
The text €25,217.65 is the innerHTML.
So, you can extract the text €25,217.65 using either:
w.get_attribute("innerHTML")
Now to get the value €25,217.65 as a number instead of text you need to:
Remove the €
and ,
character using re.sub()
:
import re
string = "€25,217.65"
my_string = re.sub('[€,]', '', string)
Finally, to convert the string to float you need to pass the string as an argument to the float()
as follows:
my_number = float(my_string)
So the entire operation in a single line:
import re
string = "€25,217.65"
print(float(re.sub('[€,]', '', string)))
Effectively, your line of code can be either of the following:
Using text attribute:
cell[withdrawal_cell].value = float(re.sub('[€,]', '', w.text))
Using get_attribute("innerHTML")
:
cell[withdrawal_cell].value = float(re.sub('[€,]', '', w.get_attribute("innerHTML")))
Upvotes: 1
Reputation: 3503
You could use some of Python's built in functions for that:
Specifically:
str_w = w.text # this is the '€25,217.65' string
digits=str_w.strip('€').replace(',','') # use the functions above to get number-like string
cell[withdrawal_cell].value = float(digits) # convert to float number
Upvotes: 1