clear() method not working

1,464 views
Skip to first unread message

Lalit Kanoriya

unread,
Sep 14, 2023, 10:58:20 AM9/14/23
to Selenium Users
Hi,

This is my setup: -
  • Python 3.11.5
  • Selenium 4.12.0
  • Microsoft Edge (version 116.0.1938.81)
  • Microsoft Windows 11 (version 22H2 22621.2283)
Link to the public-facing site which I used to reproduce the problem: - OrangeHRM (orangehrmlive.com)

I am trying to clear the value of an input HTML element using the selenium clear() method, but it is not working as expected, so the send_keys() method appends the new value to the current value of the input box.

Here is my code: - Selenium Python clear() method not working - Stack Overflow

What I've tried:
  1. element.send_keys(Keys.CONTROL, 'a') with element.send_keys(Keys.BACKSPACE) or  element.send_keys(Keys.DELETE)
  2. driver.execute_script('arguments[0].value = "";', driver.find_element_by_id("text-input-where"))
But didn't work for me. I also tried with the Chrome browser but got the same issue. Only Keys.BACKSPACE is working, but to clear all text I have to use the loop which I don't want to.

Any ideas?
Thanks, and regards,
Lalit

joseph...@gmail.com

unread,
Sep 14, 2023, 4:49:11 PM9/14/23
to Selenium Users
I have had issues like this before that I resolved by using Javascript to select all the text in the input, then use send_keys which will overwrite the selection. Try something like this (pseudo code based on your code).

element = driver.find_element_by_id("text-input-where")
driver.execute_script('arguments[0].select()', element)
element.send_keys(text-to-type)

Adrian

unread,
Sep 14, 2023, 5:44:29 PM9/14/23
to Selenium Users
Hi,
I see you have a number of clear statements.  Which one is not working for you?
All of them or just the last one?

Cheers,
Adrian.

prashant choudhary

unread,
Sep 15, 2023, 12:15:21 AM9/15/23
to seleniu...@googlegroups.com
Hi Lalit,


Firstly you need to apply clear method in selenium and call it by text..

****


In case .clear() does not work you can try:

from selenium.webdriver.common.keys import Keys

#...your code (I was using python 3)

driver.find_element_by_id('foo').send_keys(Keys.CONTROL + "a");
driver.find_element_by_id('foo').send_keys(Keys.DELETE) ****


Thanks..!!

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/ff9f2715-7ff5-450b-b4ad-b59bcd36b9b4n%40googlegroups.com.

prashant choudhary

unread,
Sep 15, 2023, 12:15:23 AM9/15/23
to seleniu...@googlegroups.com
Hi Lalit,

Please try this hopefully it  works for you.

*** selenium.webdriver.common.keys import Keys ****

Thanks..!!!

On Thu, 14 Sept, 2023, 20:28 Lalit Kanoriya, <lalit.k...@1digitalstack.ai> wrote:
--
Reply all
Reply to author
Forward
0 new messages