Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用xpath为Python Selenium选择元素时出现InvalidSelectorException_Python_Selenium_Xpath - Fatal编程技术网

使用xpath为Python Selenium选择元素时出现InvalidSelectorException

使用xpath为Python Selenium选择元素时出现InvalidSelectorException,python,selenium,xpath,Python,Selenium,Xpath,我有以下html代码片段: <div class="btn"> <a href="javascript:void(0)" class="S_btn_b" node-type="OK"> <span>确定</span> </a> </div> 但程序只是抱怨这不是一个有效的选择器,但有一个例外: Traceback (most recent call last): File "test.p

我有以下html代码片段:

<div class="btn">
    <a href="javascript:void(0)" class="S_btn_b" node-type="OK">
        <span>确定</span>
    </a>
</div>
但程序只是抱怨这不是一个有效的选择器,但有一个例外:

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
回溯(最近一次呼叫最后一次):
文件“test.py”,第63行,在
ac.move_to_元素(driver.find_element_by_xpath('//div[@class=“btn”]]/span[text()=”确定"'))
文件“/usr/local/lib/python2.7/dist packages/selenium/webdriver/remote/webdriver.py”,第230行,按xpath查找元素
返回self.find_元素(by=by.XPATH,value=XPATH)
文件“/usr/local/lib/python2.7/dist packages/selenium/webdriver/remote/webdriver.py”,第662行,在find_元素中
{'using':by'value':value})['value']
文件“/usr/local/lib/python2.7/dist packages/selenium/webdriver/remote/webdriver.py”,执行中的第173行
self.error\u handler.check\u响应(响应)
文件“/usr/local/lib/python2.7/dist packages/selenium/webdriver/remote/errorhandler.py”,第166行,在check_响应中
引发异常类(消息、屏幕、堆栈跟踪)
selenium.common.Exception.InvalidSelectorException

我做错了什么吗?

如果您没有将XPath表达式标记为Unicode,则当搜索命令发送到浏览器时,汉字将被损坏,搜索将搜索您想要的以外的内容,因此请将XPath表达式标记为Unicode:

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

如果未将XPath表达式标记为Unicode,则当搜索命令发送到浏览器时,汉字将被损坏,搜索将搜索除所需内容以外的内容,因此请将XPath表达式标记为Unicode:

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

如果未将XPath表达式标记为Unicode,则当搜索命令发送到浏览器时,汉字将被损坏,搜索将搜索除所需内容以外的内容,因此请将XPath表达式标记为Unicode:

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

如果未将XPath表达式标记为Unicode,则当搜索命令发送到浏览器时,汉字将被损坏,搜索将搜索除所需内容以外的内容,因此请将XPath表达式标记为Unicode:

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


如果您仅尝试使用
'//div[@class=“btn”]//span'
?@MathiasMüller如果不指定
span
文本()
,会发生什么情况?可能是引号符号的大小写问题?不,但这些字符(中文?)似乎不受支持。嗯,但我可以使用
通过链接查找\u文本(u'确定')
像那样,是的,它们是汉字,但很奇怪。@Louis spark idea!我试过用xpath(u'//div[@class=“btn”]//span[text()=确定“]'”成功了。你应该回答这个问题,这样我才能接受它:-)如果你只尝试
'//div[@class=“btn”]//span'
?@MathiasMüller如果不指定
span
文本()
可能是引号大小写,会发生什么问题?不,但这些字符(中文?)不支持。嗯,但是我可以使用
find\u元素\u by\u link\u text(u'确定')
像那样,是的,它们是汉字,但很奇怪。@Louis spark idea!我试过用xpath(u'//div[@class=“btn”]//span[text()=确定“]'”成功了。你应该回答这个问题,这样我才能接受它:-)如果你只尝试
'//div[@class=“btn”]//span'
?@MathiasMüller如果不指定
span
文本()
可能是引号大小写,会发生什么问题?不,但这些字符(中文?)不支持。嗯,但是我可以使用
find\u元素\u by\u link\u text(u'确定')
像那样,是的,它们是汉字,但很奇怪。@Louis spark idea!我试过用xpath(u'//div[@class=“btn”]//span[text()=确定“]'”成功了。你应该回答这个问题,这样我才能接受它:-)如果你只尝试
'//div[@class=“btn”]//span'
?@MathiasMüller如果不指定
span
文本()
可能是引号大小写,会发生什么问题?不,但这些字符(中文?)不支持。嗯,但是我可以使用
find\u元素\u by\u link\u text(u'确定')
像那样,是的,它们是汉字,但很奇怪。@Louis spark idea!我试过用xpath(u'//div[@class=“btn”]//span[text()=确定“]”)它成功了。你应该回答这个问题,这样我才能接受它:-)