1

Html code looks like this.

<div>
  <span>Title</span>
  <input value="a">
</div>
<div>
  <span>Price</span>
  <input value="">
</div>

I need to check if Span is price then insert value for Price etc...

Basically I can access to all inputs like

for el in driver.find_elements(by=By.XPATH, value="//input"):
    try:
        if el.parent.span... == 'Price':  #HERE I NEED TO ADD MISSING PART
           el.send_keys('10')
    except:
        pass

But then I can't determine upper node value.

Also tried with :

driver.find_element(by=By.XPATH, value="//input/preceding-sibling::span[contains(.,'Price')]")

Like this I'm able to locate Span element but I need to insert price in INPUT.

Since this is only snippet from a way bigger code where class names positions etc. are dynamically generated. I need to go this way.

2
  • Your question is not so clear for me. Do you want to insert the value inside the input where span contains text Price or this block also must be below the block with Title? Commented Dec 5, 2022 at 15:17
  • I need to insert value in the input field where span contains text Price. So, span Price and input are both inside the div. Commented Dec 6, 2022 at 11:51

1 Answer 1

1

You can locate the input element based on it span sibling text content value as following:

"//div[contains(.,'Price')]//input"

(There are more ways to do that).
So your Selenium code can be as following:

driver.find_element(By.XPATH, "//div[contains(.,'Price')]//input").send_keys('10')

In case <span>Price</span> is a direct child of the parent div we can make the locator more precise, as following:

driver.find_element(By.XPATH, "//div[./span[contains(text(),'Price')]]//input").send_keys('10')

Briefly explanations:
With XPath we can locate elements by any it attributes.
Here we can locate the parent div based on the fact it contains a direct span child element containing the Price text.
So, span[contains(text(),'Price')] will match a span with Price text content while //div[./span[contains(text(),'Price')]] literally says:
"Find somewhere // an element with div tag name so that inside it (this is why a dot . is used there) a direct child / is span whos text content is Price".
Once we have this div we can find it input child by //input.
So simple.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for you answer. WIth this xPath driver.find_elements(By.XPATH, "//input") I got 8 members same as with this driver.find_elements(By.XPATH, "//div[contains(.,'Price')]//input") It filters me all //inputs but I need to get only the one with span above titled Price.
I see.. 2 clarification questions: does div containing these span and input nodes have any unique class or something unique? And are span and input are direct children of that parent div?
In case <span>Price</span> is a direct child of the parent div the updated answer should work. Please add the clarifications I asked about.
I'm not sure about good, clear and simple documentations, so I added some explanations. I hope it is clear.
In case something is still not clear - let me know. I will be happy to clarify.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.