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.

rj2700
  • 1,770
  • 6
  • 28
  • 55
  • What's the error ? – Hassan Mehmood Apr 14 '16 at 04: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 Apr 14 '16 at 05:06
  • Hmm, put some delay after send_keys statement so you can actually see whether your text is entering into the textarea or not ? – Hassan Mehmood Apr 14 '16 at 05:11
  • Still nothing for me. Even with a delay – rj2700 Apr 14 '16 at 05:17
  • Did clear statement clears the default text ? If yes then try removing that statement and use only send_keys() statement – Hassan Mehmood Apr 14 '16 at 05:22
  • It does clear the text but still does not enter it in..hmm – rj2700 Apr 14 '16 at 05:24
  • Let's try this. after clear statement perform a click on textarea e.g driver.find_element_by_id("text_to_score").clear() driver.find_element_by_id("text_to_score").click() driver.find_element_by_id("text_to_score").send_keys(text) – Hassan Mehmood Apr 14 '16 at 05:29
  • No but it seems like the issue wasn't with selenium but with the text, actually. So what we've been doing was correct. I lied a bit about the text input, that string was just a simple typed out example because I thought all strings were treated the same but the real string that I used has about 1000+ words and is imported through a external text file by using the open command with read(). That seems to be the problem but I'm trying to find how I can input that entire text into this area. – rj2700 Apr 14 '16 at 06:17
  • Let me play around with this but thank you so much for your help (and everyone else), Hassan. – rj2700 Apr 14 '16 at 06:19

3 Answers3

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)
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • 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 Apr 14 '16 at 05: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 Apr 14 '16 at 05: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 Apr 14 '16 at 06:19
  • Can you explain how we can press enter to send our text? –  Mar 20 '18 at 13:23
  • @hamed_baziyad, See [this question]( See this https://stackoverflow.com/questions/38312033/enter-key-press-using-selenium-webdriver-with-python). 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 Mar 20 '18 at 13:44
  • @falsetru It didn't help me –  Mar 20 '18 at 14:53
  • @user7621299 .send_keys(Keys.ENTER) will need to import from selenium.webdriver.common.keys import Keys – CodingMatters Jan 14 '21 at 02:53
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.

Titim
  • 1
  • 1