Home > Back-end >  Selenium - Get element after a specific element (Python)
Selenium - Get element after a specific element (Python)

Time:03-25

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.

CodePudding user response:

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()
  • Related