1

I'm using Selenium in Python 3.4.

I tried to put a Korean word as an argument in the find_element_by_link_text() method, but it doesn't work.

What I mean is that it did not create any errors but it cannot find any hyperlink whose text is Korean.

Here is some example code:

from selenium import webdriver

browser = webdriver.PhantomJS()
browser.get('www.certain-korean-web-page.com')
a = browser.find_element_by_link_text('한국말')
print(a)

And it raises NoSuchElementException even though the page source has that element (of course, I checked an English one and it works).

Is it a unicode problem?

Thanks.

Mikev
  • 2,012
  • 1
  • 15
  • 27
user3595632
  • 5,380
  • 10
  • 55
  • 111

2 Answers2

1

couldn't find the element mentioned in your code on naver.com. but tried with another element and its working fine for me. code below.

# coding=utf-8
from selenium import webdriver

browser = webdriver.Firefox()
browser.get('http://www.naver.com/')
a = browser.find_element_by_link_text('네이버뉴스')
print(a)
mchackam
  • 81
  • 4
1

Firstly, you must add the line below at the top of project_file.py:

# -*- coding: utf-8 -*-

Then include this line:

a = browser.find_element_by_link_text(u'한국말')

This will work for sure.

ragingasiancoder
  • 616
  • 6
  • 17
Mojtaba Yousefi
  • 626
  • 1
  • 9
  • 27
  • It will only work for sure if the source file is saved UTF-8. The comment declares the source encoding only, so if it is not UTF-8, it won't work. – Mark Tolonen Jan 10 '16 at 23:19
  • I'm afraid I don't agree with you. python has got interpreter so whenever the file has been run, utf-8 encoding will be read by python interpreter.So, it must be present in source code when we wanna write utf-8 stuff in source code. On the other hand (u'한국말') has worked for me exactly in this situation. So please let me know where the problem of my script is.Thanks dear – Mojtaba Yousefi Jan 12 '16 at 06:03
  • The source encoding comment tells the Python interpreter what bytes were used to encode the file. Try your code but save it in an encoding other than UTF-8. Good luck! – Mark Tolonen Jan 12 '16 at 06:15
  • Thanks for the example. I thought the problem was something else – Mojtaba Yousefi Jan 12 '16 at 06:59