简体   繁体   English

使用 selenium 的函数我做错了什么?

[英]What am I doing wrong using functions with selenium?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

# define path of driver
driver = webdriver.Chrome()

def mainloop():
    driver.get("https://www.nitrotype.com/login")
    login(username, password)

# LOGGING IN

def login(username, password):
    time.sleep(2)
    username = driver.find_element_by_id('username')
    username.send_keys(username)
    time.sleep(1)
    password = driver.find_element_by_id('password')
    username.send_keys(username)
    time.sleep(1)
    password.send_keys(password, Keys.RETURN)


mainloop()

when I run this, it shows me当我运行它时,它显示了我

Traceback (most recent call last):
  File "/home/stevek/Projects/Python/nitrotype-bot/main.py", line 25, in <module>
    mainloop()
  File "/home/stevek/Projects/Python/nitrotype-bot/main.py", line 10, in mainloop
    login(username, password)
  File "/home/stevek/Projects/Python/nitrotype-bot/main.py", line 17, in login
    username.send_keys(username)
  File "/home/stevek/.local/lib/python3.9/site-packages/selenium/webdriver/remote/webelement.py", line 478, in send_keys
    {'text': "".join(keys_to_typing(value)),
  File "/home/stevek/.local/lib/python3.9/site-packages/selenium/webdriver/common/utils.py", line 150, in keys_to_typing
    for i in range(len(val)):
TypeError: object of type 'WebElement' has no len()

I'm so confused.我很混乱。 I didn't use any XPaths.我没有使用任何 XPath。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.get("https://www.nitrotype.com/login")
username = driver.find_element_by_id('username')
password = driver.find_element_by_id('password')
username.send_keys(username)
password.send_keys(password, Keys.RETURN)

When I didn't use functions, like this, it runs as expected.当我不使用像这样的函数时,它会按预期运行。 It logins.它登录。 No functions.没有功能。 What's wrong?怎么了? Also, note that I replaced all username with variables.另外,请注意我用变量替换了所有用户名。

You are passing variable names into login() that have the same name as the variables you are defining for the web elements within that function.您将变量名称传递到login()中,该名称与您为 function 中的 web 元素定义的变量具有相同的名称。 You can't do that.你不能那样做。

Change them to something different, like below user and pw将它们更改为不同的东西,例如下面的userpw

def mainloop():
    driver.get("https://www.nitrotype.com/login")
    login("test", "testPW")

# LOGGING IN

def login(user, pw):
    time.sleep(2)
    username = driver.find_element_by_id('username')
    username.send_keys(user)
    time.sleep(1)
    password = driver.find_element_by_id('password')
    time.sleep(1)
    password.send_keys(pw, Keys.RETURN)


mainloop()

I also removed the duplicate username.send_keys(user) in the function我还删除了 function 中重复的username.send_keys(user)

The problem is that you are overwriting the username parameter passed into the method with a WebElement then passing that to .send_keys() which is expecting a string.问题是您正在使用 WebElement 覆盖传递给方法的username参数,然后将其传递给.send_keys() ,它需要一个字符串。

def login(username, password):
          ^ username parameter is a string
    username = driver.find_element_by_id('username')
    ^ username is overwritten with a WebElement here

In your method, you don't need to assign the elements to a variable.在您的方法中,您不需要将元素分配给变量。 The whole method can be simplified down to two lines,整个方法可以简化为两行,

def login(username, password):
    driver.find_element_by_id('username').send_keys(username)
    driver.find_element_by_id('password').send_keys(username, Keys.RETURN)

I also removed all of the sleeps because they likely aren't needed.我还删除了所有的睡眠,因为它们可能不需要。 Best practice when you need a wait is to use WebDriverWait .需要等待时的最佳做法是使用WebDriverWait

Also, your final code with no method wouldn't work either because of the same reason.此外,由于同样的原因,没有方法的最终代码也无法工作。

username = driver.find_element_by_id('username')
^ this is a WebElement
username.send_keys(username)
^ this is a WebElement
                   ^ this is a WebElement but should be a string.

You likely hardcoded the .send_keys() string but replaced it when you "replaced all username with variables."您可能对.send_keys()字符串进行了硬编码,但在“用变量替换所有用户名”时替换了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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