4

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 Answer 1

3

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

final = -int(Pre_Day_Difference.text.rstrip("%"))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.