我正在编写一个通用库,以使用Selenium 2.0 Python的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" % message
except Exception, e:
self.logger.exception(e)
我希望在将消息传递给XPath查询之前对其进行转义,以便支持“message”为“使用的内存插槽数量(32)超过可用的内存插槽数量(16)”等内容。
如果不进行转义,XPath查询将无法正常工作,因为它包含“(”和“)”。
我们可以在Python中使用哪个库来完成这项任务?
我知道这是一个简单的问题,但我在Python方面没有太多经验(刚刚开始)。
谢谢。
附加信息:
在firebug中进行测试时,以下查询将不返回任何结果:
//span[@class='error'][contains(.,'The number of memory slots used (32) exceeds the number of memory slots that are available (16)')]
虽然下面的查询将返回所需的组件:
//span[@class='error'][contains(.,'The number of memory slots used \(32\) exceeds the number of memory slots that are available \(16\)')]
从逻辑上讲,这个问题可以通过将特定的字符串文字中的)
替换为\)
来解决,但是仍然需要转义其他字符。因此,是否有任何库可以以正确的方式处理这个问题?