1

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,'####/##/##')">
1
  • You can't have two variables with the same name. Change one of the receiptDate variables. Commented Oct 10, 2024 at 7:11

1 Answer 1

0

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your feedback. Yes you are right: I have made a mistake here and should rename the variables to avoid “naming conflict”. I rename the variables and it solves the problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.