欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python 自動(dòng)化表單提交實(shí)例代碼

 更新時(shí)間:2017年06月08日 14:26:28   作者:liuhz  
今天以一個(gè)表單的自動(dòng)提交,來進(jìn)一步學(xué)習(xí)selenium的用法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧

今天以一個(gè)表單的自動(dòng)提交,來進(jìn)一步學(xué)習(xí)selenium的用法

練習(xí)目標(biāo)

  0)運(yùn)用selenium啟動(dòng)firefox并載入指定頁面(這部分可查看本人文章 http://www.cnblogs.com/liu2008hz/p/6958126.html

  1)頁面元素查找(多種查找方式:find_element_*)

  2)內(nèi)容填充(send_keys)

  3)iframe與父頁面切換(switch_to_frame是切換到iframe,switch_to_default_content是切換到主頁面)

  4)瀏覽器交互處理:window.alert, window.confirm, window.prompt

    與上面的三個(gè)瀏覽器交互內(nèi)容,需要用到switch_to_alert,有幾個(gè)用法需要注意:

    a)accept():發(fā)送確定指令,相當(dāng)于點(diǎn)擊“確定”按鈕

    b)dismiss():取消操作,相當(dāng)于點(diǎn)擊“取消”按鈕或點(diǎn)擊右上角“關(guān)閉”

    c)send_keys:填充prompt框需要填寫的內(nèi)容 

準(zhǔn)備工作

  html頁面(注冊(cè)頁,內(nèi)嵌一個(gè)注冊(cè)表單;之所以這樣舉例,是為了介紹練習(xí)selenium的switch_to_frame的用法)

  1)注冊(cè)頁面(路徑D:\RegisterDEMO\index.htm)  

<!DOCTYPE>
<html>
<head>
 <title>用戶注冊(cè)</title>
 <meta charset="utf-8" />
</head>
<body>
 <h3>測(cè)試Python selenium自動(dòng)提交表單</h3>
 <iframe id="register_iframe" width="320" height="200" border="0" src="register.htm" />
</body>
</html>

   2)注冊(cè)表單(路徑D:\RegisterDEMO\register.htm)

<!DOCTYPE>
<html>
<head>
 <title>這是內(nèi)嵌表單</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="提交注冊(cè)" onclick="return confirm('是否確認(rèn)提交注冊(cè)');" /></td>
   </tr>
  </table>
 </form>
</body>
</html>

運(yùn)行步驟

  我們通過Python IDLE一步步來運(yùn)行,這有助于理解,一步一個(gè)操作,驚喜不斷

  1)引入selenium模塊

from selenium import webdriver

  2)啟動(dòng)firefox并載入注冊(cè)頁面

bs = webdriver.Firefox()
bs.get('file:///D:/RegisterDEMO/index.htm')

  3)查找輸入框(用戶名、密碼、電子郵件)和按鈕(提交注冊(cè)),并填充指定內(nèi)容

# 由于表單內(nèi)容是嵌在iframe里的,所以需要查找指向至iframe
# 如果又想跳出iframe,回到父頁面,可以使用 bs.switch_to_default_content()
bs.switch_to_frame('register-iframe')
# 由于所有的元素都命名了id,可以使用find_element_by_id,還有很多的其它find_element_*大家可以練習(xí)
# 查找用戶名框,并填充“hertz.liu"
account = bs.find_element_by_id('txt_account')
account.send_keys('hertz.liu')
# 查找密碼框,并填充"pwd123"
pwd = bs.find_element_by_id('txt_password')
pwd.send_keys('pwd123')
# 查找電子郵箱框,并填充”hertz.liu@mail.com"
email = bs.find_element_by_id('txt_email')
email.send_keys('hertz.liu@mail.com')
# 查找提交按鈕,并模擬點(diǎn)擊提交
btn_reg = bs.find_element_by_id('btn_register')
btn_reg.click()

  4)非常順利的,完成了表單的填充和提交。一般的表單,由于涉及到數(shù)據(jù)的操作,開發(fā)人員都會(huì)設(shè)置一些二次確認(rèn)以防止誤操作。此處就是用了簡(jiǎn)單的confirm來進(jìn)行二次確認(rèn),下面是如何讓selenium來識(shí)別出confirm框,并點(diǎn)擊“確定”按鈕

# 將查找對(duì)象轉(zhuǎn)移至confirm
confirm = bs.switch_to_alert()
# 點(diǎn)擊確定按鈕
confirm.accept()
# 如果要取消,使用confirm.dismiss()
# 如果是prompt,則可以使用send_keys()先填充內(nèi)容,再調(diào)用accept()或dismiss()

  5)關(guān)閉瀏覽器

bs.close()

以上所述是小編給大家介紹的Python 自動(dòng)化表單提交實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論