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

Selenium – Get element after a specific element (Python)

enter image description here

As you can see from the picture, the pagination is a bit different. So my idea is to get the position of the current-page and get the link for the a tag.

        current_page= driver.find_element(By.CSS_SELECTOR,"span.current-page")
        driver.find_element(By.TAG_NAME,"a").click()

but I didn’t know to use the current_page element to find the a tag after it and click on it.
Thanks for your help in advance.

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 :

To get the next element using current page reference you can use the following css selector in one liner or XPATH option.

next_page= driver.find_element(By.CSS_SELECTOR,"span.current-page+a")
print(next_page.get_attribute("href"))
next_page.click()

Or you can use this.

current_page= driver.find_element(By.CSS_SELECTOR,"span.current-page")
current_page.find_element(By.XPATH,"./following::a[1]").click()
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