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

时间:2013-11-24 13:59:31

标签: python file-io character-encoding selenium-webdriver

我使用以下代码从.txt文件中读取一些非英语(Chienese)文本。

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 /

错误讯息:

InvalidSelectorException: Message: u'The given selector //a[contains(.,\ufeff\'\u8054\u7edc\u6211\u4eec\'\n)] is either invalid or does not result in a WebElement

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

1 个答案:

答案 0 :(得分:0)

如果没有看到实际的chinese.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 + "')]")

此外,我在链接的末尾看到了\ n,并且在开头就是\ ufeff。用line.strip()

抛弃它们