How do I stop Selenium from trimming the space at the end of my string?
The space is necessary in the app, so I want to make sure that the space stays so that I can assert the proper spacing is happening!
This is what the HTML itself looks like:
<span>Response: </span>
I confirmed for absolute sure that the space is displaying on the page by editing that span to remove that space, and it did indeed remove the space between this element and the result.
It's being trimmed in Chrome and in Firefox but not in Safari. I ran the test against all three browsers and confirmed.
What should I be doing to stop the space from being trimmed?
Currently using Selenium 4.16.0 and Python 3.12.1
Here's what I'm currently doing:
response_text = driver.find_element(By.TAG_NAME, "span").text
assert response_text == "Response: ", print("Endpoint 1 should have returned 'Response: ' but returned '" + str(response_text) + "'")
And here's the relevant output of my test:
Expected :'Response: '
Actual :'Response:'
I had answered this question. looks like the question has been deleted.
What I understand from your answer is: if space is there at the end of any text, then it is trimmed.(I understand that not all the spaces are trimmed, because I'm sure that doesn't happen). That is the default behaviour of html tag, i.e if any space found at the end of any tag, it assumes itself as an unwanted space. The following should solve the problem.
Use f-string or string literal for better clarity. That's nothing but prefixing the string with an
f.(being string between single or double quote) And if any objects(variables, function to be called, numbers etc.,) to be added inside the string, that needs to be placed inside the curly braces. In the above code, you'll finf a space after the curly brace as a part of the string.