Home > Mobile >  How do you find a specific descendent from an element in selenium?
How do you find a specific descendent from an element in selenium?

Time:01-28

I'm automating the process of filling some forms online. The problem is that there are many individual elements whose children have basically the same ID of the stuff I want to find and fill. So my idea was to first find the parent I needed using Selenium and then go from there.

for range in cards:
  cardID = driver.find_element(By.XPATH, "//a[contains(text(),'{cardis}/')]/ancestor::tr".format(cardis=allCards_NUMBER_List[range]))
  cardREG_PRICE = cardID.find_element(By.XPATH, "input[contains(id(), 'txt_preco_')]")

But when I run this it only said that it can't find cardREG_PRICE. The ID name is correct, and from what I've read the XPATH structure should work. How can I fix this?

CodePudding user response:

Your xpath is incorrect.That's why it is failing. Instead of this

cardREG_PRICE = cardID.find_element(By.XPATH, "input[contains(id(), 'txt_preco_')]")

if should be like.

cardREG_PRICE = cardID.find_element(By.XPATH, ".//input[contains(@id, 'txt_preco_')]")

first thing id is an attribute and should pass with @, second thing // denote the node, Third thing . means intermediate child of the parent.

  • Related