简体   繁体   中英

InvalidSelectorException when select element for Python Selenium with xpath

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 ?

如果未将XPath表达式标记为Unicode,则在将搜索命令发送到浏览器时,汉字会被弄乱,并且搜索将搜索您想要的内容以外的其他内容,因此将XPath表达式标记为Unicode:

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

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