今天以一個表單的自動提交,來進一步學習selenium的用法
練習目標
0)運用selenium啟動firefox并載入指定頁面(這部分可查看本人文章 http://www.cnblogs.com/liu2008hz/p/6958126.html)
1)頁面元素查找(多種查找方式:find_element_*)
2)內容填充(send_keys)
3)iframe與父頁面切換(switch_to_frame是切換到iframe,switch_to_default_content是切換到主頁面)
4)瀏覽器交互處理:window.alert, window.confirm, window.prompt
與上面的三個瀏覽器交互內容,需要用到switch_to_alert,有幾個用法需要注意:
a)accept():發送確定指令,相當于點擊“確定”按鈕
b)dismiss():取消操作,相當于點擊“取消”按鈕或點擊右上角“關閉”
c)send_keys:填充prompt框需要填寫的內容
準備工作
html頁面(注冊頁,內嵌一個注冊表單;之所以這樣舉例,是為了介紹練習selenium的switch_to_frame的用法)
1)注冊頁面(路徑D:/RegisterDEMO/index.htm)
<!DOCTYPE><html><head> <title>用戶注冊</title> <meta charset="utf-8" /></head><body> <h3>測試Python selenium自動提交表單</h3> <iframe id="register_iframe" width="320" height="200" border="0" src="register.htm" /></body></html>
2)注冊表單(路徑D:/RegisterDEMO/register.htm)
<!DOCTYPE><html><head> <title>這是內嵌表單</title> <meta charset="utf-8" /> <style type="text/css">  input[type='text']{border:1px solid #abc; font-size:14px; padding:5px; width:200px;}  input[type='password']{border:1px solid #abc; font-size:14px; padding:5px; width:200px;}  input[type='submit']{border:1px solid #abc; font-size:14px; padding:5px 10px; width:100px; cursor:pointer; margin-top:20px;}  input[type='submit']:hover{background-color:#aaaaff;} </style></head><body> <form action="/register/regaction" method="POST">  <table>   <tr>    <td>用戶名:</td>    <td><input id="txt_account" type="text" value="" placeholder="用戶名" /></td>   </tr>   <tr>    <td>密碼:</td>    <td><input id="txt_password" type="password" value="" placeholder="密碼" /></td>   </tr>   <tr>    <td>電子郵箱:</td>    <td><input id="txt_email" type="text" value="" placeholder="電子郵箱" /></td>   </tr>   <tr>    <td> </td>    <td><input id="btn_register" type="submit" value="提交注冊" onclick="return confirm('是否確認提交注冊');" /></td>   </tr>  </table> </form></body></html>            
新聞熱點
疑難解答