4

Issue: How can you read a value in a nested <div>?

Situation: I am testing the registration on our web site. In order to complete the process, the user will need to enter the captcha. Using a helper class (on our dev server) we can display (hidden) the current captcha. I need to grab that and save it to be used by a .send_keys function (that part is the easy part). I have no code to share with what I am using, as I am not even sure where to jump in.

Well, I have this, but this is obviously not the answer:

driver.find_element_by_id("capchta").send_keys("foobar")

With above, I can send a key value (seen as "foobar"). I just need to replace "foobar" with a string that I read in the value=XXX

Here is the HTML code with the captcha, represented with the value=XXXX.

<div id="code_area">
     <p id="captcha_blurb">Please enter the security code below</p>
     <div id="capchta_area">
     <img id="secuity_icon" src="/img3/Secuity-Icon.png">
     <img id="security-image" src="data:image/png;==">
     <input id="current_captcha" type="hidden" value="XXXX">
     <input id="capchta" class="reg_form_input" value="" name="code" placeholder="Enter Security Code" title=" Please Enter These Characters." onblur="removeInputInfoBox('reg_box');" onfocus="addInputInfoBox('#capchta','#capchta_area', 'reg_box');">
</div>
2
  • Are you just trying to change the value of a hidden input? Commented Aug 29, 2013 at 1:03
  • No. Just read it. So I can use it. (See below) Commented Aug 29, 2013 at 1:25

2 Answers 2

7

I don't think you need to execute JS to get the hidden input's value.

You can use get_attribute to the value attribute

get_attribute(name)

So in your case, try:

captcha_value = driver.find_element_by_id("current_captcha").get_attribute("value")
driver.find_element_by_id("capchta").send_keys(captcha_value)
Sign up to request clarification or add additional context in comments.

Comments

1

As explained here Selenium can't interact with hidden elements. You're going to need to execute javascript to change the value for you; which is still a perfectly valid and accepted way of using Selenium.

1 Comment

It states, "Selenium WebDriver cannot interact with hidden elements, it can only find them" I am not trying to interact with it. Just find it. If I can find the value=XXXX, then I can capture it to a string and .send_keys(XXXX), exactly what I want.

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.