无法通过selenium使用的driver.find_element_by_xpath捕获

时间:2014-11-05 15:30:45

标签: python selenium xpath phantomjs

我想抓一个按钮点击它。源网址是:

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

1 个答案:

答案 0 :(得分:2)

使用此:

driver.find_element_by_xpath(
    '//button[@type="button"]/span[@class="ui-button-text"]').click()

在XPath表达式末尾没有/text()。 Artjom B.在评论中向您解释的是,如果您使用find_element_by_xpath,则传递给此方法的XPath表达式必须解析为HTML元素。就 XPath 而言,你给它的XPath是有效的,是的。但它是一个有效的XPath表达式是不够的。您在问题中使用的表达式的值是 string ,而不是元素,但Selenium绝对需要一个元素。