繁体   English   中英

硒无法使用driver.find_element_by_xpath捕获

[英]Can't capture by driver.find_element_by_xpath used by selenium

我试图捕获一个按钮以单击它。 源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']

用这个:

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表达式这一事实还不够。 您在问题中具有的表达式的值是一个字符串 ,而不是一个元素,但是Selenium绝对需要一个元素。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM