How do I stop Selenium from trimming the space at the end of my string?

73 Views Asked by At

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:'
2

There are 2 best solutions below

1
Suramuthu R On

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.

response_text = driver.find_element(By.TAG_NAME, "span").text
response_text = f'{response_text} '
assert response_text == "Response: "

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.

0
sytech On

Your test appears to be catching an actual issue in your HTML when used by your browser. This has to do with how your browser is choosing to parse the HTML. In some cases, trailing spaces are omitted by the browser. The problem is not selenium or your test -- your HTML is interpreted by the browser this way and must change if you want the ensure the behavior always results in an actual space.

For example, suppose you have an HTML page simply with <span>test </span> and you open this page in firefox, the DOM shows the HTML without the space (it also corrects some errors automatically by adding the html, head, and body tags):

<html>
  <head></head>
  <body>
    <span>test</span>
  </body>
</html>

Notice that the space has been removed. This happens despite the fact that the actual source of the same page is simply <span>test </span>. The behavior may change depending on the parser (browser) used, which is why you don't see consistent behavior between browsers (among many many other differences between browsers).

This is also the case when this value is retrieved by the selenium API:

>>> driver.find_element(By.TAG_NAME, "span").text
'test'

To prevent this from happening, you should use an explicit space like &nbsp; to prevent browser parsers from misunderstanding this trailing space.

For example, if you use the following HTML:

<span>Response:&nbsp;</span>

Then your browser (and your code) will produce the correct text:

>>> driver.find_element(By.TAG_NAME, "span").text
'Response: '