TheReshi
TheReshi

Reputation: 81

Python 3.6 Selenium How to find a child node through the reference of a parent node (xPath)

enter code hereI have two fields by xPath.

I have located and fixed the first div by:

main_field = driver.find_element_by_xpath('//[@id="app"]/div/div/div[2]/div/div[2]/div[2]/div[3]/div[1]')

Is there any way of getting the /input field using the main_field? I mean something like main_field['input']. I don't want to use the find_element_by_xpath function again if possible.

Thanks in advance!

Upvotes: 2

Views: 47

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193208

As you have located the first field by :

main_field = driver.find_element_by_xpath('//[@id="app"]/div/div/div[2]/div/div[2]/div[2]/div[3]/div[1]')

Now, there is a possibility that the main_field may contain one/multiple child nodes. So to get the specific child <input> field you can use the reference of the parent node i.e. the main_field but you have to invoke find_element_by_* again as follows :

main_field.find_element_by_xpath('.//input')

Upvotes: 1

Related Questions