使用 Selenium 从 Python 中的列表自动填写 Web 表单

Automated filling of web form from a list in Python using Selenium

这是 Python "sel.py" 中的 Selenium 代码 :-

from selenium import webdriver

driver = webdriver.Chrome()

driver.get('http://127.0.0.1:8000/employee/register/')

employes = ['1', '2016-1-1', 'Tarun', 'Gupta', 'Male', '1995-12-7', 'Indian', 'Hinduism', 'General', 'O+', 'Single', '1122334455', 'diploma', 'Btech', 
              'HMRITM','GGSIPU', '2016', 'distinction', '95', 'full time', 'Web Developer', 'Comp Science', '2017', '95000', '8700925621', '44087062', 
              'gupta.tarun@gmail.com', 'Vasant Vihar', 'East delhi', 'Delhi', '110056']

input = []
input = driver.find_elements_by_xpath('//*[@id]')
i = 0
for xpaths in input:
    xpaths.send_keys(employes[i]) 
    print(employee[i])
    i += 1


driver.find_element_by_xpath('//*[@id="Submit"]').click()

这是注册表:-

这是 HTML 代码,但这里的输入标签是 python 变量,正如在 Django 中所做的那样:-

<form action="/employee/register/" method="post">{% csrf_token %}
        <div class="col-md-12 col-lg-12">

            <div id="col1" class="col-md-2 col-lg-2"></div>
            <div id="col2" class="col-md-4 col-lg-4">
              <h4 class="text-center">Application Details</h4>
                <fieldset class="form-group">
                        <label for="Applicationno">Application No.</label>
                        <!--<input type="number" class="form-control" id="Applicationno" name="Applicationno"
                        placeholder="Application No.">-->
                        {{appform.ApplicationNo}}
                </fieldset>

                <fieldset class="form-group">
                    <label for="applyon">Apply On</label>
                    <!--<input type="date" class="form-control" id="applyon" name="applyon">-->
                    {{appform.ApplyOn}}
                </fieldset>

            </div>
            <div id="col3" class="col-md-6 col-lg-6">

                 <h4 class="text-center">Personal Details</h4>
                <div class="form-inline">
                    <!--<select id="title1" name="title1" class="form-control">-->
                        <!--<option selected>Title</option>-->
                        <!--<option>Mr.</option>-->
                        <!--<option>Mrs.</option>-->
                        <!--<option>Ms.</option>-->
                    <!--</select>-->
                <label for="firstName1"></label>
                    <!--<input id="firstName1" class="form-control" name="firstName1" type="text" placeholder="First name" >-->
                    {{empform.FirstName}}
                <label for="lastName1"></label>
                    <!--<input id="lastName1" class="form-control" name="lastName1" type="text"  placeholder="Last name" >-->
                    {{empform.LastName}}
               </div>

               <br>

                <div class="form-inline">
                <label>Gender:
                    {{empform.Gender}}</label>
                    <!--<label for="Male" class="radio-inline">-->
                        <!--<input id="Male" name="Male" type="radio">Male</label>-->

                    <!--<label for="Female" class="radio-inline">-->
                        <!--<input id="Female" name="Female" type="radio">Female</label>-->

                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

                <label for="BirthDate">
                    Birth Date:
                    <!--<input id="BirthDate" name="BirthDate" type="date" class="form-control">-->
                    {{empform.BirthDate}}
                </label>
                    </div>

                <br>
                <div class="form-inline">
                <label for="Nationality">
                    Nationality:
                    <!--<input id="Nationality" class="form-control" name="Nationality" type="text"  placeholder="Nationality" >-->
                    {{empform.Nationality}}
                </label>

                    &nbsp;
                    &nbsp;
                    &nbsp;
                <label for="Religion">
                    Religion:
                    <!--<input id="Religion" class="form-control" name="Religion" type="text"  placeholder="Religion" >-->
                    {{empform.Religion}}
                </label>
                 </div>

                <br>
                <div class="form-inline">
                <label class="Category" for="Category">
                    {{empform.Category}}
                    <!--Category:-->
                    <!--<select id="Category" name="Category" class="form-control">-->
                        <!--<option selected="selected">GEN</option>-->
                        <!--<option>OBC</option>-->
                        <!--<option>SC</option>-->
                        <!--<option>ST</option>-->
                    <!--</select>-->
                </label>
                   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                   &nbsp;&nbsp;&nbsp;&nbsp;
                <label for="BloodGroup">
                    Blood Group:
                    <!--<input id="BloodGroup" class="form-control" name="BloodGroup" type="text"  placeholder="Blood Group" >-->
                    {{empform.BloodGroup}}
                </label>
                    </div>

                <br>

                <div class="form-inline">
                <label class="MaritalStatus" for="MaritalStatus">
                    {{empform.MaritalStatus}}
                    <!--Marital Status:-->
                    <!--<select id="MaritalStatus" name="MaritalStatus" class="form-control">-->
                        <!--<option selected="selected">Single</option>-->
                        <!--<option>Married</option>-->
                    <!--</select>-->
                </label>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <label class="AdhaarCardNo" for="AdhaarCardNo">
                    Adhaar Card No.:
                    <!--<input type="number" class="form-control" id="AdhaarCardNo" name="AdhaarCardNo" placeholder="Adhaar card No.">-->
                    {{empform.AdhaarCardNo}}
                </label>
                    </div>

            </div>

        </div>


        <div class="col-md-12 col-lg-12" style="margin-top:5px;">

            <div class="col-md-5 col-lg-5">

                <div class="row" style="padding:5px 5px 5px 5px;">

                    <div id="col4" class="col-md-12 col-lg-12">

                          <h4 class="text-center">Education & Qualification Details</h4>
                          <div class="form-inline">
                        <label class="Level" for="Level">
                      Level:
                            {{eduform.Level}}
                    <!--<select id="Level" name="Level" class="form-control">-->
                        <!--<option selected="selected">Diploma</option>-->
                        <!--<option>Bachelors</option>-->
                        <!--<option>Masters</option>-->
                        <!--<option>Professional</option>-->
                    <!--</select>-->
                </label>
                 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <label class="Degree">
                    Degree:
                    <!--<input type="text" id="Degree" name="Degree" placeholder="Degree" class="form-control">-->
                    {{eduform.Degree}}
                </label>
                              </div>
                        <br>
            <div class="form-inline">
                <label class="College">
                    College:
                    <!--<input type="text" id="College" name="College" placeholder="College" style="width:350px;" class="form-control">-->
                    {{eduform.College}}
                </label>
              </div>
                <br>
                        <div class="form-inline">
                <label class="University">
                    University:
                    <!--<input type="text" id="University" name="University" placeholder="University" style="width:350px;" class="form-control">-->
                    {{eduform.University}}
                </label>
                            </div>
                <br>

         <div class="form-inline">
                <label class="YearOfQualification">
                    Year Of Qualification:
                    <!--<input type="date" id="YearOfQualification" name="YearOfQualification" class="form-control">-->
                    {{eduform.YearOfPassing}}
                </label>
             </div>
                        <br>
                 <div class="form-inline">
                <label class="Class" for="Class">
                    Class:
                    {{eduform.Class}}
                    <!--<select id="Class" name="Class" class="form-control">-->
                        <!--<option selected="selected">Distinction</option>-->
                        <!--<option>First</option>-->
                        <!--<option>Second</option>-->
                        <!--<option>Third</option>-->
                    <!--</select>-->
                </label>
                &nbsp;
                <label class="Percentage">
                    Percentage %:
                    <!--<input type="number" id="Percentage" name="Percentage" placeholder="Percentage" class="form-control">-->
                    {{eduform.Percentage}}

                </label>

                    </div>
            </div>
                </div>
                <div class="row" style="padding:5px 5px 5px 5px;">

                    <div id="col5" class="col-md-12 col-lg-12">

                        <h4 class="text-center">Official Details</h4>
                        <!--<label class="EmpCode">-->
                    <!--Emp Code:-->
                    <!--<input type="number" id="EmpCode" name="EmpCode" placeholder="Emp Code" class="form-control">-->
                <!--</label>-->
                 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <label class="EmpType" for="EmpType">
                    Emp Type:
                    {{offform.EmpType}}
                    <!--<select id="EmpType" name="EmpType" class="form-control">-->
                        <!--<option selected="selected">Full Time</option>-->
                        <!--<option>Part Time</option>-->
                    <!--</select>-->
                </label>
                <br><br>
                 <label class="Designation">
                    Designation:
                    <!--<input type="text" id="Designation" name="Designation" placeholder="Designation" class="form-control">-->
                     {{offform.Designation}}
                </label>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <label class="Department">
                    Department:
                    <!--<input type="text" id="Department" name="Department" placeholder="Department" class="form-control">-->
                    {{offform.Department}}
                </label>
                <br><br>
               <label class="JoiningOn">
                    Joining On:
                    <!--<input type="date" id="JoiningOn" name="JoiningOn" class="form-control">-->
                   {{offform.JoiningOn}}
                </label>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <label class="Salary">
                    Salary:
                    <!--<input type="number" id="Salary" name="Salary" class="form-control">-->
                    {{offform.Salary}}
                </label>

                    </div>

                </div>

            </div>
            <div class="col-md-7 col-lg-7">

                 <div class="row" style="padding:5px 5px 5px 5px;">

                    <div id="col6" class="col-md-12 col-lg-12">

                        <h4 class="text-center">Contact Details</h4>
                                        <div class="form-inline">
                         <label class="Mobile">
                    Mobile:
                    <!--<input type="number" id="Mobile" name="Mobile" placeholder="Mobile" class="form-control">-->
                             {{contform.MobileNo}}
                </label>

                                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                   &nbsp;&nbsp;&nbsp;&nbsp;

                 <label class="PhoneNo" for="PhoneNo">
                    Phone No.:
                    <!--<input type="number" id="PhoneNo" name="PhoneNo" placeholder="Phone No." class="form-control">-->
                     {{contform.PhoneNo}}
                </label>
                                            </div>
                <br><br>

                 <div class="form-inline">
                <label class="Email" for="Email">
                    Email:
                    <!--<input type="email" id="Email" name="Email" placeholder="Email" class="form-control">-->
                    {{contform.EmailId}}
                </label>

                     </div>

                <br><br>
                <label class="Address" for="Address">
                    Address:
                    <!--<textarea id="Address" name="Address" placeholder="Address" rows="5" cols="70" class="form-control"></textarea>-->
                    {{contform.AddressLine}}
                </label>
                <br><br>
                        <div class="form-inline">
                 <label class="City" for="City">
                    City:
                    <!--<input type="text" id="City" name="City" placeholder="City" class="form-control">-->
                     {{contform.City}}
                </label>

                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

                 <label class="State">
                    State:
                    <!--<input type="text" id="State" name="State" placeholder="State" class="form-control">-->
                     {{contform.State}}
                </label>
                            </div>
                        <br>
                        <div class="form-inline">
                <label class="PinCode">
                    PinCode:
                    <!--<input type="number" id="PinCode" name="PinCode" placeholder="PinCode" class="form-control">-->
                    {{contform.PinCode}}
                </label>
                      </div>

                    </div>

                </div>
                <div class="row" style="padding:5px 5px 5px 5px;">

                    <div class="col-md-12 col-lg-12" style="margin:60px 10px 10px 10px;">

                       <div class="form-inline text-right">
                        <input type="submit" id="Submit" name="Submit" class="btn btn-success btn-lg" >
                        <input type="submit" id="Clear" name="Clear" value="Clear" class="btn btn-warning btn-lg"
                               style="margin-left:20px;">
                        <input type="submit" id="Cancel" name="Cancel" value="Cancel" class="btn btn-danger btn-lg"
                         style="margin-left:20px;">

                       </div>
                    </div>

                </div>

            </div>

        </div>
       </form>

