我正在使用 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")
任何人都可以建議一種更好的方法來撰寫此代碼以輸入用戶名和密碼嗎?
uj5u.com熱心網友回復:
就像上面提到的那樣,這是非常標準的登錄方式,
根據您作為元素 ID 顯示的內容,使用 WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "j_password_pseudo"))).send_keys("Password")'
而不是 NAME
如果您希望在代碼之外保護您的用戶名和密碼安全,您可以將它們放在 YMAL 檔案中供 python 解碼并分配給區域變數或加密的 txt 檔案以分配用戶名和密碼
如果它與本地環境相同,則使用 Import os 庫捕獲用戶名的其他方法是 username = os.environ.get('USERNAME')
希望這可以幫助
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/460957.html
