我有这个html片段:
<div class="btn">
<a href="javascript:void(0)" class="S_btn_b" node-type="OK">
<span>确定</span>
</a>
</div>
我想选择确定
元素并单击它。我使用xpath尝试了以下选择器:
confirm_btn = driver.find_element_by_xpath('//div[@class="btn"]//span[text()="确定"]')
但程序只是抱怨说这不是一个有效的选择器:
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
我做错了什么吗?
答案 0 :(得分:3)
如果你没有将你的XPath表达式标记为Unicode,那么当搜索命令发送到浏览器并且搜索将搜索除你想要的内容之外的其他内容时,汉字将会被破坏,因此将你的XPath表达式标记为Unicode:
confirm_btn = driver.find_element_by_xpath(u'//div[@class="btn"]//span[text()="确定"]')