我想用 "sel.py" 文件中列表 "employes" 中的文本填充表单中的所有 WebElements,但出现此错误:-

我不知道如何解决这个错误,感谢您的帮助! 提前致谢。

from selenium import webdriver
from selenium.webdriver.support.select import Select


driver = webdriver.Chrome()

driver.get('http://127.0.0.1:8000/employee/register/')



employees = { 'id_ApplicationNo':'1', 'id_ApplyOn':'2016-1-1', 'id_FirstName':'Tarun',
            'id_LastName':'Gupta', 'id_Gender':'Male', 'id_BirthDate':'1995-12-7', 'id_Nationality':'Indian',
            'id_Religion':'Hinduism', 'id_Category':'General', 'id_BloodGroup':'O+', 'id_MaritalStatus':'single',
            'id_AdhaarCardNo':'1122334455', 'id_Level':'diploma', 'id_Degree':'Btech', 'id_College':'HMRITM',
            'id_University':'GGSIPU', 'id_YearOfPassing':'2016', 'id_Class':'first', 'id_Percentage':'95',
            'id_EmpType':'part time', 'id_Designation':'Web Developer', 'id_Department':'Comp Science',
            'id_JoiningOn':'2017-3-3', 'id_Salary':'95000', 'id_MobileNo':'8700925621', 'id_PhoneNo':'44087062',
            'id_EmailId':'gupta.tarun@gmail.com', 'id_AddressLine':'Vasant Vihar', 'id_City':'East delhi',
            'id_State':'Delhi', 'id_PinCode':'110056' }

