【问题标题】:Robot Framework ASCII Code Encoding - error "UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' ... ordinal not in range(128)"机器人框架 ASCII 代码编码 - 错误“UnicodeEncodeError: 'ascii' 编解码器无法编码字符 u'\u2019' ...序数不在范围内 (128)”
【发布时间】:2016-04-24 20:42:28
【问题描述】:

我正在寻找解决方法:

UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 4: ordinal not in range(128)

目前我的代码是:

    coding: utf-8
*** Settings ***
Documentation  This is a simple test with Robot Framework
Suite Setup     Connect To Database    pymysql    ${DBName}    ${DBUser}    ${DBPass}    ${DBHost}    ${DBPort}
Suite Teardown  Disconnect From Database
Library         Selenium2Library
Library         DatabaseLibrary
Library         OperatingSystem
Library         String

*** Variables ***
${DBHost}         localhost
${DBName}         robottest
${DBPass}         sahill
${DBPort}         3306
${DBUser}         sahill

*** Test Cases ***
Open Browser To Start Roboting
    [Documentation]  Sarting robot
    [Tags]  Smoke
    Open Browser  http://movieplus.cc/a-z-movies/    firefox
    Maximize Browser Window
    WAIT UNTIL PAGE CONTAINS    have any legal issues please contact appropriate media file owners / hosters.

    Click Element   xpath=//div[@class='wrapper']//div[@class='thumbs']//div[@class='wrap']//div[1]//a
    WAIT UNTIL PAGE CONTAINS    have any legal issues please contact appropriate media file owners / hosters.
    ${count} =  Get Matching Xpath Count     xpath=//div[@class='wrap_content']//ul[@class='list']//li
    ${title} =  get text    xpath=//div[@class='wrap_content']//h2[@class='title']//span[@class='color']
    ${cate} =   get text    xpath=//div[@class='wrap_content']//ul[@class='list']
    ${director} =   get text    xpath=//div[@class='wrap_content']//div[1]//span[@class='desc']
    ${cast} =   get text    xpath=//div[@class='wrap_content']//div[2]//span[@class='desc']
    ${content} =    get text    xpath=//div[@class='wrap_content']//div[3]//span[@class='description']//p
    ${image_link} =     Get element attribute    xpath=//div[@class='content']//div[@class='wrap_img']//img@src
    Execute SQL String      INSERT INTO test VALUES('${title}', '${cate}', '${director}', '${cast}','${content}', '${image_link}', 'link');
    Go Back

${content}变量中,存储了这个内容:

一名年轻女子在一名男子的地下室中醒来,这名男子声称在将她从她倾覆的汽车中拉下来后救了她的命,该汽车在高速公路上猛烈地撞毁。该男子表示,他们上方的世界不再安全,现在是威胁其他人类的危险地带。由于不知道上面到底是什么,他们之间的信任也越来越少,这位女士质疑什么是真的,什么不是。这个男人是妄想精神病患者,对她有更险恶的议程吗?还是像他发誓的那样,真的是地上的地狱?

【问题讨论】:

  • 你用的是python 2还是python 3?
  • Python 2,与 python 3 机器人框架不工作
  • 这是网站link,这是代码${content} = get text xpath=//div[@class='wrap_content']//div[3]//span[@class='description']//p,当我运行机器人框架时,会显示此错误UnicodeEncodeError: 'latin-1' codec can't encode character u'\u2019' in position 590: ordinal not in range(256)

标签: python robotframework


【解决方案1】:

您使用的是 python 2,因此,您的问题是您没有在文件开头定义正确的编码。

# coding: utf-8

您可以选择:

  • 将编码头放在文件的顶部
  • 或:

    Use an ascii character for "’", like "'"
    

【讨论】:

  • 感谢您的回复,基本上我制作了一个系统,在该系统中我使用机器人框架从网站获取文本关键字获取内容,在这种情况下无法更改“'”
  • @Sahill 这不是我在你的代码中看到的,你能更新它以提供一个类似于现实世界的例子吗?
【解决方案2】:

在执行 SQL 字符串之前,您可以对内容进行编码,如:

${content} =    Encode String To Bytes    ${content}    ASCII    errors=replace

在这种情况下,结果是:... the woman questions what?s true and what?s not. Is the ...

如果您忽略错误:

${content} =    Encode String To Bytes    ${content}    ASCII    errors=ignore

结果将是:... the woman questions whats true and whats not. Is the ...

(这样会更容易理解)

【讨论】:

    猜你喜欢
    • 2017-03-29
    • 2017-09-08
    • 2014-12-25
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 2016-08-26
    • 2011-07-05
    • 2018-07-10
    相关资源
    最近更新 更多