chetan
chetan

Reputation: 117

How to get text area content using selenium web driver

I have the following element(text area). The value of this text area won't appear in the html code. But that will be displayed in webpage when page is loaded. How to get the value using selenium and python.

<textarea id="query" class="textarea" cols="37" rows="30"></textarea>

Upvotes: 9

Views: 13361

Answers (2)

ItsPrinceAk
ItsPrinceAk

Reputation: 110

one thing which you can do is capture a screenshot of that area using and extract the text later using tesseract. Got same issue as text entered is not being stored in value attribute EG:

to capture screenshot

featureElement = browser.find_element_by_xpath("//textarea//..")
featureElement.screenshot('foo.png')

#to read from image

images = cv2.imread('image_path')

convert to grayscale image

gray = cv2.cvtColor(images, cv2.COLOR_BGR2GRAY)

cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]

memory usage with image i.e. adding image to memory

filename = "{}.jpg".format(os.getpid())
cv2.imwrite(filename, gray)
text = pytesseract.image_to_string(Image.open(filename))
print(text)

Upvotes: 2

Arran
Arran

Reputation: 25076

The contents of the textarea will be shown in it's value property, just like input elements. So something like (pseudo-Python)

 contents = driver.find_element_by_id('query').get_attribute('value')

Upvotes: 8

Related Questions