0

I am having trouble with re.search finding the string "Senders' Domains" on my UI.

word_check1 = 'Senders'
word_check2 = "Senders' Domains"
page_check1, page_check2 = mymessages_page.page_checker(word_check1, word_check2, 'chart-content-container')

When I Debug, my page_checker method finds "Senders//s Domains" but then re.search returns "Senders//sDomains" (removing the space).

def page_checker(self, word_check1, word_check2, ids):
    container = self.driver.find_element_by_id(ids)
    content = container.text
    organized_container_content = content.split('\n')
    for text in organized_container_content:
        if re.search(word_check1, text):
            check1 = text
            for text2 in organized_container_content:
                if re.search(word_check2, text2):
                    check2 = text2
                    return check1, check2
                    break

Is there any way I can escape the single quote (') and space characters so that I can find and match the string "Senders' Domains" on my UI?

Golshy
  • 63
  • 2
  • 13
  • single quote and space aren't special regex chars. Can you create a [mcve] to show your exact issue? – Jean-François Fabre Aug 16 '17 at 14:55
  • 2
    Is there any reason you should use `re.search(word_check1, text)` instead of simple `word_check1 in text`? – Andersson Aug 16 '17 at 14:56
  • Possible duplicate of [How to escape a ( backslash and a single quote) or double quote in python !](https://stackoverflow.com/questions/6717435/how-to-escape-a-backslash-and-a-single-quote-or-double-quote-in-python) – JeffC Aug 16 '17 at 18:03

1 Answers1

1

Have you tried the escape character '\'? https://docs.python.org/2.0/ref/strings.html

>>> word_check2 = "Senders\' Domains"
>>> print (word_check2)
Senders' Domains
>>> 
name goes here
  • 280
  • 4
  • 10
  • 1
    Wouldn't that work without escaping in your case regardless? – Mangohero1 Aug 16 '17 at 15:00
  • Thank you, this worked and helped me match the text found. @mangoHero1 is also right I tried again without escaping and it worked, so it was partly my issue of misunderstanding how re.search works. – Golshy Aug 16 '17 at 15:05
  • Well, glad it worked. - was totally guessing based on your question. I'm new and only now can I even add a comment, could only 'answer' which I think is real silly. – name goes here Aug 16 '17 at 16:50