0

I use a screen scraper to get the value of my account balance, I would like to get 10% of that value but it gives this error: TypeError: can't multiply sequence by non-int of type 'float'. The code looks like this:

account_balance = driver.find_element_by_xpath("/html/body/div[1]/div/div[1]/div[2]/main/section/div/div[1]/section[2]/button[1]/span[1]") 
account_balance_value = account_balance.get_attribute("title")  

The value that it gives right now is

print(account_balance_value) 
0,10

So I change it to this

account_balance_value = account_balance_value.replace(',', '.') 
print(account_balance_value)  
0.10

When I print this it gives the error:

print(account_balance_value * 0.1)   
TypeError                                 Traceback (most recent call last)
<ipython-input-102-536a3835612f> in <module>
----> 1 print(account_balance_value * 0.1)

TypeError: can't multiply sequence by non-int of type 'float'

If I just run this it's fine

print(0.10 * 0.1) 
0.010000000000000002

What did I do wrong here?

4 Answers4

1

get_attribute(attribute_name) would return a string. So account_balance_value is of type string hence further you can manipulate the string using replace() as:

account_balance_value = account_balance_value.replace(',', '.')

So by all possible means, through the line of code:

print(account_balance_value)  

you are printing a string and you can't multiply the string account_balance_value with a float i.e. 0.1


Solution

You need to convert the string value within account_balance_value to a float using float() and then print the result as follows:

print(float(account_balance_value) * 0.1)

Reference

You can find a relevant detailed discussion in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

Your account_balance_value seems to be a string. Try

print(float(account_balance_value) * 0.1)   
Timbolt
  • 196
  • 6
0

This is probably because your variable account_balance_value is a string.

Try adding:

account_balance_value = float(account_balance_value)

to convert it to a float, before running your other code.

If that also doesn't work, it could be because it contains a comma, so you will have to separate the two values before running that code.

Gavin Wong
  • 1,254
  • 1
  • 6
  • 15
0

This error occurs because the variable account_balance_value is of string type. For performing any mathematical operations you have to convert that variable to float type using the function float().

SYNTAX= float(variable name)

here,
account_balance_value=float(account_balance_value) is the correction that has to be done.

kadamb
  • 1,532
  • 3
  • 29
  • 55