0

I have this html snippet:

<div class="btn">
    <a href="javascript:void(0)" class="S_btn_b" node-type="OK">
        <span>确定</span>
    </a>
</div>

And I want to select the 确定 element and click it. I tried the following selector with xpath:

confirm_btn = driver.find_element_by_xpath('//div[@class="btn"]//span[text()="确定"]')

But the program just complain that that is not a valid selector with this Exception:

Traceback (most recent call last):
  File "test.py", line 63, in <module>
    ac.move_to_element(driver.find_element_by_xpath('//div[@class="btn"]/span[text()="确定"'))
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 230, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 662, in find_element
    {'using': by, 'value': value})['value']
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 173, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 166, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSelectorException

Did I do something wrong ?

5
  • What happens if you try with only '//div[@class="btn"]//span'? Commented Feb 10, 2015 at 11:02
  • @MathiasMüller it seems ok when not specify the text() of span may be the quote sign case the problem? Commented Feb 10, 2015 at 11:10
  • No, but it seems those characters (Chinese?) are not supported. Commented Feb 10, 2015 at 11:12
  • hmm, but I can use the find_element_by_link_text(u'确定') like that, and yes, they are Chinese characters, weird, though. Commented Feb 10, 2015 at 11:15
  • @Louis spark idea ! I have tried this find_element_by_xpath(u'//div[@class="btn"]//span[text()="确定"]') and it worked. You should answer this problem so that I can accept it :-) Commented Feb 10, 2015 at 12:27

1 Answer 1

3

If you do not mark your XPath expression as Unicode, the Hanzi will get mangled when the search command is sent to the browser and the search will search for something else than what you want so mark your XPath expression as Unicode:

confirm_btn = driver.find_element_by_xpath(u'//div[@class="btn"]//span[text()="确定"]')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.