2016-03-31 4 views
1

私の要素にテキストがあるかどうかを確認しています。テキストには、文字列内の引用符が含まれます。私の方法はFalseを主張している。 GUI上にテキストがあるので、それがTrueであることを期待しています。Selenium Pythonが文字列の引用符でエラーをスローしています

私は文字列に正しく引用符を含めていません。正しい構文はどうですか?

私は、デバッガを使用してコードを検査するとき、それは言う:

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 

ありがとう、 Riaz

+0

は意図的な 'Do'前に、この二重のスペースですか? – alecxe

+0

'overwritten_element.text'の正確な値は何ですか? – alecxe

+0

私はhtmlを調べるときに二重空間を持っています。 –

答えて

0

同様のテストを作成すると、問題は引用符ではなく二重スペースで表示されることがわかります。 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