-2

I want to select the next sibling of a td tag in a tr element.

The tr element is this:

<tr>
  <td>Created On:</td>
  <td>06/28/2018 06:32      </td>
</tr>

My Scrapy code looks like this: response.xpath("//text()[contains(.,'Created On:')]/following-sibling::td"). But that gives me an empty list [].

How do I select the next td?

Username
  • 3,463
  • 11
  • 68
  • 111
  • You already got an answer in your [previous ticket](https://stackoverflow.com/questions/51217794/how-do-i-select-the-next-td-in-this-tr). Why did you modify the solution to make it return you undesirable result? – Andersson Jul 06 '18 at 22:12
  • 1
    Possible duplicate of [How do I select the next \`td\` in this \`tr\`?](https://stackoverflow.com/questions/51217794/how-do-i-select-the-next-td-in-this-tr) – Andersson Jul 06 '18 at 22:14

1 Answers1

3

Try this XPath expression:

//text()[contains(.,'Created On:')]/../following-sibling::td

You were trying to use the following-sibling axis from the wrong context node. Going back one level fixes this problem.

An alternative is matching the td element in the first place like in this expression:

//td[contains(text(),'Created On:')]/following-sibling::td
zx485
  • 28,498
  • 28
  • 50
  • 59
  • After reading the comment from @Andersson I'm wondering why you asked this question in the first place... – zx485 Jul 06 '18 at 22:15