3

I have an element of type hidden in an iframe. I am wondering if there would be any way to get this value as I am using selenium. More specifically it is a captcha field. I've tried pulling it with something along the lines of

#!/usr/bin/env python

from selenium import webdriver
driver=webdriver.Chrome(chrome_bin_path)
driver.get('http://websitehere.com')
print driver.find_element_by_xpath('//*[@id="recaptcha-token"]').text

but because of it's hidden nature it returns nothing.

Below is a snippet of the source. Highlighted is the string of interest. (value)

source

Thomas Dukes
  • 139
  • 1
  • 2
  • 10
  • You may have a look at [this](http://stackoverflow.com/questions/18500711/read-a-hidden-value-in-a-div-using-selenium-python-binding) question. Maybe it could help you. – dazzieta Jul 17 '16 at 04:06

2 Answers2

5
driver.switch_to_frame('undefined')
token_value = driver.find_element_by_id('recaptcha-token').get_attribute('value')
driver.switch_to_default_content()

Moving between windows and frames.

Vladimir Danilov
  • 2,338
  • 14
  • 15
  • As a bonus, would you know if I could repeat the step after captcha solve? After solving, a new tag appears with matching id inside of the #document in a similar fashion. – Thomas Dukes Jul 17 '16 at 04:33
4

Use this method

hidden_text = element.get_attribute("textContent")
Nadav k
  • 41
  • 1