【问题标题】:Can't capture by driver.find_element_by_xpath used by selenium无法由 selenium 使用的 driver.find_element_by_xpath 捕获
【发布时间】: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


【解决方案1】:

使用这个:

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

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

【讨论】:

  • +1 谢谢...我想在点击接受后获取页面来源,那么我现在应该做什么?
  • 这与您最初在此处提出的问题完全不同。请参阅this question 了解如何做到这一点。如果那里的方法没有帮助,请发布一个新问题并确保解释为什么您的问题与我提供的链接不重复。否则,它将作为副本关闭。
猜你喜欢
  • 2016-04-29
  • 1970-01-01
  • 2022-01-21
  • 1970-01-01
  • 2022-08-19
  • 2017-05-02
  • 2019-03-18
  • 2014-11-19
  • 1970-01-01
相关资源
最近更新 更多