Search code examples
pythonselenium-chromedriver

Can not input data into password field with selenium


I have created a python signup script with selenium to access a site and signuo to it, the user can add their specifications from the terminal. I tested the script on one of my django sites, the script succeeds in adding the first and last name but gets errors when trying to add the password.

'''INSTALL CHROME DRIVER ONLINE AND COPY THE PATH TO THE chrome_path'''


import time

import random

from chromedriver_py import binary_path

from selenium import webdriver

from selenium.webdriver.chrome.service import Service

from selenium.webdriver.common.by import By

number_of_signups = 0


'''TO RANDOMLY GENERATE USERNAMES'''
def gen_rand_un():
    un = "16JE00"
    un += str(random.randint(0, 9)) + str(random.randint(0, 9)) + str(random.randint(0, 9)) + str(random.randint(0, 9))
    return un

'''You would have to know the ELEMENT ID or element NAME of the inputs, this can be gotten by inspecting elements'''
def main(site, firstName, lastName, el, password):
    while 1:
            un = gen_rand_un()

            options = webdriver.ChromeOptions()

            driver = webdriver.Chrome(service=Service(executable_path=binary_path), options=options)

            url = site

            driver.get(url)
            
            if firstName == "" or lastName == "":
                pass            
            else:
                fname = driver.find_element(By.NAME,"first_name")
                fname.send_keys(firstName)

                lname = driver.find_element(By.NAME,"last_name")
                lname.send_keys(lastName)

            # username = driver.find_element(By.NAME, "username")
            # if username:     
            #     username.send_keys(un)
            # else:
            #     pass

            password = driver.find_element(By.NAME, "password1")
            password.send_keys(password)

            re_password = driver.find_element(By.NAME, "password2")
            re_password.send_keys(password)

            email = driver.find_element(By.name, "email")
            if email:
                if el == '':
                    email.send_keys("hacker" + un + "@gmail.com")
                else:
                    email.send_keys(el)
            else:
                pass

            # outside = driver.find_element(By.NAME, "log")
            # if outside:
            #     outside.click()
            # else:
            #     pass
            
            btn = driver.find_element(By.CLASS_NAME, "button")
            btn.click()

            #Waiting to get registered:
            time.sleep(5)

            driver.quit()
            
            number_of_signups += 1
            print("No. of Signups so far: {}".format(number_of_signups))

            
        
if __name__ == "__main__":
    print("Input info here:")
    site = input("input site url here: ")
    firstName = input("input first name if needed: ")
    lastName = input("input last name if needed: ")
    email = input("input email if needed: ")
    password = str(input("Input the password: "))
    


    main(site, firstName, lastName, email, password)

the error i get from the terminal is:

Input info here:
input site url here: http://127.0.0.1:3000/
input first name if needed: malik
input last name if needed: kamil
input email if needed: [email protected]
Input the password: 0987654321qQ

DevTools listening on ws://127.0.0.1:55176/devtools/browser/3af7b233-5460-4af4-937a-f3da8c701157
Traceback (most recent call last):
  File "c:\Users\MAIN_new\signup script.py", line 97, in <module>
    main(site, firstName, lastName, email, password)
  File "c:\Users\MAIN_new\signup script.py", line 54, in main
    password.send_keys(password)
  File "C:\Users\MAIN_new\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\remote\webelement.py", line 231, in send_keys
    Command.SEND_KEYS_TO_ELEMENT, {"text": "".join(keys_to_typing(value)), "value": keys_to_typing(value)}
                                                   ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MAIN_new\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\common\utils.py", line 137, in keys_to_typing
    characters.extend(val)
TypeError: 'WebElement' object is not iterable

Solution

  • Read carefully what you are assigning in your code. You are overwriting the parameter password by assigning the element you are trying to find with selenium.

    def main(site, firstName, lastName, el, password):
        # doing stuff ...
        password = driver.find_element(By.NAME, "password1")
        password.send_keys(password)
        # more stuff ...
    

    Your parameter password is being overwritten by the driver.find_element() method, so when you want to password.send_keys() to the element, you are not passing the string password but the element you found with selenium. Just rename it

    password_element = driver.find_element(By.NAME, "password1")
    password_element.send_keys(password)