How to remove part of text from text gotten from webelement?


How do you remove part of text after you get it from the webpage

matchname = (WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='text-nowrap text-truncate text-muted']"))))

The output of code looks like this : clubname1 vs clubname 2.
I want to remove vs part of it so it only outputs : clubname1 clubname2

I tried using numpy.char.lstrip(array, chars=value) to remove "vs" from text, but it didn’t work.

Asked By: ElBob



You can applay replace() method on received string replacing the unwanted substring with nothing, as following:

matchname_text = (WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='text-nowrap text-truncate text-muted']")))).text
wanted_text = matchname_text.replace('vs','')
Answered By: Prophet