我怎样才能最好地调整我的 Selenium 代码以安全地输入用户名和密码?
How can I best adapt my Selenium code to enter username and password securely?
我正在使用 Selenium 在网站上输入登录详细信息。用户名元素是:
<input class="stdnavinitialfocus subfocus" id="j_username" name="j_username" type="text" autocapitalize="off" autofocus="autofocus" aria-labelledby="usernamelabel">
密码元素是:
<input class="subfocus" id="j_password_pseudo" name="j_password_pseudo" type="password" maxlength="47" aria-labelledby="passwordlabel" role="application">
我目前有以下输入用户名和密码的代码:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "j_username"))).send_keys("Username")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "j_password_pseudo"))).send_keys("Password")
任何人都可以建议一种更好的方法来编写输入用户名和密码的代码吗?
就像上面提到的那样,这是非常标准的登录方式,
根据您显示的元素 ID 使用 WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "j_password_pseudo"))).send_keys("密码")'
而不是NAME
如果您希望在代码之外保护您的用户名和密码安全,您可以将它们放在 YMAL 文件中以供 python 解码并分配给局部变量或加密的 txt 文件以分配用户名和密码
使用 Import os 库捕获用户名的其他方法(如果它与您的本地环境相同)将是 username = os.environ.get('USERNAME')
希望这对您有所帮助
我正在使用 Selenium 在网站上输入登录详细信息。用户名元素是:
<input class="stdnavinitialfocus subfocus" id="j_username" name="j_username" type="text" autocapitalize="off" autofocus="autofocus" aria-labelledby="usernamelabel">
密码元素是:
<input class="subfocus" id="j_password_pseudo" name="j_password_pseudo" type="password" maxlength="47" aria-labelledby="passwordlabel" role="application">
我目前有以下输入用户名和密码的代码:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "j_username"))).send_keys("Username")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "j_password_pseudo"))).send_keys("Password")
任何人都可以建议一种更好的方法来编写输入用户名和密码的代码吗?
就像上面提到的那样,这是非常标准的登录方式,
根据您显示的元素 ID 使用 WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "j_password_pseudo"))).send_keys("密码")'
而不是NAME
如果您希望在代码之外保护您的用户名和密码安全,您可以将它们放在 YMAL 文件中以供 python 解码并分配给局部变量或加密的 txt 文件以分配用户名和密码
使用 Import os 库捕获用户名的其他方法(如果它与您的本地环境相同)将是 username = os.environ.get('USERNAME')
希望这对您有所帮助