在python中使用selenium在输入标记中填充用户名和密码

2024-10-02 08:19:34 发布

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

我的代码中有如下元素:

<input data-v-d6c12922="" type="text" name="account" placeholder="账号" class="username">  
<input data-v-d6c12922="" type="password" name="password" placeholder="密码" class="password">

我用过这个,但不起作用

userElem = browser.find_element_by_xpath("//html/body/div[1]/div[1]/div[2]/div/div[2]/input")

错误:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//html/body/div[1]/div[1]/div[2]/div/div[2]/input"}

如何自动填写用户名和密码

多谢各位


Tags: 代码namediv密码inputdatahtmltype
1条回答
网友
1楼 · 发布于 2024-10-02 08:19:34

问题可能是您试图在元素可见之前找到它

您可以使用下面的代码等待元素可见(我还通过CLASS_NAME而不是XPATH查找ID,并捕获一个屏幕截图以显示字段已填充)

driver.get("http://123.60.12.11:10016/#/login?url=http%3A%2F%2F123.60.12.11%3A10016%2F%23%2FMySociety%2FSocietyDetail")

wait = WebDriverWait(driver, 10)
username = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "username")))
username.send_keys("test@email.com")
pw = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "password")))
pw.send_keys("asdasd")

driver.get_screenshot_as_file("screenshot.png")

Working example

相关问题 更多 >

    热门问题