如何使用Python和Selenium填写表单?

2024-10-02 16:21:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python新手,想用它自动登录。我找到https://automatetheboringstuff.com/chapter11/并尝试:

#! python3
from selenium import webdriver
browser = webdriver.Firefox()
type(browser)
browser.get('https://forum-studienstiftung.de/')
emailEl = browser.find_element_by_id(username)

不幸的是,这导致:

Traceback (most recent call last): File "", line 1, in emailEl = browser.find_element_by_id(username) NameError: name 'username' is not defined

根据Firefox开发工具,正确的ID是“用户名”。在


Tags: httpsbrowsercomidbyusernameelementfind
2条回答

用引号将用户名括起来。现在,您正在传递一个名为username的变量,selenium正试图与页面上具有相同值的id匹配。由于值为none,Selenium无法找到它,因此出现错误。在

您试图访问的页面需要时间来加载。在访问元素之前,必须等待元素可见。在

试试这个

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC


browser = webdriver.Firefox()
type(browser)
browser.get('https://forum-studienstiftung.de/')
emailEl = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.id, "username")))

相关问题 更多 >