Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>

like image 992
chetan Avatar asked Apr 11 '14 06:04

chetan


People also ask

How do you getText from a text box in Selenium?

We can get the entered text from a textbox in Selenium webdriver. To obtain the value attribute of an element in the html document, we have to use the getAttribute() method. Then the value is passed as a parameter to the method. Let us consider a textbox where we entered some text and then want to get the entered text.

How do I find an element that contains specific text in Selenium Webdriver?

We can find an element that contains specific text with Selenium webdriver in Python using the xpath. This locator has functions that help to verify a specific text contained within an element. The function text() in xpath is used to locate a webelement depending on the text visible on the page.

Which method is used to get the text of an element in Selenium?

Selenium offers a getText() method used to get the text of an element, i.e.; it can be used to read text values of an element from a web page. In this article, we will understand how to get the text of an element using the getText() method offered by Selenium WebDriver.


2 Answers

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')
like image 175
Arran Avatar answered Oct 14 '22 04:10

Arran


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)
like image 1
ItsPrinceAk Avatar answered Oct 14 '22 04:10

ItsPrinceAk