pythonselenium-webdriversendkeys

Problem with send_keys method in selenium python


In a web automation code, I am trying to find a web element and write something in it. My code can find the web element but it fails at writing the text in the element by the send_keys method and the code raises a type error: 'WebElement' object is not iterable.

As far as I have searched about this error, it should occur when trying to iterate over a single WebElement object as if it were a list or a collection of objects, So I don’t understand why it occurs in my case.

Can you guide me on this issue?

Additional information:

My minimized code is:

receiptDate=r"1403/07/03" receiptDate = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, "input[name='nreceipt.receiptDate']")))

receiptDate.clear() 
receiptDate.send_keys(receiptDate)

the full error code is:

File "c:\kalantari\miscellaneous\PythOn\web scraping\sabad\varizi.py", line 104, in <module> receiptDate.send_keys(receiptDate) File "C:\kalantari\miscellaneous\PythOn\web scraping\sabad\sabad_env\Lib\site-packages\selenium\webdriver\remote\webelement.py", line 232, in send_keys Command.SEND_KEYS_TO_ELEMENT, {"text": "".join(keys_to_typing(value)), "value": keys_to_typing(value)} ^^^^^^^^^^^^^^^^^^^^^ File "C:\kalantari\miscellaneous\PythOn\web scraping\sabad\sabad_env\Lib\site-packages\selenium\webdriver\common\utils.py", line 137, in keys_to_typing characters.extend(val) TypeError: 'WebElement' object is not iterable

The code of the web element is:

<input type="text" name="nreceipt.receiptDate" maxlength="10" size="10" value="1403/07/18" onkeypress="return autoMask(this,event,'####/##/##')">

Solution

  • The issue is that you're trying to use the receiptDate variable for both the WebElement and the string value you want to send to that element. This is may causing a conflict because send_keys expects a string, but it's receiving a WebElement instead, leading to the WebElement object is not iterable error.

    receipt_date_value = r"1403/07/03"
    receipt_date_element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, "input[name='nreceipt.receiptDate']")))
    
    receipt_date_element.clear()
    receipt_date_element.send_keys(receipt_date_value)