0

I'm using selenium python and I have this code, it finds a comment box and then I want to type there a comment. the code finds the comment box but when it tries to type something the code breakes and i get this error:

AttributeError: 'NoneType' object has no attribute 'send_keys. 

Thats the code:

no_show = driver.find_element(By.XPATH,"xpath")
        no_show.click()
        notes = WebDriverWait(driver,60).until(EC.element_to_be_clickable(("id","the id")))
        time.sleep(2)
        notes = driver.find_element("id", "__area0-inner").click() #(The code fails here)
        notes.send_keys("test")
        print("test passed")

I tried another explicit wait:

element2 = WebDriverWait(driver,60).until(EC.presence_of_element_located(("id","the id")))

I tried to use notes.clear() Didnt work.

h4z3
  • 5,265
  • 1
  • 15
  • 29
eli2604
  • 1
  • 1

2 Answers2

1

As you noticed, your problem is here:

        notes = driver.find_element("id", "__area0-inner").click() #(The code fails here)
        notes.send_keys("test")

The problem is that instead of saving the found element, you immediately call .click() on it and save result of that - which is None because clicking doesn't return any data! Split into multiple lines, to actually save the element:

        notes = driver.find_element("id", "__area0-inner")
        notes.click()
        notes.send_keys("test")
h4z3
  • 5,265
  • 1
  • 15
  • 29
0

This error message...

AttributeError: 'NoneType' object has no attribute 'send_keys.

...implies that send_keys() can't be invoked on NoneType objects.


This usecase

click() doesn't return anything. So notes remains NULL/unassigned in the line:

notes = driver.find_element("id", "__area0-inner").click()

Hence when you try to invoke send_keys() on notes

notes.send_keys("test")

you see the error.


Solution

To invoke send_keys() on an element you don't need to invoke click() initially and can invoke send_keys() directly once the element is identified. Additionally instead of presence_of_element_located() you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "element_cssSelector"))).send_keys("test")
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "element_xpath"))).send_keys("test")
    
  • 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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 3
    The problem is they they were immediately chaining into `.click()` and capturing its return value, which returns `None`. – John Gordon Jun 14 '23 at 22:39
  • @JohnGordon yes champion, I misread the error. The error was on send_keys where as I took it on click :( – undetected Selenium Jun 14 '23 at 22:47
  • "So notes remains NULL/unassigned in the line:" - this is python, there's never "unassigned" after non-exception var= line. And we don't have `NULL` nor `null`, just `None` – h4z3 Jun 15 '23 at 08:25