【发布时间】:2016-04-01 05:17:54
【问题描述】:
我正在验证我的元素中是否存在某些文本。文本在字符串中包含引号。我的方法是断言 False。我希望它是 True,因为 GUI 上有文本。
我没有在字符串中正确地包含引号。请问正确的语法是什么?
当我使用调试器检查代码时,它会说:
overwritten_element.text = {unicode} u'One or more reports use the \\'default\\'prefix and will be overwritten. Do you wish to continue?
我的方法是:
def is_save_overwrite_dialog_displayed(self):
overwritten_element = self.get_element(By.ID, 'message_dialog_question_content')
return overwritten_element.text == r"One or more reports use the 'default' prefix and will be overwritten. Do you wish to continue?"
带引号的字符串是: 一份或多份报告使用“默认”前缀并将被覆盖。你想继续吗?
我试过了
r"One or more reports use the 'default' prefix and will be overwritten. Do you` wish to continue?"
我已经试过了:
r"One or more reports use the \\'default\\' prefix and will be overwritten. Do you wish to continue?"
HTML 是:
<div id="message_dialog_question_content">
<div>One or more reports use the 'default' prefix and will be overwritten. Do you wish to continue?</div>
</div>
get_element
# returns the element if found
def get_element(self, how, what):
# params how: By locator type
# params what: locator value
try:
element = self.driver.find_element(by=how, value=what)
except NoSuchElementException, e:
print what
print "Element not found "
print e
screenshot_name = how + what + get_datetime_now() # create screenshot name of the name of the element + locator + todays date time. This way the screenshot name will be unique and be able to save
self.save_screenshot(screenshot_name)
raise
return element
谢谢, 里亚兹
【问题讨论】:
-
Do前面的这个双空格是故意的吗? -
overwritten_element.text的确切值是多少? -
当我检查 html 时,它在 Do 之前有一个双空格
-
我已经浏览了代码,在 overwritten_element.text 处设置了一个断点。该值显示 overwritten_element.text = {unicode} u'一个或多个报告使用 \\'default\\' 前缀并将被覆盖。你想继续吗?
-
你能在两个字符串上执行
" ".join(hex(ord(c)) for c in "your text")并分辨出区别吗?
标签: python-2.7 selenium xpath selenium-webdriver