Mechanical soup - form filling, working with a tricky HTML table - python

I am trying to use mechanical soup to automatically fill and submit a time sheet for me.
This is what the form looks like:
timesheet_form
This is the relevant source code for that section of the page:
<FORM ACTION="bwpkteci.P_UpdateTimeInOut" METHOD="post">
<INPUT TYPE="hidden" NAME="JobsSeqNo" VALUE="208138">
<INPUT TYPE="hidden" NAME="LastDate" VALUE="0">
<INPUT TYPE="hidden" NAME="par_restart" VALUE="Y">
<INPUT TYPE="hidden" NAME="par_update" VALUE="Y">
<INPUT TYPE="hidden" NAME="par_submit" VALUE="Y">
<INPUT TYPE="hidden" NAME="par_recall" VALUE="N">
<INPUT TYPE="hidden" NAME="EarnCode" VALUE="RSA">
<INPUT TYPE="hidden" NAME="DateSelected" VALUE="20-FEB-2018">
<TABLE CLASS="dataentrytable" SUMMARY="This user enters the time of day for the hours worked into this table in order for the system to calculate the hours.">
<TR>
<TD CLASS="delabel" scope="row" >Date:</TD>
<TD CLASS="dedefault">Tuesday, Feb 20,2018</TD>
</TR>
<TR>
<TD CLASS="delabel" scope="row" >Earnings Code:</TD>
<TD CLASS="dedefault">Regular Student Aide</TD>
</TR>
</TABLE>
<TABLE CLASS="dataentrytable" SUMMARY="This is the detail table where the user enters the time of the day for the hours worked in order for the system to calculate the hours.">
<TR>
<TD CLASS="deheader" scope="col" ><LABEL for=shift_input_id><SPAN class="fieldlabeltext">Shift</SPAN></LABEL></TD>
<TD COLSPAN="2" CLASS="deheader" scope="col" ><LABEL for=timein_input_id><SPAN class="fieldlabeltext">Time In</SPAN></LABEL></TD>
<TD COLSPAN="2" CLASS="deheader" scope="col" ><LABEL for=timeout_input_id><SPAN class="fieldlabeltext">Time Out</SPAN></LABEL></TD>
<TD CLASS="deheader" scope="col" >Total Hours</TD>
</TR>
<INPUT TYPE="hidden" NAME="LineNumber" VALUE="1">
<TR>
<TD CLASS="dedefault"><INPUT TYPE="text" NAME="Shift" SIZE="2" MAXLENGTH="1" VALUE="1" ID="shift_input_id"></TD>
<TD CLASS="dedefault"><INPUT TYPE="text" NAME="TimeIn" SIZE="6" MAXLENGTH="5" ID="timein_input_id"></TD>
<TD CLASS="dedefault">
<SELECT NAME="TimeInAm" SIZE="1">
<OPTION VALUE="AM" SELECTED>AM
<OPTION VALUE="PM">PM
</SELECT>
</TD>
<TD CLASS="dedefault"><INPUT TYPE="text" NAME="TS_TimeOut" SIZE="6" MAXLENGTH="5" ID="timeout_input_id"></TD>
<TD CLASS="dedefault">
<SELECT NAME="TimeOutAm" SIZE="1">
<OPTION VALUE="AM" SELECTED>AM
<OPTION VALUE="PM">PM
</SELECT>
</TD>
<TD CLASS="dedefault"><p class="rightaligntext">0</p></TD>
</TR>
<TR>
<TD COLSPAN="5" CLASS="dedead"> </TD>
<TD CLASS="dedefault"><p class="rightaligntext">0</p></TD>
</TR>
</TABLE>
<P>
<TABLE CLASS="plaintable" SUMMARY="This layout table is used to align buttons.">
<TR>
<TD CLASS="pldefault">
<INPUT TYPE="submit" NAME="ButtonSelected" VALUE="Time Sheet">
<INPUT TYPE="submit" NAME="ButtonSelected" VALUE="Previous Day">
<INPUT TYPE="submit" NAME="ButtonSelected" VALUE="Next Day">
</TD>
</TR>
<TR>
<TD CLASS="pldefault">
<INPUT TYPE="submit" NAME="ButtonSelected" VALUE="Add New Line">
<INPUT TYPE="submit" NAME="ButtonSelected" VALUE="Save">
<INPUT TYPE="submit" NAME="ButtonSelected" VALUE="Copy">
<INPUT TYPE="submit" NAME="ButtonSelected" VALUE="Delete">
</TD>
</TR>
</TABLE>
</FORM>
I am using mechanical soup to fill the form out and submit it.
#navigate to the timesheet page
browser.open(timesheetURL)
browser.get_current_page()
#select the form
browser.select_form('form[action="bwpkteci.P_UpdateTimeInOut"]')
form = browser.get_current_form()
#add required controls and set values for testing
form.new_control('text', 'shift', '1')
form.new_control('text', 'TimeIn', '08:30')
form.new_control('select', 'TimeInAm', 1) #1 stands for PM
form.new_control('text', 'TS_TimeOut', '09:30')
form.new_control('select', 'TimeOutAm', 1) #1 stands for PM
form.new_control('submit', 'ButtonSelected', 'Save')
form.choose_submit('ButtonSelected')
browser.launch_browser()
browser.submit_selected()
browser.launch_browser()
I had to add new controls using new_control function because the TimeIn, TimeOut boxes did not show up when I checked by launching a browser.
This code does not work and I cannot figure out why. I thought maybe I was messing up the names or types of the controls that I was adding (maybe it didn't match with the actual input types and names of the form - I checked with a chrome extension and that too is not the case) as the save button would be clicked but the test values did not really register.
check
This is what the browser looks like before the save button is clicked:
before
after
It doesn't register!

Using MechancialSoup version 0.10.0, I was able to correctly parse your HTML snippet (as best I can tell). I don't see anything specifically that would cause this to fail for older version of MechanicalSoup, but perhaps try updating if nothing else works.
I used the following simple code:
import mechanicalsoup
browser = mechanicalsoup.StatefulBrowser()
browser.open_fake_page(text) #Here 'text' is the HTML snippet
form = browser.select_form('form[action="bwpkteci.P_UpdateTimeInOut"]')
form.print_summary()
browser.launch_browser()
The print_summary() method outputs all the form elements, which include the TimeIn and TimeOut boxes that were missing for you:
<input name="JobsSeqNo" type="hidden" value="208138"/>
<input name="LastDate" type="hidden" value="0"/>
<input name="par_restart" type="hidden" value="Y"/>
<input name="par_update" type="hidden" value="Y"/>
<input name="par_submit" type="hidden" value="Y"/>
<input name="par_recall" type="hidden" value="N"/>
<input name="EarnCode" type="hidden" value="RSA"/>
<input name="DateSelected" type="hidden" value="20-FEB-2018"/>
<input name="LineNumber" type="hidden" value="1"/>
<input id="shift_input_id" maxlength="1" name="Shift" size="2" type="text" value="1"/>
<input id="timein_input_id" maxlength="5" name="TimeIn" size="6" type="text"/>
<select name="TimeInAm" size="1">
<option selected="" value="AM">AM</option><option value="PM">PM</option></select>
<input id="timeout_input_id" maxlength="5" name="TS_TimeOut" size="6" type="text"/>
<select name="TimeOutAm" size="1">
<option selected="" value="AM">AM</option><option value="PM">PM</option></select>
<input name="ButtonSelected" type="submit" value="Time Sheet"/>
<input name="ButtonSelected" type="submit" value="Previous Day"/>
<input name="ButtonSelected" type="submit" value="Next Day"/>
<input name="ButtonSelected" type="submit" value="Add New Line"/>
<input name="ButtonSelected" type="submit" value="Save"/>
<input name="ButtonSelected" type="submit" value="Copy"/>
<input name="ButtonSelected" type="submit" value="Delete"/>
The elements missing for you were also displayed correctly when I use launch_browser():

Related

I am unable to get round toggle in my django template

I have a table which contains for loop and if tag using jinja this table shows some data but in last column I want round toggle button but I only get a checkbox, I am unable to find the error please help me.
<tbody>
{%for student in students%}
{%if user.staff.class_coordinator_of == student.division and user.staff.teacher_of_year == student.year%}
<tr>
<td style="color:white;">{{student.user.first_name}}</td>
<td style="color:white;">{{student.user.last_name}}</td>
<td style="color:white;">{{student.year}}</td>
<td style="color:white;">{{student.division}}</td>
<td style="color:white;">{{student.batch}}</td>
<td>
<label class="switch ">
<input type="checkbox" id="" value="" checked>
<span class="slider round"></span>
</label>
</td>
</tr>
{% endif %}
{%endfor%}
</tbody>
OUTPUT
Output Image
You should use radio button instead of checkbox so, it should be type="radio".
<input type="radio" id="" value="" checked>

How to handle filter box that appears while using selenium python?

I go to a web page in chrome using selenium python. After going to a page ,I click on an item called "search". As I click on it a filterbox appears as below
What should I do to click on restrict by date and fill values inside from and to box and submit using selenium python? Sorry I am new to selenium, I cant find any examples that satisfy my requirements.This filter box doesnt have a URL associated with it.
Here is the back end source code I found:
<div class="hd" id="alert_search_dialog_h" style="cursor: move;">
Search Criteria
</div>
<form action="/portal/alerts" id="alert_search_form" method="get" onsubmit="new Ajax.Request('/portal/alerts', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;"> <input id="search_startTimeXml" name="search[startTimeXml]" type="hidden">
<input id="search_endTimeXml" name="search[endTimeXml]" type="hidden">
<input id="search_limit" name="search[limit]" type="hidden" value="50">
<input id="search_offset" name="search[offset]" type="hidden" value="0">
<fieldset><legend><input class="restrict_checkboxes" id="include_id" name="include_id" onclick="javascript: admin.alerts.SearchDialog.disableByCheckbox(this, "id_field");" type="checkbox" value="1">Restrict by ID</legend>
<div id="id_field" class="disabled">
<label for="alert_id">Alarm ID</label>
<input id="search_alert_id" name="search[alert_id]" type="text" disabled="">
</div>
</fieldset>
<fieldset><legend><input class="restrict_dropdown" id="include_route" name="include_route" onclick="javascript: admin.alerts.SearchDialog.disableByCheckbox(this, "type_drop_down_list");" type="checkbox" value="1">Restrict by route</legend>
<input id="search_route_min_distance" name="search[route_min_distance]" type="hidden">
<input id="search_route_max_distance" name="search[route_max_distance]" type="hidden">
<input id="search_route_min_velocity" name="search[route_min_velocity]" type="hidden">
<input id="search_route_max_velocity" name="search[route_max_velocity]" type="hidden">
<div id="type_drop_down_list" class="dropdown_list disabled">
<table>
<tbody><tr>
<th>Route ID</th>
<td>
<select id="search_route_id" name="search[route_id]" onchange="change_route_directions(this.value)"><option value="">--All--</option>
</select>
</td>
</tr>
<tr>
<th>Route direction</th>
<td>
<select id="search_route_direction" name="search[route_direction]"><option value="">--All--</option>
</select>
</td>
</tr>
<tr>
<th>Distance(<span>m</span>)</th>
<td>
min
<input id="search_route_min_distance_" name="search[route_min_distance_]" size="8" type="text" disabled="">
max
<input id="search_route_max_distance_" name="search[route_max_distance_]" size="8" type="text" disabled="">
</td>
</tr>
<tr>
<th>
Velocity (m/s):
</th>
<td>
min
<input id="search_route_min_velocity_" name="search[route_min_velocity_]" size="8" type="text" disabled="">
max
<input id="search_route_max_velocity_" name="search[route_max_velocity_]" size="8" type="text" disabled="">
</td>
</tr>
</tbody></table>
</div>
</fieldset>
<fieldset><legend><input class="restrict_checkboxes" id="include_type" name="include_type" onclick="javascript: admin.alerts.SearchDialog.disableByCheckbox(this, "type_check_box_list");" type="checkbox" value="1">Restrict by alarm type</legend>
<div id="type_check_box_list" class="checkbox_list disabled" style="max-height:100px;-webkit-columns: 100px 2;">
<input id="search_name_" name="search[name][]" type="checkbox" value="digging_alert" disabled="">
Digging<br>
<input id="search_name_" name="search[name][]" type="checkbox" value="fibre_break_alert" disabled="">
Fibre Break<br>
<input id="search_name_" name="search[name][]" type="checkbox" value="helios" disabled="">
Helios Unit Failure<br>
<input id="search_name_" name="search[name][]" type="checkbox" value="mech_digging_alert" disabled="">
Mechanized Digging<br>
<input id="search_name_" name="search[name][]" type="checkbox" value="panoptes" disabled="">
Panoptes Failure<br>
<input id="search_name_" name="search[name][]" type="checkbox" value="unknown_alert" disabled="">
Unknown<br>
<input id="search_name_" name="search[name][]" type="checkbox" value="vehicle_alert" disabled="">
Vehicle<br>
<input id="search_name_" name="search[name][]" type="checkbox" value="walk_alert" disabled="">
Walking<br>
</div>
</fieldset>
<fieldset><legend><input class="restrict_checkboxes" id="include_threat" name="include_threat" onclick="javascript: admin.alerts.SearchDialog.disableByCheckbox(this, "threat_check_box_list");" type="checkbox" value="1">Restrict by threat level</legend>
<div id="threat_check_box_list" class="checkbox_list disabled" style="-webkit-columns: 100px 2;">
<input id="search_threat_level_" name="search[threat_level][]" type="checkbox" value="red" disabled="">
Red<br>
<input id="search_threat_level_" name="search[threat_level][]" type="checkbox" value="amber" disabled="">
Amber<br>
<input id="search_threat_level_" name="search[threat_level][]" type="checkbox" value="green" disabled="">
Green<br>
<input id="search_threat_level_" name="search[threat_level][]" type="checkbox" value="clear" disabled="">
Clear<br>
</div>
</fieldset>
<fieldset><legend><input class="restrict_checkboxes" id="include_status" name="include_status" onclick="javascript: admin.alerts.SearchDialog.disableByCheckbox(this, "status_check_box_list");" type="checkbox" value="1">Restrict by alarm status</legend>
<div id="status_check_box_list" class="checkbox_list disabled" style="-webkit-columns: 100px 3;">
<input id="search_status_" name="search[status][]" type="checkbox" value="acknowledged" disabled="">
Acknowledged<br>
<input id="search_status_" name="search[status][]" type="checkbox" value="new" disabled="">
New<br>
<input id="search_status_" name="search[status][]" type="checkbox" value="resolved" disabled="">
Resolved<br>
</div>
</fieldset>
<fieldset><legend><input class="restrict_checkboxes" id="include_date" name="include_date" onclick="javascript: admin.alerts.SearchDialog.disableByCheckbox(this, "date_table");" type="checkbox" value="1">Restrict by date</legend>
<table id="date_table" class="disabled">
<tbody><tr>
<td><label for="start_time">From</label></td>
<td> <input type="text" name="start_time" id="start_timeId" size="12" value="" disabled="">
<img alt="Img" id="start_timeImg" onmouseout="this.style.background='';" onmouseover="this.style.background='red';" src="/javascripts/fotech/common_gui/jscalendar/img.gif?1570706592" style="cursor: pointer;" title="Select min date/time">
<script type="text/javascript">
_fotechSetupCalendar('start_time',{});
</script>
</td>
<td><label for="end_time">To</label></td>
<td> <input type="text" name="end_time" id="end_timeId" size="12" value="" disabled="">
<img alt="Img" id="end_timeImg" onmouseout="this.style.background='';" onmouseover="this.style.background='red';" src="/javascripts/fotech/common_gui/jscalendar/img.gif?1570706592" style="cursor: pointer;" title="Select max date/time">
<script type="text/javascript">
_fotechSetupCalendar('end_time',{});
</script>
</td>
</tr>
</tbody></table>
</fieldset>
<table>
<tbody><tr>
<th><label for="search_order_by">Order by</label></th>
<td>
<select id="search_order_by" name="search[order_by]"><option value="time">Time</option>
<option value="name">Alarm type</option>
<option value="status">Status</option></select>
<select id="search_order_dir" name="search[order_dir]"><option value="asc">Ascending</option>
<option value="desc">Descending</option></select>
</td>
</tr>
<tr>
<td></td>
<td>
<input id="search_include_suppressed" name="search[include_suppressed]" type="checkbox" value="1">
<label for="search_include_suppressed">Include suppressed</label>
</td>
</tr>
</tbody></table>
</form>
What you need is to work first with Chrome to understand the site structure and understand what you want to click and how, and only then to write the code that will run with selenium.
Chrome DevTools is a set of web developer tools built directly into the Google Chrome
When you want to work with the DOM or CSS, right-click an element on the page and select Inspect to jump into the Elements panel. Or press Command+Option+C (Mac) or Control+Shift+C (Windows, Linux, Chrome OS).
Chrome DevTools

python selenium excpected conditions send_keys

i'm having a problem with the following code:
iFrame = EC.frame_to_be_available_and_switch_to_it(("MAIN_IFRAME"))
uscita = EC.presence_of_element_located((By.XPATH, "//input[contains(.,'password')]"))
uscita.send_keys('passwd')
and i'm getting the following error:
AttributeError: 'presence_of_element_located' object has no attribute 'send_keys'
i'm new Python's user and I would like your help on this problem.
Thanks
HTML for iframe and input:
<td style="text-align:center">
<iframe height="350" width="450" name="timb" src="timb.php" style="position: relative;top:0px"></iframe>
</td>
<td>
<div style="position: relative;top:0px">
<form action="mnghlog6.php" method="post" target="timbri">
<input type="hidden" id="esculappio" name="escu" value="0">
<table style="position: relative;top:0px">
</div></td><td><div class="buttons" style="display:inline;text-align: left;">
</div></td></tr><tr><td><div class="buttons" style="display:inline;text-align: left;">
</div></td><td><div class="buttons" style="display:inline;text-align: left;">
</div></td></tr></tbody></table> </div>
</td>
</tr>
<tr>
<td style="text-align:center">Password <input type="password" name="password" id="password" size="30" value=""></td>
</tr>
</tbody></table>
<input type="hidden" name="tipo" value="">
<input type="hidden" name="flag_inizio">
<input type="hidden" name="durata">
</form>
</div>
</td>
</tr>
You need to use until function from WebDriverWait with the expected_conditions. It also doesn't looks like the field is in iframe. Try this
wait = WebDriverWait(driver, 10);
uscita = wait.until(EC.presence_of_element_located((By.ID, "password")))
uscita.send_keys('passwd')
By the way, to switch to the frame you can do something like
iFrame = wait.until(EC.frame_to_be_available_and_switch_to_it((By.NAME, "timb")))

How to select from drop down box using selenium webdriver

I am trying to select Channel in a webpage from Drop Down Box using selenimum webdriver (Python). HTML is given below:
<iframe id="page" frameborder="0" src="DashBoard.htm" onfocus="this.blur()" scrolling="no" cellspacing="0" border="0" name="page" style="width: 1076px; height: 525px; margin-left: 30px; margin-top: 0px; visibility: visible;">
<!DOCTYPE html>
<html>
<head>
<body class="page-body" onresize="change_size();" onload="change_size();initScrn();setKeys();showWpsAlert();highLightMenu('setup_header', 'wireless_set');">
<img class="cover-image" src="img/cover-image_noh.gif" style="display: none;">
<img class="body-image" src="img/subhead2-background_noh.jpg" style="width: 1076px; height: 495px; position: absolute; top: 5px;">
<div id="full-page-container">
<form id="target" action="wireless.cgi?id=1484693214" method="POST">
<input type="hidden" name="buttonHit">
<input type="hidden" name="buttonValue">
<img class="subtop-image" src="img/subhead2-top_noh.gif" style="width: 1076px; height: 32px;">
<div class="subhead2"> Wireless Setup</div>
<table class="subhead2-table" border="0" style="height: 405px; position: relative; top: -3px; width: 1049px;">
<tbody>
<tr valign="middle" align="left">
<tr>
<td class="scrollpane-table-seperate-border" colspan="2">
<div class="scroll-pane" style="height: 405px; width: 1049px; overflow: auto;">
<table style="border-collapse:collapse;width:97%">
<tbody>
<tr>
<tr>
<tr>
<tr>
<td colspan="2">
<div id="setting_2G" style="display: block;">
<table cellspacing="0" cellpadding="0" border="0" width="100%" <table="">
<tbody>
<tr>
<tr>
<tr>
<tr>
<tr>
<td nowrap="">Channel:</td>
<td nowrap="">
<select size="1" name="w_channel">
</td>
</tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
</tbody>
</table>
</div>
</td>
</tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
</tbody>
</table>
</div>
</td>
</tr>
<tr valign="middle" align="center">
</tbody>
</table>
<img class="subfooter-image" src="img/subhead2-bottom.gif" style="width: 1076px; height: 24px; position: relative; top: -3px;">
<div class="subhead2-bottom" style="width: 1076px;">
<input type="hidden" value="0" name="tempSetting">
<input type="hidden" value="5" name="tempRegion">
<input type="hidden" value="17" name="setRegion">
<input type="hidden" value="0" name="wds_enable">
<input type="hidden" value="0" name="wds_enable_an">
<input type="hidden" value="0" name="only_mode">
<input type="hidden" value="0" name="show_wps_alert">
<input type="hidden" value="WPA-AUTO-PSK" name="security_type_2G">
<input type="hidden" value="WPA-AUTO-PSK" name="security_type_5G">
<input type="hidden" value="WPA-AUTO-PSK" name="init_security_type_2G">
<input type="hidden" value="WPA-AUTO-PSK" name="init_security_type_5G">
<input type="hidden" value="11" name="initChannel">
<input type="hidden" value="automatic" name="initAuthType">
<input type="hidden" value="0" name="initDefaultKey">
<input type="hidden" value="161" name="initChannel_an">
<input type="hidden" value="automatic" name="initAuthType_an">
<input type="hidden" value="0" name="initDefaultKey_an">
<input type="hidden" value="1" name="telec_dfs_ch_enable">
<input type="hidden" value="0" name="ce_dfs_ch_enable">
<input type="hidden" value="0" name="fcc_dfs_ch_enable">
<input type="hidden" value="1" name="auto_channel_5G">
<input type="hidden" value="1" name="support_ac_mode">
<input type="hidden" value="U12H270T00_NETGEAR" name="board_id">
<input type="hidden" value="0" name="enable_band_steering">
<input type="hidden" value="SKU_WW" name="fw_sku">
<input type="hidden" value="0.0.0.0" name="wla_radius_ipaddr">
<input type="hidden" value="0.0.0.0" name="wlg_radius_ipaddr">
<input type="hidden" value="WPA-AUTO" name="wla_ent_secu_type">
<input type="hidden" value="WPA-AUTO" name="wlg_ent_secu_type">
<input type="hidden" value="192.168.0.100" name="wan_ipaddr">
<input type="hidden" value="255.255.255.0" name="wan_netmask">
<a name="helpframe-anchor"></a>
</form>
</div>
<meta content="R7000" name="description">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta content="text/css" http-equiv="Content-Style-Type">
<meta content="no-cache" http-equiv="Pragma">
<meta content="no-cache" http-equiv="Cache-Control">
<meta content="Mon, 06 Jan 1990 00:00:01 GMT" http-equiv="Expires">
</body>
</html>
</iframe>
I have following python code:
driver.find_element_by_css_selector("#WLS_menu > span").click()
Select(driver.find_element_by_name("w_channel")).select_by_visible_text("12")
driver.find_element_by_name("Apply").click()
I am getting following error:
File "netgear", line 23, in test_netge Select(driver.find_element_by_name("w_channel")).select_by_visible_text("12")
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: Unable to locate element: {"method":"name","selector":"w_channel"}
Am I missing something in my code?
yes please try with xpath
driver.find_element_by_xpath("//*[#id='setting_2G']/table/tbody/tr[5]/td[2]/select").send_keys("12")
UPDATE
so in that case you have to switch to frame then (Please note i am a java guy so below answer is in java for python update it into python)
WebDriver driver = new FirefoxDriver();
driver.get("â€ĒC:\\Users\\rajnish\\Desktop\\mytest.html");
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.switchTo().frame("page");
driver.findElement(By.xpath("//*[#id='setting_2G']/table/tbody/tr[5]/td[2]/select")).sendKeys("12");

Python mechanize to submit form data

My form HTML source is below, I am trying to "check one of the checkboxes" and hit the "update" or submit button using mechanize. How would I go about this? Does the variable for linear_entitlements make it not possible?
<form accept-charset="UTF-8" action="/admin/users/3548003/user_linear_entitlements" class="form with-border" id="edit_user_3548003" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓"><input name="_method" type="hidden" value="put"><input name="authenticity_token" type="hidden" value="samplevalue"></div>
<table class="table table-striped">
<thead>
<tr>
<th>Name</th>
<th>GUID</th>
<th>CMS Subpack ID</th>
<th>Last Updated</th>
</tr>
</thead>
<tbody>
<tr id="linear_entitlement_1">
<td>
<div class="control-group">
<label class="checkbox">
<span>SUN Pack</span>
<input class="checkbox" id="user_linear_entitlement_ids_" name="user[linear_entitlement_ids][]" type="checkbox" value="1">
</label>
</div>
</td>
<td> 2a59739c-13ed-11e2-a36b-12313d298802 </td>
<td> 1 </td>
<td> 2014-02-12 21:32:56 UTC <div style="float:right">→</div></td>
</tr>
<tr id="linear_entitlement_7">
<td>
<div class="control-group">
<label class="checkbox">
<span>Tamil Pack - Legacy</span>
<input class="checkbox" id="user_linear_entitlement_ids_" name="user[linear_entitlement_ids][]" type="checkbox" value="7">
</label>
</div>
</td>
<td> 2ab298dc-13ed-11e2-a36b-12313d298802 </td>
<td> 3 </td>
<td> 2015-04-01 23:11:33 UTC <div style="float:right">→</div></td>
</tr>
</tbody>
</table>
<div class="form-actions">
<input class="btn primary input_submit" name="commit" type="submit" value="Update"> <button type="reset" class="btn">Cancel</button>
</div>
</form>
So far I have this, which selects the form:
htmlResponse2 = browser.open(URL + 'admin/users/' + old_user_url + '/edit')
browser.select_form(nr=0)
I don't know if you need to select the form, but if you did so, following should just do the trick:
br.find_control(type="checkbox").items[0].selected=True
if you want to select all checkboxes:
for i in range(0, len(br.find_control(type="checkbox").items)):
br.find_control(type="checkbox").items[i].selected =True
then submit
br.submit()

Categories