0

What is Valid Xpath for link extract by div class name?

Here is html code:

<div class="poster">
<a href="/title/tt2091935/mediaviewer/rm4278707200?ref_=tt_ov_i"> <img alt="Mr. Right Poster" title="Mr. Right Poster" src="http://ia.media-imdb.com/images/M/MV5BOTcxNjUyOTMwOV5BMl5BanBnXkFtZTgwMzUxMDk4NzE@._V1_UX182_CR0,0,182,268_AL_.jpg" itemprop="image">
</a>    </div>

I want to know exact Xpath as if i found href link. I try with //a/@href[@class='poster'] but it's doesn't work

3
  • The class is on the div, not the anchor. Commented Jun 13, 2016 at 21:37
  • Yea. I want link for <div class="poster"> Commented Jun 13, 2016 at 21:40
  • Yes, which is why you'd use it as a criterion for div, not a or @href. Commented Jun 13, 2016 at 21:41

1 Answer 1

1

The <div> contains the <a> so you can use that to navigate:

//div[@class='poster']/a/@href

Remember that the "poster" class is defined on the <div> not on the <a> so that's where you need to apply the predicate.

  • //div returns all <div> elements
  • [@class='poster'] is a predicate that filters by class
  • /a returns all <a> elements that are children of those <div>s
  • /@href gives us the attribute we want

Depending on the system you're using you might need to wrap the whole expression in text() in order to bring back the attribute data rather than the DOM node.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.