1

I want to input password to the box with Selenium but it returns selenium.common.exceptions.WebDriverException: Message: element not interactable

My python script:

from selenium import webdriver
browser = webdriver.Chrome(r'c:\chromedriver.exe')
url = 'https://creis.fang.com/'
browser.get(url)
browser.find_element_by_id('cnotp').send_keys('123456')

If I run the script, the above error appears. However, if I type line by line in the console. Then there is no error.

What should I do?

Thanks.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Chan
  • 3,605
  • 9
  • 29
  • 60
  • What happens if you wait a few milliseconds between each pair of commands (something like [`implicitly_wait`](https://selenium-python.readthedocs.io/waits.html#implicit-waits))? – user2814332 Jul 05 '19 at 12:31
  • I added time.sleep(5) before and after `browser.get(url)`. But it has no use. – Chan Jul 05 '19 at 12:34

3 Answers3

0

Maybe you should try to do it "by step". First select the element, clear its value then do the send_key stuff...

element = browser.find_element_by_id('cnotp')
element.clear()
element.send_keys('123456')

Hope it helps !

Flo
  • 936
  • 1
  • 8
  • 19
  • Oh, I checked the website and the field you're trying to fill seem to be a password field. There maybe is a protection on that specific field that won't let you interact that easily as the other fields would. – Flo Jul 05 '19 at 12:40
  • What should I do then? – Chan Jul 05 '19 at 12:46
  • I don't know but if the website blocks you using a JS script you have nothing to do. It could be some protection agaisnt bots. But I didn't check if it's true, it's just a supposition. – Flo Jul 05 '19 at 12:47
0

Wailt always whenever there is a url change.

from selenium import webdriver

driver = webdriver.Chrome()  # Change
driver.get('https://creis.fang.com/')

element = WebDriverWait(driver, 60).until(
    EC.presence_of_element_located((By.ID, "cnotp"))
)

element.clear()
element.send_keys("123456")

If it does not work, use js_executor

element = WebDriverWait(driver, 60).until(
    EC.presence_of_element_located((By.ID, "cnotp"))
)

driver.execute_script("document.getElementById('cnotp').click()")
driver.execute_script("arguments[0].setAttribute('value', '123456')", element);
mahan
  • 12,366
  • 5
  • 48
  • 83
0

To send a character sequence within the password field using Selenium you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following solutions:

  • Using CSS_SELECTOR:

    driver.get("https://creis.fang.com/")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.loginipt.fl#cnotp"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.loginipt.fl#cnpassword"))).send_keys("Chan")
    
  • Using XPATH:

    driver.get("https://creis.fang.com/")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='loginipt fl' and @id='cnotp']"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='loginipt fl' and @id='cnpassword']"))).send_keys("Chan")
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Browser Snapshot:

password

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    Thank you, DebanjanB. Why should we click first instead of directly input the text? – Chan Jul 05 '19 at 14:48
  • @Chan This was a one off case which I haven't seen before. In short, the developers have induced `display: none` on focus, so the you need to send text to the adjustment element. – undetected Selenium Jul 05 '19 at 14:53
  • DebanjanB, I tried to input text to the next field but failed. – Chan Jul 05 '19 at 15:08
  • `WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='cntempcode']"))).click() WebDriverWait(browser, 2).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='cntempcode']"))).send_keys("0000")` – Chan Jul 05 '19 at 15:08
  • @Chan Observe the HTML properly and track the changes. I believe they have implemented the same logic for each element. If you are still unable, raise a new ticket, I will look into it once I reach home. – undetected Selenium Jul 05 '19 at 15:10
  • I solved the problem by directly executing `WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='cntempcode']"))).send_keys("0000")` – Chan Jul 05 '19 at 15:53