Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading