如何编码非英语web链接以在selenium中使用

2024-04-25 21:41:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用下面的代码从一个.txt文件中读取一些非英语(中文)文本。在

f = open('C:\data\chinese.txt')
    for line in f:
        print line  # this displays the chinese characters properly in console
        currelem = d.find_element_by_xpath("//a[contains(.," + line + ")]")  # this gives error as mentioned below /

错误消息:

^{pr2}$

有没有办法克服这个问题?在


Tags: 文件the代码in文本txtfordata
1条回答
网友
1楼 · 发布于 2024-04-25 21:41:09

没有看到真实的中文.txt,我想您的contains函数代码中缺少一些'。也许应该是这样的:

f = open('C:\data\chinese.txt')
for line in f:
    print line  # this displays the chinese characters properly in console
    currelem = d.find_element_by_xpath("//a[contains(.,'" + line + "')]")

另外,我在链接的结尾看到\n,在开头看到这个\u效果。用line.strip()丢弃它们

相关问题 更多 >

    热门问题