Using XPATH Contains Function

if I had a section of HTML I was trying to scrape like this:

<SPAN id=ctl00_MainContent_gvSearch_Result_ctl09_lblDescription class=smallbold>VGA video cable</SPAN>

The automatic XPATH is this:

//TD[@class='textSmall']/SPAN[2]

While that does work most of the time, sometimes it gathers additional fields depending on search results, or misses the desired selection entirely. I've been reading about the XPATH Contains function - is there a way for me to restrict the XPath here to just be results where the span ID contains a specific value (like "lblDescription")? I've pored through the manual but cannot seem to figure out how to do this - I tried something like /SPAN[.,contains('Description')] but that doesn't seem to work.



Answer:

You can use XPath matches function. The first parameter is the attribute you want to check. The second parameter is the text you want to test.
In your case, the xpath should be..

//SPAN[matches(@id, 'Description')]

SHARE

About Agung Pambudi

I am a Blogger with passion in business.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment