【问题标题】:How to fill username and password with selenium on a page where IDs are hidden?如何在隐藏 ID 的页面上用 selenium 填充用户名和密码?
【发布时间】:2020-03-23 09:06:40
【问题描述】:

我正在尝试用 python 代码填充用户名和密码。尽管代码成功打开了页面,但它并没有填写用户名和密码部分。我附上以下代码:

from selenium import webdriver
import time
from selenium.webdriver.chrome.options import Options

options = webdriver.ChromeOptions()
options.add_argument('--ignore-ssl-errors=yes')
options.add_argument('--ignore-certificate-errors')
options.add_argument('--allow-running-insecure-content')
driver = webdriver.Chrome(options=options)

driver.get('https://120.72.92.102:10443/remote/login?lang=en')
username = driver.find_element_by_id("Name")
password = driver.find_element_by_id("password")

username.send_keys("YourUsername")
password.send_keys("PassworD")

driver.find_element_by_name("Login").click()

页面的图像如下所示:

【问题讨论】:

    标签: python python-3.x selenium webdriver selenium-chromedriver


    【解决方案1】:

    您从页面中选择了错误的 ID。您选择的是type 属性而不是ID。
    您可以使用以下代码在页面上进行操作(已选择正确的 id)并在第一个元素上应用显式等待:

    from selenium import webdriver
    import time
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    options = webdriver.ChromeOptions()
    options.add_argument('--ignore-ssl-errors=yes')
    options.add_argument('--ignore-certificate-errors')
    options.add_argument('--allow-running-insecure-content')
    driver = webdriver.Chrome(options=options)
    
    driver.get('https://120.72.92.102:10443/remote/login?lang=en')
    WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, "username")))
    
    username = driver.find_element_by_id("username")
    password = driver.find_element_by_id("credential")
    
    username.send_keys("YourUsername")
    password.send_keys("PassworD")
    
    driver.find_element_by_id("login_button").click()
    

    【讨论】:

    • 成功了!伟大的。非常感谢,伙计!我不想打扰你,但如果你能告诉我你是如何确定正确的 id 以及 WebDriverWait 在这里做什么,我将非常感激?
    • @PranjalPathak 我在这里使用了显式等待,它一直等到页面上出现元素。如果你检查元素并检查它的 html,你可以找到一个名为 id 的属性,所以我从那里得到了 ids
    • 非常感谢!这是一个很大的帮助。
    猜你喜欢
    • 2013-09-10
    • 2021-04-06
    • 1970-01-01
    • 1970-01-01
    • 2014-01-12
    • 2012-04-19
    • 1970-01-01
    • 2020-03-01
    相关资源
    最近更新 更多