简体   繁体   中英

how to encode non-english web links to use in selenium

I am using the following code to read some non-english (Chienese) texts from a .txt file.

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 /

Error Message:

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

Is there any way to overcome this issue ?

Without seeing the actual chinese.txt, I think you are missing some ' in your contains function code. Maybe it should be like:

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 + "')]")

Also I see \\n at the end of your link and this \ at the beginning. Ditch them with line.strip()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM