Skip to content
Advertisement

Getting first sibling of second instance using Xpath

I’m trying to extract information from this page:

I’m trying to extract the time (6:30 PM).

My strategy is to find the second instance of the date (Mar. 31st, 2022), and then get the first sibling of that. Photo here (I want the part boxed in yellow):

enter image description here

Here’s what I’ve tried:

JavaScript

However, this is not getting me what I want. What am I doing wrong here? I’m looking for a way to get the first sibling of the second instance using Xpath.

Advertisement

Answer

It seems it is first element with PM / AM so I would use find_element with

JavaScript

like this

JavaScript

I use space before PM/AM to make sure it is not inside word.


Your xpath works when I add ( ) so it first gets divs and later select by index.
Without () it may treats [text()="..."][1] like [text()="..." and 1].
And it needs [2] instead of [1] because xpath start counting at 1, not 0

JavaScript

Full working example

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement