Home > Software engineering >  Python String cleanup from spaces and Unknown empty element
Python String cleanup from spaces and Unknown empty element

Time:10-26

I got attribute from Selenium Element and it contains empty char or spaces: enter image description here

When I double click the result : enter image description here

In VS code: enter image description here

What I tried so far :

string.replace(" ","") #didnt work

So I came with this resolution (I know its bad ):

    edit1 = ticketID[:1]
    ticketF = ticketID.replace(edit1,"")

    edit2 = ticketF[:1]
    ticketE = ticketF.replace(edit2,"")

    edit3 = ticketE[:1]
    ticketD = ticketE.replace(edit3,"")

enter image description here

What Im looking for is what is those blanks ? tabs ? new lines ? how to make it better ?

Edit: enter image description here

ticketID.replace("\n","")
ticketID.replace(" ","")
ticketID.strip()

CodePudding user response:

Those are basically whitespaces, Please use .strip() for any trailing spaces.

In Python, the stripping methods are capable of removing leading and trailing spaces and specific characters. The leading and trailing spaces include blanks, tabs (\t), carriage returns (\r, \n), and the other lesser-known whitespace characters.

If you have ele as an web element.

You can use .text to get the text and then on top of that use .strip()

Probably in your code :

ticketID.text.strip()

CodePudding user response:

They look like lines and not spaces to me.

string.replace("\n","")
  • Related