在 selenium 中运行 Xpath 的 text() [英] Run text() of Xpath in selenium

查看:27
本文介绍了在 selenium 中运行 Xpath 的 text()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在经历 selenium 实践,其中 xpath 在 firefox xpath 扩展 中验证,但不在 python 中验证硒.

I am going through selenium practice where xpath validates in firefox xpath extesion but not in python selenium.

例如转到这里 并应用 xpath //span[@class='tableNode']/text()[preceding-sibling::br and following-sibling::br] 它适用于 firefox 扩展但不适用于硒表达式,如 driver.find_elements_by_xpath("//span[@class='tableNode']/text()[preceding-sibling::br and following-sibling::br]")

e.g. go to here and apply xpath //span[@class='tableNode']/text()[preceding-sibling::br and following-sibling::br] it works in firefox extension but not in selenium expression like driver.find_elements_by_xpath("//span[@class='tableNode']/text()[preceding-sibling::br and following-sibling::br]")

我面临的异常

    Traceback (most recent call last):
      Debug Probe, prompt 17, line 1
      File "C:\Python27\ArcGIS10.3\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 270, in find_elements_by_xpath
        return self.find_elements(by=By.XPATH, value=xpath)
      File "C:\Python27\ArcGIS10.3\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 739, in find_elements
        {'using': by, 'value': value})['value']
      File "C:\Python27\ArcGIS10.3\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 201, in execute
        self.error_handler.check_response(response)
      File "C:\Python27\ArcGIS10.3\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 181, in check_response
        raise exception_class(message, screen, stacktrace)
    selenium.common.exceptions.InvalidSelectorException: Message: The given selector //span[@class='tableNode']/text()[preceding-sibling::br and following-sibling::br] is either invalid or does not result in a WebElement. The following error occurred:
    InvalidSelectorError: The result of the xpath expression "//span[@class='tableNode']/text()[preceding-sibling::br and following-sibling::br]" is: [object Text]. It should be an element.

欢迎任何帮助.

推荐答案

对于多条返回记录:

driver.get("http://www.hotleathers.com/Front-Printed-T-Shirts-C1232.aspx?s=OrderBy%20ASC&&v=all")

returnText = []
returnText = self.driver.execute_script("var iterator = document.evaluate(\"//span[@class='tableNode']/text()[preceding-sibling::br and following-sibling::br]\", document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null ); var arrayXpath = new Array();  var thisNode = iterator.iterateNext(); while (thisNode) {arrayXpath.push(thisNode.textContent);  thisNode = iterator.iterateNext(); }    return arrayXpath;")

for item in returnText:
    print item

纯 Javascript 代码是:

Pure Javascript code is:

var iterator = document.evaluate('//span[@class='tableNode']/text()[preceding-sibling::br and following-sibling::br]", documentNode, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null );

try {
  var thisNode = iterator.iterateNext();

  while (thisNode) {
    alert( thisNode.textContent );
    thisNode = iterator.iterateNext();
  } 
}
catch (e) {
  dump( 'Error: Document tree modified during iteration ' + e );
}

这篇关于在 selenium 中运行 Xpath 的 text()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