Python Selenium Converting .Text into int

Viewed 616

I have recently been trying to get an integer from a text attribute in Selenium

print(Pre_Day_Difference.text)
final = -Pre_Day_Difference.text
print(final)

I want to try to negate the pre_day_difference.text but the only way to do that is to turn it into an integer and I would need an integer for later calculations as well. The first print comes out as (2.33%) but the final print crashes giving me a

"TypeError: bad operand type for unary -: 'str'" error.

Instead of "-2.33%".

Is there a good way to do this? I've tried multiple ways like putting it in int() etc.

1 Answers

Just right-strip the %, convert to int and negate:

final = -int(Pre_Day_Difference.text.rstrip("%"))
Related