【发布时间】:2015-02-03 19:14:45
【问题描述】:
我在各种 xpath 表达式中都遇到过这个问题。 selenium 的 find_element_by_xpath() 方法失败,而同一表达式的 lxml 给出了预期值。例如:
>>> lxml.html.fromstring(br.page_source).xpath('//a[@title="2"]/text()')
... ['\n\t\t\t\t\t\t2\n\t\t\t\t\t', '\n\t\t\t\t\t\t2\n\t\t\t\t\t']
>>> br.find_element_by_xpath('//a[@title="2"]/text()')
... InvalidSelectorException: Message: {"errorMessage":"The result of the xpath expression \"//a[@title=\"2\"]/text()\" is: [object Text]. It should be an element.","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"108","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:47455","User-Agent":"Python-urllib/2.7"},"httpVersion":"1.1","method":"POST","post":"{\"using\": \"xpath\", \"sessionId\": \"85546b60-7c8e-11e4-b2ba-2bb5fbee7719\", \"value\": \"//a[@title=\\\"2\\\"]/text()\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/85546b60-7c8e-11e4-b2ba-2bb5fbee7719/element"}}
Screenshot: available via screen
为什么会发生这种情况,我该如何纠正?
【问题讨论】:
标签: python selenium xpath selenium-webdriver lxml