简体   繁体   English

在 selenium 中运行 Xpath 的 text()

[英]Run text() of Xpath in selenium

I am going through selenium practice where xpath validates in firefox xpath extesion but not in python selenium.我正在经历 selenium 实践,其中 xpath 在firefox xpath extesion 中验证但不在 python selenium 中验证。

eg 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]")例如,转到此处并应用 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]")这样的硒表达式driver.find_elements_by_xpath("//span[@class='tableNode']/text()[preceding-sibling::br and following-sibling::br]")

Exception i am facing我面临的例外

    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.

Any help is welcome.欢迎任何帮助。

Selenium evaluate only web elements. Selenium 仅评估 Web 元素。 text() function returns an object. text() 函数返回一个对象。 the solution is to execute xpath in javascript function and return its value into variable.解决方案是在javascript函数中执行xpath并将其值返回到变量中。 for ex, for python:例如,对于python:

returnText = []
returnText = driver.execute_script("return  document.evaluate(\"//span[@class='tableNode']/text()[preceding-sibling::br and following-sibling::br]\", document, null, XPathResult.STRING_TYPE, null).stringValue;")

for item in returnText:
    print item

For multiple returning record:对于多次返回记录:

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

Pure Javascript code is:纯 Javascript 代码是:

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 );
}

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

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