for key, value in employees.items():
    element = driver.find_element_by_id(key)
    if element.tag_name == "select":
       select = Select(element)
       select.select_by_visible_text(value)
    else:
       element.send_keys(value)


driver.find_element_by_id('Submit').click()

答案在这里!

代码打开 chrome 然后点击 url,填写表格,提交表格,休眠 3 秒并刷新页面。这一直持续到数据库中填充了 10 名员工代码中列表中的数据。

from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.chrome.options import Options
import time

driver = webdriver.Chrome()
driver.get('http://127.0.0.1:8000/employee/register/')

#lists for 10 employees
employees = [['1', '2016-1-1', 'Tarun', 'Gupta', 'Male', '1995-12-7', 'Indian', 'Hinduism', 'General', 'O+', 'single',
              '1122334455', 'diploma', 'ITI', 'HMRITM', 'GGSIPU', '2016', 'distinction', '95', 'full time',
              'Web Developer', 'Comp Science', '2017-3-5', '95000', '8700925621', '44087062', 'gupta.tarun@gmail.com',
              'Vasant Vihar', 'East delhi', 'Delhi', '110056'],

             ['2', '2016-2-2', 'Arun', 'Aggarwal', 'Male', '1995-11-6', 'Pakistani', 'Islam', 'General', 'A+',
              'married', '1122334453', 'bachelors', 'BComm', 'IIIT-H', 'IIIT-H', '2016', 'first', '69', 'full time',
              'Sales Person', 'Marketing', '2017-4-5', '85000', '8700922621', '44227062', 'aggarwal.arun@gmail.com',
              'Mayur Vihar', 'East delhi', 'Delhi', '110091'],

             ['3', '2016-3-3', 'Varun', 'Malik', 'Male', '1995-10-5', 'American', 'Christian', 'OBCs', 'B+', 'single',
              '1122334425', 'masters', 'MCA', 'Hans Raj', 'DU', '2014', 'distinction', '98', 'part time',
              'Web Designer', 'Comp Science', '2017-5-5', '75000', '8700555621', '44567062', 'malik.varun@gmail.com',
              'Kailash Vihar', 'West delhi', 'Delhi', '110056'],

             ['4', '2016-4-4', 'Karun', 'Rao', 'Male', '1995-9-4', 'British', 'Christian', 'General', 'AB+', 'married',
              '1122334155', 'professional', 'Mtech', 'MRITM', 'GGSIPU', '2012', 'distinction', '88', 'full time',
              'Android Developer', 'Comp Science', '2017-6-5', '65000', '8777925621', '44098762', 'rao.karun@gmail.com',
              'Preet Vihar', 'Central delhi', 'Delhi', '110045'],

             ['5', '2016-5-5', 'Garima', 'Bhardwaj', 'Female', '1994-12-7', 'Chinese', 'Buddhist', 'STs', 'A-',
              'single', '1122734455', 'diploma', 'ITI', 'HMRITM', 'GGSIPU', '2016', 'second', '61', 'full time',
              'Network manager', 'IT', '2017-7-5', '55000', '8708825621', '44145062', 'bhardwaj.garima@gmail.com',
              'Nirman Vihar', 'South delhi', 'Delhi', '110023'],

             ['6', '2016-6-6', 'Jarun', 'Sachdeva', 'Male', '1993-12-7', 'Indian', 'Sikh', 'General', 'B-', 'single',
              '1122333755', 'masters', 'MCom', 'SRCC', 'DU', '2015', 'third', '58', 'part time',
              'Data Analyst', 'Comp Science', '2017-8-5', '45000', '8709755621', '44087682', 'sachdeva.jarun@gmail.com',
              'Kala Vihar', 'North delhi', 'Delhi', '110098'],

             ['7', '2016-7-7', 'Nikita', 'Garg', 'Female', '1995-6-7', 'Indian', 'Hinduism', 'SCs', 'AB-', 'married',
              '1122332155', 'professional', 'Phd', 'IIT-D', 'IIT-D', '2013', 'distinction', '78', 'part time',
              'Machine Learner', 'R%D', '2017-9-5', '35000', '8700236621', '41234062', 'garg.nikita@gmail.com',
              'Hans Vihar', 'East delhi', 'Delhi', '110236'],

             ['8', '2016-8-8', 'Parul', 'Goel', 'Female', '1995-2-3', 'Indian', 'Hinduism', 'General', 'O-', 'single',
              '1126634455', 'diploma', 'ITI', 'HMRITM', 'GGSIPU', '2016', 'distinction', '92', 'full time',
              'Web FrontEnd', 'Comp Science', '2017-3-1', '25000', '8111925621', '47345062', 'goel.parul@gmail.com',
              'Hari Vihar', 'Old delhi', 'Delhi', '110011'],

             ['9', '2016-9-9', 'Darun', 'Goyal', 'Male', '1996-1-5', 'Japanese', 'Hinduism', 'OBCs', 'O+', 'married',
              '1122388455', 'bachelors', 'BTech', 'MAIT', 'GGSIPU', '2015', 'first', '76', 'part time',
              'Web BackEnd', 'Comp Science', '2017-3-2', '15000', '8700944421', '49999062', 'goyal.darun@gmail.com',
              'Subhash Vihar', 'New delhi', 'Delhi', '110033'],

             ['10', '2016-10-10', 'Bhumika', 'Ghai', 'Female', '1996-7-7', 'Indian', 'Hinduism', 'General', 'A+',
              'single', '1122334895', 'bachelors', 'BTech', 'Amity', 'GGSIPU', '2016', 'distinction', '90', 'full time',
              'Web Developer', 'Comp Science', '2017-3-3', '40000', '8700956121', '43337062', 'ghai.bhumika@gmail.com',
              'Janak Vihar', 'East delhi', 'Delhi', '110078']]


