简体   繁体   中英

Run text() of Xpath in selenium

I am going through selenium practice where xpath validates in firefox xpath extesion but not in 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]")

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. text() function returns an object. the solution is to execute xpath in javascript function and return its value into variable. for ex, for 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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