Imran Omar Bukhsh
Imran Omar Bukhsh

Reputation: 8071

XPATH - Correct?

I am using the following xpath to get the sections outlined as in the image below ( http://advrider.com/forums/ ), but am not being to. Is there something wrong with it?

//TABLE/TBODY/TR[@class='dg-forums-level2 dg-align-center']/TD[2]/DIV[1]/A[1]

enter image description here

Updated

<?php
$BASE_PATH = "../src/";

include_once($BASE_PATH  . "classes/forumdb.php");
include_once($BASE_PATH  . "classes/curl.php");

$curl = new curl();
$html = $curl->get_web_page('http://advrider.com/forums/');

$doc = new DOMDocument();
$doc->loadHTML($html);

$xpath = new DOMXpath($doc);

//$elements = $xpath->query("//TABLE[@class='tborder']/TBODY/TR[@class='']/TD[2]/DIV[1]/A[1]/STRONG[1]");

$elements = $xpath->query("//*[@id='f3']"); //works
//$elements = $xpath->query("//TABLE/TBODY/TR");


//TD[@id='f74']/DIV[1]/A[1]

if (!is_null($elements))
{

  foreach ($elements as $element)
  {
    echo "f<br/>[". $element->nodeName. "]";
    $nodes = $element->childNodes;
    foreach ($nodes as $node)
    {
      echo $node->nodeValue. "\n";
    }
  }
}

?>

Upvotes: 1

Views: 252

Answers (4)

Mounika Medipelli
Mounika Medipelli

Reputation: 61

You can try the below xpath:

//ol[@class='nodeList']//*[@class='nodeTitle']//a

It will find the all the sections in the page.

Upvotes: 0

Sebachtian
Sebachtian

Reputation: 37

I would use this:

//*[@class="nodeTitle"]/a

demo

Upvotes: 1

pythonlearner
pythonlearner

Reputation: 91

To grap the section details text or url ,pls use these below xpath

//h3[@class="nodeTitle"]/a[@data-description]/text() #To grap text
//h3[@class="nodeTitle"]/a[@data-description]/@href #To grap url

Upvotes: -1

mdementev
mdementev

Reputation: 145

//li[div[@class='nodeInfo categoryNodeInfo categoryStrip' and div/h3/a[text()='Riding']]]//div[@class='nodeText']/h3[@class='nodeTitle']

Use it and try to avoid places like TD[2]/DIV[1]/A[1]. Selectors with classes are less brittle than selectors with numeric position in DOM tree.

I hope that it'll help

Upvotes: 1

Related Questions