Send keys not working selenium webdriver python
Asked Answered
M

2

5

I need to send text to description textarea. There is some predefined text which is cleared after click. I tried to use clear() or click() before sendkeys but nothing works correctly. It will send text there but it is still grey and after saving page there is error that tere is no text in description... Can I use something else instead of send keys? Thanks

Textarea looks like:

<textarea id="manage_description" class="eTextArea" name="e.description" cols="" rows="" onfocus="clearDescHint(this);" onblur="resetDescHint(this);" style="color: grey;"></textarea>

send_keys not working

self.driver.find_element_by_id('manage_description').send_keys("TEST")

enter image description here

Moderation answered 16/10, 2017 at 12:46 Comment(2)
Any chance you can add more HTML to see if there are any other elements that are actually being used for setting the text and not the preview text?Welsh
The send_keys function is the right one to use. But it's obvious you're competing with javascript functions here. Invoking the javascript might help. But when you focus away (onblur event), it might reset the text. So you can try: self.driver.execute_script('clearDescHint(document.getElementById("manage_description"))'). See if that clears the text, then if it does, send_keysFusty
K
8

As you mentioned send_keys("TEST") are not working, there are a couple of alternatives to send a character sequence to respective fields as mentioned below :

  1. Use Keys.NUMPAD3 [simulating send_keys("3")]:

    login.send_keys(Keys.NUMPAD3)
    
  2. Use JavascriptExecutor with getElementById :

    self.driver.execute_script("document.getElementById('login_email').value='12345'")
    
  3. Use JavascriptExecutor with getElementsById :

    self.driver.execute_script("document.getElementsById('login_password')[0].value='password'")
    

Now comming to your specific issue, as you mentioned I tried to use clear() or click() before sendkeys but nothing works correctly, so we will take help of javascript to click() on the text area to clear the predefined text and then use send_keys to populate the text field as follows:

self.driver.execute_script("document.getElementById('manage_description').click()")
self.driver.find_element_by_id('manage_description').send_keys("TEST")

Update :

As you mentioned sometimes it works sometimes not, so I would suggest the following:

  1. Induce ExplicitWait for textarea to be clickable.
  2. Use javascript to send the text within the textarea too.
  3. Your code will look like:

    my_string = "TEST";
    elem = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "manage_description")))
    self.driver.execute_script("document.getElementById('manage_description').click()")
    self.driver.execute_script("arguments[0].setAttribute('value', '" + my_string +"')", elem);
    
Kauppi answered 16/10, 2017 at 15:29 Comment(4)
hm, so update... it is working but only sometimes... without any changes, sometimes it works sometimes notModeration
I tried to input text also this way self.driver.execute_script("document.getElementById('manage_description').value='test123'") but it still input only "grey" text so save is not working than...Moderation
I tried updated code there are some small errors like executeScript should be execute_script but it is not working. There is no error but description remains empty. I tried also change last row with this but inserted text is grey... self.driver.execute_script("document.getElementById('manage_description').value='test123'",elem)Moderation
I think we can't avoid self.driver.execute_script("document.getElementById('manage_description').click()"). This line is needed to clear the pre-filled text.Kauppi
R
0

This code will work for sending key to every type of input box or text area

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
element = browser.find_element(By.XPATH,'*//span[text()="Price"]')

ActionChains(browser).move_to_element(element).click(element).send_keys('1200').perform()
Riancho answered 7/4, 2023 at 19:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.