2

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;                    
}

2 Answers 2

8

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')
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the reply. I ran the code, but the value 'P' is not getting entered in the text box. The text box is only accepting 'P', 'A', 'p', and 'a' from the keyboard. Is there a way to make it like that?
@user2825570 could you please share the link to the page where this is happening? Thanks.
Thanks. I have uploaded the html file using tinyupload[dot]com. It can be downloaded from here the file name is att[dot]html. The actual website can be accessed only through our university LAN.
Thank you very much for the help. You are great. Thanks a lot. I was trying this for the past three hours.
1

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

3 Comments

Thanks for the reply. But the problem is I am unable to give the value 'P' to the text box, by using send_keys('P'). Is there any other way to pass the value. I have also edited the question by adding the isNumberKey() function.
The JS looks fine, try changing your query as suggested (and put it in a foreach loop or pick the first element from the list ) since there might be other input elements in the page with tabindex=1
I searched the source code, there was only one tabindex=1, and my code was talking the cursor to the exact text box, but the value 'P' is not being entered in the text box. The text box is only talking when the value 'P' is pressed from the keyboard. Is there any way to give the value 'P' using selenium, as if it was given from keyboard?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.