【问题标题】:Selenium Python include quote in string is throwing errorSelenium Python 在字符串中包含引号引发错误
【发布时间】: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


【解决方案1】:

创建了一个类似的测试后发现问题不在于引号,而在于双空格。 Spaces in HTML are compacted.

from splinter import Browser


def test_dummy():
    with Browser() as browser:
        browser.visit("http://localhost:8888/index.html")
        elem = browser.find_by_id("message_dialog_question_content")

        compare = r"One or more reports use the 'default' prefix and will be overwritten.  Do you wish to continue?"
        print "length of elem : {}, length of compare : {}".format(len(elem.text), len(compare))

        assert elem.text == compare

以上测试输出,

>       assert elem.text == compare
E       assert 'One or more ... to continue?' == 'One or more r... to continue?'
E         Skipping 60 identical leading characters in diff, use -v to show
E         - rwritten. Do you wish to continue?
E         + rwritten.  Do you wish to continue?
E         ?           +
---------- Captured stdout call ----------
length of elem : 94, length of compare : 95

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-05
    • 2011-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多