Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate through DOM elements that match a css class using xpath?

I'm processing an HTML page with a variable number of p elements with a css class "myclass", using Python + Selenium RC.

When I try to select each node with this xpath:

//p[@class='myclass'][n]

(with n a natural number)

I get only the first p element with this css class for every n, unlike the situation if I iterate through selecting ALL p elements with:

//p[n]

Is there any way I can iterate through elements by css class using xpath?

like image 877
GJ. Avatar asked Jul 17 '10 16:07

GJ.


People also ask

Can I use XPath in CSS?

XML Path Language (XPath) is a syntax for locating elements in structured documents such as an XML document or web page. XPath defines a kind of hierarchy of elements in the document. You do not need to know XPath or CSS (though it might be helpful) in order to use them in actions.

Does XPath use DOM?

The XML Document Object Model (DOM) contains methods that allow you to use XML Path Language (XPath) navigation to query information in the DOM. You can use XPath to find a single, specific node or to find all nodes that match some criteria.

How do I select a class in XPath?

Xpath class is defined as a selector that is usually shared by multiple elements in the document means it extracts all the class names. Nodes or lists of nodes are selected using XPath expressions based on property class names. The class name is separated by a Spaces. This token has white space.

What is a DOM XPath?

DOM and XPath are different concepts. DOM (Document Object Model) is a representation of a document or document fragment consisting of XML nodes arranged as a tree. XPath is a syntax for expressing a navigation through a DOM to locate one or more nodes. The name DOMXPath in PHP is somewhat redundant.


Video Answer


1 Answers

XPath 1.0 doesn't provide an iterating construct.

Iteration can be performed on the selected node-set in the language that is hosting XPath.

Examples:

In XSLT 1.0:

   <xsl:for-each select="someExpressionSelectingNodes">
     <!-- Do something with the current node -->
   </xsl:for-each>

In C#:

using System;
using System.IO;
using System.Xml;

public class Sample {

  public static void Main() {

    XmlDocument doc = new XmlDocument();
    doc.Load("booksort.xml");

    XmlNodeList nodeList;
    XmlNode root = doc.DocumentElement;

    nodeList=root.SelectNodes("descendant::book[author/last-name='Austen']");

    //Change the price on the books.
    foreach (XmlNode book in nodeList)
    {
      book.LastChild.InnerText="15.95";
    }

    Console.WriteLine("Display the modified XML document....");
    doc.Save(Console.Out);

  }
}

XPath 2.0 has its own iteration construct:

   for $varname1 in someExpression1,
       $varname2 in someExpression2, 
      .  .  .  .  .  .  .  .  .  .  .
       $varnameN in someExpressionN 
    return
        SomeExpressionUsingTheVarsAbove
like image 51
Dimitre Novatchev Avatar answered Oct 20 '22 09:10

Dimitre Novatchev