无法按驱动程序捕获。selenium使用的find_element_by_xpath

2024-05-06 11:24:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图捕捉一个按钮点击它。源url是:

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']

Tags: textdivuibytypedriverseleniumbutton