I'm writing a common library to setup an automation test suite with Selenium 2.0 Python's webdriver.
def verify_error_message_present(self, message):try:self.driver.find_element_by_xpath("//span[@class='error'][contains(.,'%s')]" % message)self.assertTrue(True, "Found an error message containing %s" % messageexcept Exception, e:self.logger.exception(e)
I would like to escape the message before passing it to XPath query, so it can support if 'message' is something like "The number of memory slots used (32) exceeds the number of memory slots that are available (16)"
Without escaping, the xpath query won't work since it contains '(' and ')'
Which library can we use to do this in Python?
I know that this is a simple question, but I don't have so much experience in Python (just started).
Thanks in advance.
Additional info:
During testing in firebug, the query below will return no result:
//span[@class='error'][contains(.,'The number of memory slots used (32) exceeds the number of memory slots that are available (16)')]
While the query below will return the desired component:
//span[@class='error'][contains(.,'The number of memory slots used \(32\) exceeds the number of memory slots that are available \(16\)')]
Logically this problem can be solved by replacing )
with \)
for this particular string literal, but then there are still the other characters need to be escaped. So is there any library to do this in a proper way?