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>
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.
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.
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.
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')
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:
featureElement = browser.find_element_by_xpath("//textarea//..")
featureElement.screenshot('foo.png')
#to read from image
images = cv2.imread('image_path')
gray = cv2.cvtColor(images, cv2.COLOR_BGR2GRAY)
cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
filename = "{}.jpg".format(os.getpid())
cv2.imwrite(filename, gray)
text = pytesseract.image_to_string(Image.open(filename))
print(text)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With