简体   繁体   中英

selenium webdriver python finding elements with out id

I am new to selenium python, How to select username field and password field to automate the login page. the id is not given for both fields. this is the source code.

<div class='tablecss'>   
<table cellpadding='1' cellspacing='1' border='0' align='center'> <tr 
id='usernamelbl'><td style='color:#565656;text-align:left;'>
<label >Username</label></td></tr><tr id='usernametxt'>
<td><input type='text' name='username' maxlength='50' class='textbox' >
</td></tr>
<tr id='passwordlbl'><td style='color:#565656;text-align:left;'>
<label>Password</label></td></tr><tr><td>
<input type='password' name='password' autocomplete='off' 
class='textbox'>
</td></tr>
<tr><td>
<input type='submit' name='btnSubmit' value='Login' id='logincaption' 
class='button' style='color:#565656' /></td></tr></table></div> 

Below is the code that I use to select the elements:

user = driver.find_element_by_css_selector('#txtUserName')
user.send_keys('pv13')
password = driver.find_element_by_css_selector('#txtPassword')
password.send_keys('9979173')

As you have done it here, it won't work as there is no ID 'txtUserName' or 'txtPassword'.

If you want to select the input for the username and password, you could simply do it by xpath.

For example:

user_name_input = driver.find_element_by_xpath('//input[@name="username"]')
user_name_input.send_keys('pv13')
password_input = driver.find_element_by_css_selector('//input[@name="password"]')
password_input.send_keys('9979173')

Hope that helped!

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