2013-11-24 29 views
1

我正在使用以下代码从.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 

有什么办法来解决这个问题?

+0

你还没有说过你用来保存文件的工具。如果您正在使用程序,请显示您正在使用的方法。 – Vorsprung

+0

我在这里使用.txt文件。在我的问题中也添加了详细信息。 谢谢。 – Shan

+0

所以,'chinese.txt'实际上包含原始的html权限?看起来像是每行一页......(你可能已经做了一些预处理来删除每一行中的新行字符,对吗?) –

回答

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()

+0

仍然是同一个问题!无法找到元素! NoSuchElementException:消息:u'Unable to locate element:{“method”:“xpath”,“selector”:“// a [contains(。,\'\ ufeff \ u8054 \ u7edc \ ull11 \ u4eec \') ]“}'; – Shan

+0

@ user159087请在问题中发布您的chinese.txt样本。我对这个\ ufeff感到困惑。这应该是一个utf-8类型的空间,应该用strip()消除。 – symbiotech

相关问题