rj2700
rj2700

Reputation: 1965

Python selenium sending keys into textarea

I'm using Python 3.4.4 to access a website (https://readability-score.com/) that has a textarea, which dynamically updates when new values are added.

I'm trying to input a string into that textarea box but it isn't working for me.

Here's the code that I'm trying:

driver = webdriver.Firefox()
driver.wait = WebDriverWait(driver, 2)
URL = "https://readability-score.com/"

text = "Hello hello hello, this is a test"

driver.get(URL)
time.sleep(2)
driver.find_element_by_id("text_to_score").clear()
driver.find_element_by_id("text_to_score").send_keys(text)
#driver.find_element_by_xpath("/html/body/div[1]/div[6]/div/div[1]/form/fieldset/textarea").clear()
#driver.find_element_by_xpath("/html/body/div[1]/div[6]/div/div[1]/form/fieldset/textarea").send_keys(text)

The problem is that the selenium driver can not find the textarea to send the keys into. I think it's able to clear it (because I can literally see the text being cleared when you enter the page) but no text can be enter. Would anyone have an idea about this? I followed the online guide but I felt like I've tried all options that were listed (http://selenium-python.readthedocs.org/). Thanks.

Upvotes: 6

Views: 22477

Answers (3)

Titim
Titim

Reputation: 1

this is work for me

textarea = driver.find_element(By.ID, "text_to_score")
textarea.click()
textarea = driver.find_element(By.NAME, "text_to_score")
textarea.send_keys('your text')

you can use the different elements for input text to one field text area.

Upvotes: 0

Anonymous Helper
Anonymous Helper

Reputation: 31

the "send_keys" function only TYPES out the string you input, but does not "send" the keys, as counterintuitive as that may seem. The proper way to submit the textarea would be with a simple

driver.find_element_by_id("text_to_score").submit()

OR

textarea = driver.find_element_by_id("text_to_score")
textarea.submit()

Upvotes: 3

falsetru
falsetru

Reputation: 369494

You can explicitly wait until the textarea appear.

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


...


url = "https://readability-score.com/"
text = "Hello hello hello, this is a test"

driver.get(url)
WebDriverWait(driver, 5).until(
    EC.presence_of_element_located((By.ID, "text_to_score"))
)  # Wait until the `text_to_score` element appear (up to 5 seconds)
driver.find_element_by_id("text_to_score").clear()
driver.find_element_by_id('text_to_score').send_keys(text)

Upvotes: 7

Related Questions