for employes in employees:
    driver.find_element_by_id("id_ApplicationNo").send_keys(employes[0])
    driver.find_element_by_id("id_ApplyOn").send_keys(employes[1])
    driver.find_element_by_id("id_FirstName").send_keys(employes[2])
    driver.find_element_by_id("id_LastName").send_keys(employes[3])
    Select(driver.find_element_by_id("id_Gender")).select_by_visible_text(employes[4])
    driver.find_element_by_id("id_BirthDate").send_keys(employes[5])
    driver.find_element_by_id("id_Nationality").send_keys(employes[6])
    driver.find_element_by_id("id_Religion").send_keys(employes[7])
    Select(driver.find_element_by_id("id_Category")).select_by_visible_text(employes[8])
    driver.find_element_by_id("id_BloodGroup").send_keys(employes[9])
    Select(driver.find_element_by_id("id_MaritalStatus")).select_by_visible_text(employes[10])
    driver.find_element_by_id("id_AdhaarCardNo").send_keys(employes[11])
    Select(driver.find_element_by_id("id_Level")).select_by_visible_text(employes[12])
    driver.find_element_by_id("id_Degree").send_keys(employes[13])
    driver.find_element_by_id("id_College").send_keys(employes[14])
    driver.find_element_by_id("id_University").send_keys(employes[15])
    driver.find_element_by_id("id_YearOfPassing").send_keys(employes[16])
    Select(driver.find_element_by_id("id_Class")).select_by_visible_text(employes[17])
    driver.find_element_by_id("id_Percentage").send_keys(employes[18])
    Select(driver.find_element_by_id("id_EmpType")).select_by_visible_text(employes[19])
    driver.find_element_by_id("id_Designation").send_keys(employes[20])
    driver.find_element_by_id("id_Department").send_keys(employes[21])
    driver.find_element_by_id("id_JoiningOn").send_keys(employes[22])
    driver.find_element_by_id("id_Salary").send_keys(employes[23])
    driver.find_element_by_id("id_MobileNo").send_keys(employes[24])
    driver.find_element_by_id("id_PhoneNo").send_keys(employes[25])
    driver.find_element_by_id("id_EmailId").send_keys(employes[26])
    driver.find_element_by_id("id_AddressLine").send_keys(employes[27])
    driver.find_element_by_id("id_City").send_keys(employes[28])
    driver.find_element_by_id("id_State").send_keys(employes[29])
    driver.find_element_by_id("id_PinCode").send_keys(employes[30])
    driver.find_element_by_id("Submit").submit()
    time.sleep(3)
    driver.get('http://127.0.0.1:8000/employee/register/')