【发布时间】:2014-11-05 15:30:44
【问题描述】:
我正在尝试捕获一个按钮以单击它。来源网址是:
The goal is to provide free information to website users and owners regarding website security status.
<br>
<br>
Use it wisely or we'll have to take it away.
</p>
</div><div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix"><div class="ui-dialog-buttonset"><button type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only ui-state-focus" role="button" aria-disabled="false"><span class="ui-button-text">Accept</span></button></div></div></div></body></html>
我尝试了什么:
from selenium import webdriver
driver = webdriver.PhantomJS()
url='http://example.com'
driver.get(url)
driver.page_source
driver.find_element_by_xpath('//button[@type="button"]/span[@class="ui-button-text"]/text()').click()
driver.quit()
错误信息是:
File "u.py", line 8, in <module>
driver.find_element_by_xpath('//button[@type="button"]/span[@class="ui-button-text"]/text()').click()
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: Message: u'Error Message => \'The result of the xpath expression "//button[@type="button"]/span[@class="ui-button-text"]/text()" is: [object Text]. It should be an element.\'\n caused by Request => {"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"149","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:57776","User-Agent":"Python-urllib/2.7"},"httpVersion":"1.1","method":"POST","post":"{\\"using\\": \\"xpath\\", \\"sessionId\\": \\"7b17cc00-6500-11e4-9c4e-e1b4bf9ba927\\", \\"value\\": \\"//button[@type=\\\\\\"button\\\\\\"]/span[@class=\\\\\\"ui-button-text\\\\\\"]/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/7b17cc00-6500-11e4-9c4e-e1b4bf9ba927/element"}' ; Screenshot: available via screen
更新:
当我以下列方式使用它时:
from lxml import html
cont="""<br>
<br>
Use it wisely or we'll have to take it away.
</p>
</div><div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix"><div class="ui-dialog-buttonset"><button type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only ui-state-focus" role="button" aria-disabled="false"><span class="ui-button-text">Accept</span></button></div></div></div></body></html>
"""
tree=html.fromstring(cont)
print tree.xpath('//button[@type="button"]/span[@class="ui-button-text"]/text()')
返回:
['Accept']
【问题讨论】:
-
错误信息告诉你你需要知道的一切。从 XPath 表达式中删除了
/text()。 -
但没有使用 selenium,我测试了 xpath 结构并返回给我
Accept -
是的,文本不是元素。文本不可点击。为此,您需要周围的元素。
-
请看更新部分
-
是的,它当然会返回文本。但是您不能单击文本节点。您需要单击包含文本的跨度。文本不能有点击处理程序。我不知道我怎样才能让它更清楚。您是否尝试过我在第一条评论中的建议?
标签: python selenium xpath phantomjs