6

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.

9
  • What's the error ? Commented Apr 14, 2016 at 4:57
  • Oh yeah, good point. I'll edit this but the error is that the selenium driver can't send text into the textarea.
    – rj2700
    Commented Apr 14, 2016 at 5:06
  • Hmm, put some delay after send_keys statement so you can actually see whether your text is entering into the textarea or not ? Commented Apr 14, 2016 at 5:11
  • Still nothing for me. Even with a delay
    – rj2700
    Commented Apr 14, 2016 at 5:17
  • Did clear statement clears the default text ? If yes then try removing that statement and use only send_keys() statement Commented Apr 14, 2016 at 5:22

3 Answers 3

7

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)
7
  • The problem isn't that the driver can't find the element (because I can see it clearing the default text) but it just can't enter the text
    – rj2700
    Commented Apr 14, 2016 at 5:40
  • @user1342086, It's weird. It works perfect for me (both for Firefox, Chrome). BTW, which versino of selenium package do you use? I'm using 2.53.1.
    – falsetru
    Commented Apr 14, 2016 at 5:44
  • Yes, you may be correct. I lied about the sample text above and I'm actually importing a text file in using the open and .read() methods to bring in text. So this is a correct solution (I didn't try this but it looks fine).
    – rj2700
    Commented Apr 14, 2016 at 6:19
  • Can you explain how we can press enter to send our text?
    – user7621299
    Commented Mar 20, 2018 at 13:23
  • @hamed_baziyad, See [this question]( See this stackoverflow.com/questions/38312033/…). If that does not help you to solve our problem, please post a separate question (not comment) with url (or html) you're dealing with.
    – falsetru
    Commented Mar 20, 2018 at 13:44
3

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()
0

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.