Selenium WebDriver。蟒蛇。如何在标签中断言中文文本?

时间:2014-01-30 00:05:03

标签: python selenium localization selenium-webdriver non-ascii-characters

我在尝试使用str.decode(),unicode.encode()和其他函数时遇到“ASCII”错误。无法理解如何正确使用它。

我试图建立的HTML代码:

<div class="head">
   <h1 id="localizationHeading">本地化</h1>
</div>

我的断言脚本:

assert driver.find_element_by_id("localizationHeading").text == str.decode("本地化")

我也尝试过使用这个脚本:

assert "本地化" in driver.find_element_by_id("localizationHeading").text

P.S。我的代码顶部是# coding: utf-8

错误:

Traceback (most recent call last):
  File "day2.py", line 53, in <module>
    assert driver.find_element_by_id("localizationHeading").text == str.decode("
本地化")
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 0: ordinal
not in range(128)

这些:

Traceback (most recent call last):
  File "day2.py", line 52, in <module>
    assert "本地化" in driver.find_element_by_id("localizationHeading").text
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 0: ordinal
not in range(128)

assert driver.find_element_by_id("localizationHeading").text == "本地化".decode().encode('utf-8')也不起作用 - 同样的问题。

1 个答案:

答案 0 :(得分:0)

OP解决方案。

.decode().encode('utf-8')替换.decode('utf-8')

assert driver.find_element_by_id("localizationHeading").text == "本地化".decode('utf-8')