Rich Stevens
Rich Stevens

Reputation: 609

XPATH ignore specific div class name

<div class="rowh">Engine and Drive Train
  </div>
  <a name="anc0"></a>
  <div class="optexrow oid0 row " data-oid="0">
    <div class="key">Engine Size
    </div>
    <div class="value">998 cc 
    </div>
    <div class="cf">
    </div>
  </div>

This query:

//div[contains(preceding-sibling::div[@class='rowh'][1],'Engine and Drive Train')]/div

Gives me:

    1.Engine Size
    2.998 cc
    3.
    4.Gears
    5.5
    6.
    7.Number of Valves
    8.12
    9.
    10.Transmission
    11.Manual

How do I ignore this empty div:

<div class="cf">

Or not look for it in the first place? I know it's something to do with "NOT" but I cannot seem to be able to nestle this in.

Upvotes: 1

Views: 63

Answers (1)

Ahmed Yousif
Ahmed Yousif

Reputation: 2348

you can use normalize-space() function strips leading and trailing white-space from a string, replaces sequences of whitespace characters by a single space, and returns the resulting string.

then check length of string using string-length() function in XPath. string-length() function returns the number of character in a string.

the final query will be as following:

//div[contains(preceding-sibling::div[@class='rowh'][1],'Engine and Drive Train')]/div[string-length(normalize-space(text())) > 0]

or shorten by using normalize-space() only it as following:

//div[contains(preceding-sibling::div[@class='rowh'][1],'Engine and Drive Train')]/div[normalize-space()]

Upvotes: 1

Related Questions