Find next/closest button Selenium Find next/closest button Selenium selenium selenium

Find next/closest button Selenium


You can use in.get_attribute('id') to retrieve the id.


Alternatively, you can also select the following-sibling::button of a given element using an XPath:

in.find_element_by_xpath(".//following-sibling::button").click()

Explanation:

  • . - Selects the current node (<input>)
  • // - Selects nodes in the document
    • following-sibling::button - where siblings after the current node is a <button>

Reference: https://www.w3schools.com/xml/xpath_intro.asp