How to find parent elements by python webdriver

You can find a parent element by using .. xpath:

input_el = driver.find_element_by_name('A')
td_p_input = input_el.find_element_by_xpath('..')

What about making a separate xpath for getting selected option, like this:

selected_option = driver.find_element_by_xpath('//option[@selected="selected"]')

data reference:
https://stackoverflow.com/questions/18079765/how-to-find-parent-elements-by-python-webdriver


offical doc:
https://selenium-python.readthedocs.io/locating-elements.html

4. Locating Element

There are various strategies to locate elements in a page. You can use the most appropriate one for your case. Selenium provides the following method to locate elements in a page:

  • find_element

To find multiple elements (these methods will return a list):

  • find_elements

Example usage:

from selenium.webdriver.common.by import By

driver.find_element(By.XPATH, '//button[text()="Some text"]')
driver.find_elements(By.XPATH, '//button')

others example:
https://pythonexamples.org/python-selenium-find-element-by-xpath/

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *