简体   繁体   中英

Selenium Object of Type WebElement has no len()

I'm trying to log into Facebook Marketplace from Selenium. The email address is populating as expected. The password field is not being recognized.

The error:

File "C:\Users\jsmith\fb.py", line 18, in password.send_keys(password) TypeError: object of type 'WebElement' has no len()

My code:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.keys import Keys
import time

username = 'myemail'
password = 'mypassword'
#OpenBrowser
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://facebook.com/")

#Logging
email = driver.find_element_by_id('email')
password = driver.find_element_by_xpath('//*[@id="pass"]')
#facebook username input
email.send_keys(username)
#facebook password input
password.send_keys(password)

I have tried using find_element_by_name() and find_element_by_id() (and the plural of both) in addition to the xpath above.

In your code block the variable password initially was referring to the string value of mypassword . Moving forward, password was reinitialized as a WebElement. Hence when you pass the variable password within send_keys(password) it referred to an WebElement. Hence you see the error:

password.send_keys(password) TypeError: object of type 'WebElement' has no len()

Solution

Change the variable name of the Password field from password to pass as follows:

password = 'mypassword'
# other lines of code
pass = driver.find_element_by_xpath('//*[@id="pass"]')
#facebook password input
pass.send_keys(password)

You have 2 variables named password, so when you try to send you password string to input, it won't work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM