简体   繁体   中英

How to locate the username and password element through Selenium WebDriver and Python

I am attempting to automatically enter my username and password for a particular website with Selenium. I am having trouble finding the element to send the keys to. I have attached the website's HTML code below.

Here is what I am currently using:

username = driver.find_element_by_id('UserName').getAttribute('value')

Additionally, the website ends in a .bsd which I am not entirely familiar with and think that it could be contributing to the problem.

Thanks.

 <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <link rel="stylesheet" href="css/general.css" type="text/css"> <title>RC Inspection - Risk Control</title> <script language="javascript1.3" src="scripts/clientside/badgerAPI.js"></script> <script language="javascript1.3" src="scripts/clientside/inspectionClient.js"></script> <script language="javascript1.3" src="scripts/clientside/validate.js"></script> <script language="javascript1.3" src="scripts/widJETs/JETClient.js"></script> <script language="javascript1.3" src="scripts/widJETs/JETDataTable.js"></script> <script>var searchParameters;</script> </head> <frameset rows="0,95,*" framespacing="0"> <frame name="SystemWin" src="about:blank" bgcolor="#FFFFFF" marginwidth="0" marginheight="0" frameborder="0" framespacing="0" scrolling="auto"> <frame name="HeaderWin" src="about:blank" bgcolor="#FFFFFF" marginwidth="0" marginheight="0" frameborder="0" framespacing="0" scrolling="no"> <frame name="MainWin" src="Logon.bsd?Lang=English'" marginwidth="0" marginheight="0" frameborder="0" framespacing="0" scrolling="auto" <html><head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <script language="javascript1.3" src="scripts/clientside/XHConn.js"></script> <link rel="stylesheet" href="css/logon.css" type="text/css"> <script language="javascript1.3"> function executeAction(sFunction){ if (sFunction=='OnLoad') { this.document.MainForm.UserName.focus(); hide('r5'); } else if (sFunction=='Login') { fParms='UserName,T,50,1|Password,T,20,1'; if(top.validatePage('top.MainWin.document.MainForm',fParms,'English')) top.goPage('LogonUser@Logon.bsd?form=top.MainWin.document.MainForm|target=SystemWin|Lang=English') ; } else if (sFunction=='ForgotPassword') { hide('r1'); hide('r2'); show('r5'); } else if (sFunction=='ResetPassword') { if(top.MainWin.document.MainForm.RP_UserName.value == "") { alert('Please enter Your Username'); return false; } var r = confirm("Are you sure you wish to reset your password?"); if (r == true) { try { var myConn = new XHConn(); if (!myConn) alert("XMLHTTP not available. Try a newer/better browser."); myConn.connect("ResetPassword@Utilities.bsd", "POST", "RP_UserName="+this.document.MainForm.RP_UserName.value+"&Lang=English", fn_ChangeItem); } catch(e) { var msg = (typeof e == "string") ? e : ((e.message) ? e.message : "Unknown Error"); alert("Unable to get XML data:\\n" + msg); return; } } } } fn_ChangeItem = function (XML) { eval(XML.responseText) ; } hide=function (szDivID) { if(document.layers) //NN4+ eval("document."+szDivID+".visibility = 'hide'"); else if(document.getElementById) //gecko(NN6) + IE 5+ { var obj = document.getElementById(szDivID); obj.style.display = "none"; } else if(document.all) // IE 4 document.all[szDivID].style.display = "none"; } show=function (szDivID) { if(document.layers) //NN4+ eval("document."+szDivID+".visibility = 'show'"); else if(document.getElementById) //gecko(NN6) + IE 5+ { var obj = document.getElementById(szDivID); obj.style.display = ""; } else if(document.all) // IE 4 document.all[szDivID].style.display = ""; } </script> </head> <body onload="javascript:executeAction('OnLoad');" marginwidth="0" marginheight="0"> <div class="loginwrapper"> <div class="logoncontent"> <div id="logon_header"> <img width="341" border="0" src="./images/companylogo.gif"> <div class="logintop"> Risk Control System <br> version 4.5.4.1.5 </div> </div> <div class="clear">&nbsp;</div> <div id="logon_content"> <form name="MainForm" enctype="multipart/form-data"> <div id="r1"> <div style="float:left;margin-right:20px;"> <label for="UserName">User Name</label> <input type="text" id="UserName" name="UserName" maxlength="50" tabindex="1"> </div> <div style="float:left;"> <label for="Password">Password</label> <input type="password" id="Password" name="Password" maxlength="20" tabindex="2" onkeypress="if (event.keyCode=='13') executeAction('Login');"> </div> </div> <div class="clear">&nbsp;</div> <div id="r2"> <a class="small" href="javascript:void(0);" onclick="executeAction('ForgotPassword');return false;">Forgot Password?</a> <span class="action_button green_button"><a href="javascript:void(0);" tabindex="3" onclick="executeAction('Login');">Login</a></span> </div> <div id="r5" style="display: none;"> <label class="rp_label" for="RP_UserName">Please Enter Your Username</label> <input type="text" name="RP_UserName" value="" size="30" maxlength="50">&nbsp;&nbsp; <!-- <a href="javascript:void(0);" onclick="executeAction('ResetPassword');return false;">Reset Password</a> --> <input type="submit" value="Reset Password" onclick="executeAction('ResetPassword');return false;"> </div> </form> </div> </div> <div class="clear">&nbsp;</div> <div class="loginbottom"> <a style="text-decoration: none;" href="http://www.riskcontroltech.com/" target="_blank">Risk Control Technologies Inc.</a> Copyright © 2015. All rights reserved.<br> All Trademarks are owned by their respective companies. </div> </div><!-- end loginwrapper --> </body></html> </frameset> </html> 

Just use WebDriverWait :

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

driver = webdriver.Chrome(executable_path=r'C:\path\To\chromedriver.exe')
driver.get(url)

wait = WebDriverWait(driver, 10)

username = wait.until(EC.presence_of_element_located((By.XPATH, "//input[@id='UserName']")))

# username = driver.find_element_by_xpath("//input[@id='UserName']")
username.send_keys("the useName")

# password = driver.find_element_by_xpath("//input[@id='Password']")

password =wait.until(EC.presence_of_element_located((By.XPATH, "//input[@id='Password']")))
password.send_keys("yourPassword")

Hope this helps you!

To send character sequence to the username and password field in the particular website as the elements are within a <frame> you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it .
  • Induce WebDriverWait for the desired element to be clickable .
  • You can use either of the following solutions:

    • Using CSS_SELECTOR :

       WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"frame[name='MainWin'][src^='Logon.bsd?Lang=English']"))) WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label[for='UserName']"))).send_keys("Its_code_outside") driver.find_element_by_css_selector("label[for='Password']").send_keys("Its_code_outside") 
    • Using XPATH :

       WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//frame[@name='MainWin' and starts-with(@src, 'Logon.bsd?Lang=English')]"))) WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[@for='UserName']"))).send_keys("Its_code_outside") driver.find_element_by_xpath("//label[@for='Password']").send_keys("Its_code_outside") 
  • Note : You have to add the following imports :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM