How to get first 2 following siblings using Xpath (Webscraping: Python using Selenium)?

Question:

I am trying to get the text from the first two following siblings after a set of particular elements (with class separator) from a website. The website is something as follows:

    <hr class="separator" 
    </hr>
    <p> "sometext1" </p>
    <p> "someothertext1" </p>
    <p> "someirrelevanttext1" </p>
    <hr class="separator" 
    </hr>
    <p> "sometext2" </p>
    <p> "someothertext2" </p>
    <p> "someirrelevanttext2" </p>

My existing code can get the list of the 1st siblings and the 2nd siblings separately. But I want to have a setting where I can have [sometext1, someothertext1] and [sometext2, someothertext2]. So essentially extract the texts from the first 2 siblings after separator. Here is my try as of now:

for i in driver.find_elements(By.XPATH, "//*(@class, 'separator')]"):
    t1 = i.find_elements(By.XPATH, "//*(@class = 'separator')]//following-sibling::p[1]")
    t2 = i.find_elements(By.XPATH, "//*(@class = 'separator')]//following-sibling::p[2]")

Then I can print each text in t1 or t2. But I can’t get the texts from lists t1 and t2 in the manner above. Any help is very much appreciated as always. Thanks a lot!

Asked By: econbuffin2019

||

Answers:

Once you have the 2 lists t1 and t2 you can iterate over them simultaneously, as following:

for f, s in zip(t1, t2):
    print(f, s)

This will work for Python 3
For Python there is another syntax as can be seen here

Answered By: Prophet