简体   繁体   中英

Python Selenium - entering value in a text box

I am using python selenium to automate the attendance entry of our students. The contents in a attendance web page is form of table. There is a text box where we have mark 'A' or 'P'. The source code is as follows:

<form name="attendance1" action="/sjcet/attendance.php" method="POST"> 
<table style="width: 65%; margin: auto;">
<tr>
    <th style="text-align: center;">Roll No.</th>
    <th style="text-align: center;">PID</th>
    <th style="text-align: center;">Student Name</th>
    <th style="text-align: center;">Status</th>
    <th style="text-align: center;">Any Comment</th>
</tr>
<tr style="text-align: center;">
    <td style="text-align: center;"> <input name="roll_no[]" tabindex="0" size="3" type='text' value = '1' readonly='readonly' style="text-align: center;" /> </td>
    <td style="text-align: center;"> <input name="pid[]" tabindex="0" size="10" type='text' value = 'EU1124021' readonly='readonly' style="text-align: center;" /> </td>
    <td style="text-align: left;"> Abraham Ancy Chandy Anne</td>
    <td style="text-align: center;"> 
        <input size="3" name="status[]" type="text" onkeypress="return isNumberKey(event)" maxlength ="1" tabindex="1" style="text-align: center;" value='A' />
    </td>
    <td style="text-align: center;"> 
        <input size="20" name="comment[]" type="text"  />
    </td>
</tr>
<tr style="text-align: center;">
    <td style="text-align: center;"> <input name="roll_no[]" tabindex="0" size="3" type='text' value = '2' readonly='readonly' style="text-align: center;" /> </td>
    <td style="text-align: center;"> <input name="pid[]" tabindex="0" size="10" type='text' value = 'EU2134011' readonly='readonly' style="text-align: center;" /> </td>
    <td style="text-align: left;"> Barabde Pranjal  Sanjiv Sudha</td>
    <td style="text-align: center;"> 
        <input size="3" name="status[]" type="text" onkeypress="return isNumberKey(event)" maxlength ="1" tabindex="2" 
            style="text-align: center;" value='A' />
    </td>
    <td style="text-align: center;"> 
        <input size="20" name="comment[]" type="text"  />
    </td>
</tr>

The code for the text box where we type 'P' or 'A' is as follows:

<input size="3" name="status[]" type="text" onkeypress="return isNumberKey(event)" maxlength ="1" tabindex="1" 
                                           style="text-align: center;" value='A' />
...
...
...
<input size="3" name="status[]" type="text" onkeypress="return isNumberKey(event)" maxlength ="1" tabindex="2" 
                                           style="text-align: center;" value='A' />

By default the value is 'A', I tried to enter the value 'P' using python selenium and the code I tried was:

driver.find_element_by_css_selector("input[tabindex='1']").send_keys('P')

I also tried

driver.find_element_by_xpath("//input[@tabindex='1']").send_keys('P')

But it is not changing the text to 'P', but the cursor is going to that text box. What might be the problem? Is it because of the 'onkeypress' event? Kindly help me with this, I am new to python selenium.

The code for isNumberKey(event) is as follows:

function isNumberKey(evt)
{
    var charCode = (evt.which) ? evt.which : event.keyCode

    if(charCode != 8){
        if(charCode != 112)
        {
            if(charCode != 80)
            {
                if(charCode != 97)
                {
                    if(charCode != 65)
                    {
                        return false;
                    }
                }
            }
        } 
    }
    return true;                    
}

You need to clean up the field first since there is a default A value there:

for elm in driver.find_elements_by_css_selector("input[name^=status]"):
    elm.clear()
    elm.send_keys('P')

Also, a quick and dirty solution could be to remove the onkeypress attribute :

for elm in driver.find_elements_by_css_selector("input[name^=status]"):
    driver.execute_script("arguments[0].removeAttribute('onkeypress');", elm)
    elm.send_keys('P')

First of all, your selector will work only for one student (the first) not for a list of students. Should be something like:

driver.find_elements_by_css_selector("input[name^='status']")

As for your problem, you might want to check what that javascript isNumberKey(event) is doing, it might be that it returns false when typing A or P

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