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
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():
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")))
I'm relatively new to python and programming generally. I've tried to follow steps provided from similar questions here but My program is unable to successfully Log in. The latest code I got from How to scrape a website which requires login using python and beautifulsoup?
Below is my attempted code: and the response I get
import mechanize
import BeautifulSoup
import urllib2
import cookielib
cj = cookielib.CookieJar()
br = mechanize.Browser()
br.set_cookiejar(cj)
br.open("http://www.bbnplace.com/accounts_v2/?do=signin&service=prepaidsms&returnto=http%3A%2F%2Fsms.bbnplace.com%2Fsentdir.php")
br.select_form('blogin')
br.form['busername'] = 'my_username'
br.form['passwd'] = 'my_password'
response = br.submit()
print br.response().read()
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="keywords" content="bulksms, bulk sms, bulk sms gateway, cheap bulk sms, bulk sms provider, bulk sms to nigeria, send bulk sms, personalized bulk sms, bulk sms nigeria, best sms site in nigeria, nigerian bulks sms gateway, web to mobile sms" />
<meta http-equiv="description" content="Send bulk sms with personalized sender name to all GSM networks in Nigeria and over 800 networks in 160 countries. Nigeria's Best SMS Gateway." />
<title>BBN SMS Messenger: Retail Web to Mobile Bulk SMS Messaging Utility</title>
<link rel="stylesheet" href="http://www.bbnplace.com/accounts_v2/style/layout.css" type="text/css" media="screen" />
<script type="text/javascript" async src="http://www.bbnplace.com/accounts_v2/bbjs/un.js"></script>
<script type="text/javascript">var L;var C;var bw=0;var G;var eD;var am;var eE=0;var method;var url;var J;var be;var fp='';var eY='';var dW=0;var eH='dG';var eG;var action='sent';var bS=0;var eC='';var eW=new Number();var bP='';var ft='save';var host=document.domain=='localhost'?'localhost/smsmessenger/':'sms.bbnplace.com';var protocol='http';var fc;var eA=protocol+"://"+host+"/source/";var dR=0;var responseText;var response=new Array();var bs;var bV=new Number();var H;var request;var fl;var aB;var aQ;var aL=window.innerWidth;var ab=window.innerHeight;var ca=new Array();var aK=new Array();var cv=new Array();var bb=new Array();var dN=new Array();aB=document.getElementById('aD');var et=110;aQ=document.getElementById('bx');if(request=='gocheck'){ad();}function ak(v){var k=document.getElementById(v);k.style.visibility='hidden';k.style.height='0px';};function aE(v){var k=document.getElementById(v);k.style.visibility='inherit';k.style.height='auto';};function bJ(v,ba){var ids=new Array();var aw=new String();aw=ba;ids=aw.split(',');for(var i=0;i<ids.length;i++){ak(ids[i]);}ids='';aE(v);return;};function ay(bF,cF,dw,bH,bi,aq,bN,aA){be=bi;C=bH;G=aq;H=aA;J=dw;method=bF;url=cF;am=bN;L=bO();if(L){if(method=='post'){try{L.open(method,url,true);L.onreadystatechange=bg;L.setRequestHeader('Content-Type','application/x-www-form-urlencoded');L.setRequestHeader('Content-Length',J.length);L.setRequestHeader('Connection','close');L.send(J);}catch(e){alert("Error connecting to server: "+e.toString());}}else{try{J.length>1?aG=url+'?'+J:aG=url;L.open(method,aG,true);L.onreadystatechange=bg;L.send(null);}catch(e){alert("Could not connect to server: "+e.toString());}}}};function bg(){var d;if(C.length>0){d=document.getElementById(C);}if(L.readyState==4){if(L.status==200){try{var response=dO();if(C.length>0){d.style.visibility='hidden';}bw=0;}catch(e){d.innerHTML="Error reading server response: "+e.toString();}}else{if(C.length>0){if(L.status){d.innerHTML='Server Response: '+L.status+' - '+L.statusText;d.style.visibility='visible';}else{d.innerHTML='Connection to server failed. Retrying...';d.style.visibility='visible';bQ=setTimeout('bz()',5000);}}}}else{if(C.length>0){d.innerHTML='<img border="0" src="style/29.gif" align="absmiddle" /> <b>Loading...</b>';d.style.visibility='visible';ap=setTimeout('dK()',(be*1000));}}return response;};function dK(){if(L.readyState!=4){clearTimeout(ap);document.getElementById(C).innerHTML='Connection is too slow. Retrying...';bQ=setTimeout('bD()',5000);}};function bD(){clearTimeout(bQ);if(L.readyState!=4){L.abort();ay(method,url,J,C,be,G,H);}};function bz(){clearTimeout(bQ);if(!L.status){L.abort();ay(method,url,J,C,be,G,H);}};function df(){x=document.getElementById(C);x.style.visibility='hidden';};function dO(){var responseText,cp;var doctype=L.getResponseHeader('Content-Type').toString();var l=document.getElementById(G);if(H.length){var aJ=document.getElementById(H);}l.innerHTML='';response['type']=doctype;if(doctype=='text/plain'||doctype=='text/html'){responseText=L.responseText;if(responseText.substr(0,4)=='Err:'){l.innerHTML=responseText.substr(5);l.style.visibility='inherit';l.style.height='auto';if(responseText.substr(5)==''){if(bS>0){l.innerHTML='<span style="color:red;">'+bP+'</span>';}}}else if(responseText.substr(0,3)=='OK:'){var aH=responseText.substr(4);switch(request){case 'newpost':document.forms[request].es.value=aH;bs.innerHTML='Saved';eu();break;}}else{if(H.length){aJ.innerHTML=responseText;}else if(request=='gocheck'){l.innerHTML=responseText;y=document.forms.di;if(responseText=='available'){y.cf.value=1;y.ds.value=y.username.value;l.innerHTML='Available';l.style.fontWeight='bold';l.style.color='green';ad();}}else{if(responseText=='successful'){switch(request){case 'newRequest':bJ('h','h,j');break;}}else if(responseText=='denied'){bf=protocol+'://'+host+'/accounts';window.location=bf;}else{alert(responseText);}}}}};function bO(){try{L=new XMLHttpRequest();}catch(e){var aI=new Array('MSXML2.XMLHTTP.6.0','MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','Microsoft.XMLHTTP');for(var i=0;i<aI.length&& !L;i++){try{L=new ActiveXObject(aI[i]);}catch(e){}}}if(!L){alert('Please Upgrade your web browser');}else{return L;}};function aN(){var x=document.forms['t'];request='newRequest';aq='V';var bp,ao,aW,az;bp=x.ag.value;ao=x.bI.value;aW=x.aR.value;dr=x.an.value;az=x.av.value;aZ='eml='+encodeURIComponent(ao)+'&f='+encodeURIComponent(bp)+'&p='+encodeURIComponent(aW)+'&m='+encodeURIComponent(az)+'&p2='+encodeURIComponent(dr);al=document.getElementById('V');al.style.visibility='hidden';al.style.height=0;dP='source/contact.php';ay('post',dP,aZ,'',30,'V','','');};function cu(){dJ();};function cl(){ce();};function bB(aX){var x=document.getElementById('bh');if(!x){var x=document.createElement('div');x.setAttribute('id','bh');x.style.backgroundColor='#000';x.style.top='0px';x.style.left='0px';x.style.position='fixed';x.style.zIndex=99999;x.style.opacity=0.5;}x.style.width=aL+'px';x.style.height=ab+'px';x.style.visibility='visible';document.body.appendChild(x);if(aX==null){var T=document.getElementById('t');T.style.visibility='visible';T.style.height='auto';T.style.position='fixed';T.style.left=(aL/2-250)+'px';T.style.width='500px';T.style.zIndex=10000001;bJ('j','h,j');}else{var T=document.getElementById('ax');T.style.visibility='visible';bV=aX;}};function ar(){var bE=document.getElementById('bh');bE.style.width='0px';bE.style.height='0px';bE.style.visibility='hidden';if(bV==0){ak('h');ak('j');ak('t');document.forms['t'].reset();}else{var T=document.getElementById('ax');T.style.visibility='hidden';bV=0;}};function cU(i){var aY=document.getElementById('bA');aY.innerHTML='<b>Message:</b><br />'+aK[i];aY.innerHTML+='<br /><br /><b>Broadcasted:</b> '+bb[i];document.getElementById('as').style.visibility='visible';ez(ca[i]);};function dA(){document.getElementById('as').style.visibility='hidden';document.getElementById('ae').innerHTML='';}</script>
</head>
<body><div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=174240488183";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script><div id="ppbd"></div><div id="ppbdc"></div><div id="logoholder"></div>
<div id="app_header">
<div id="line1_left"><img src="http://www.bbnplace.com/accounts_v2/style/smlogo.png" align="absmiddle" title="BBN SMS Messenger: Retail Web to Mobile Bulk SMS Messaging Utility" /></div>
<div id="line1_right">Hi Buddie! | Messaging Solutions | BBN </div>
</div>
<div id="cK" align="right"> </div><div id="workarea" align="center">
<div id="frm_right">
<div id="note_panel"><div align="left">
<!-- <div style="margin: 0 0 40px; font-size: 22px; line-height: 27px;">
manage your contacts • send sms to <b style="color: red;">groups</b>
• from personal computer and <b style="color: red;">mobile</b>
</div> -->
<div style="font-size: 28px; line-height: 35px; margin: 35px 0 0;">
Send bulk sms • <b style="color: red;">confidently!</b>
</div>
<div id="bslogin_nav">
<img alt="How to Recharge" src="style/cc_icon.png" align="absmiddle" />
<b>Learn How to Recharge</b> <img
alt="Bundles and Pricing" align="absmiddle" src="style/cog_icon.png" />
<b><a
href="http://www.bbnplace.com/documentation/?service=prepaidsms&article=networks&returnto="
target="_blank">See our Network Coverage</a></b>
</div>
<script type="text/javascript">function dJ(){var x=document.forms['aN'];var V=new Number(x.df.value);var v=new Number(0);if(isNaN(V)){document.getElementById("J").innerHTML="<span style='color: red;'>waiting...</span>";document.getElementById("hBp").innerHTML="<span style='color: red;'>waiting...</span>";document.getElementById("T").innerHTML="<span style='color: red;'>waiting...</span>";if(document.forms.F){document.forms.F.bJ.disabled=true;}if(document.getElementById("statMsg")){document.getElementById("statMsg").innerHTML="<span style='color: red;'><strong>Please Enter a Numberic Value</strong></span>";}}if(V<50){document.getElementById("J").innerHTML="<span style='color: red;'>waiting...</span>";document.getElementById("hBp").innerHTML="<span style='color: red;'>waiting...</span>";document.getElementById("T").innerHTML="<span style='color: red;'>waiting...</span>";if(document.forms.F){document.forms.F.bJ.disabled=true;}if(document.getElementById("statMsg")){document.getElementById("statMsg").innerHTML="<span style='color: red;'><strong>Please specify a minimum of 50</strong></span>";}}else if(V>=50&&V< 1000){document.getElementById("J").innerHTML="Teams & Groups";document.getElementById("hBp").innerHTML="NGN 3.50";v=V* 3.50;v=v.toFixed(2);document.getElementById("T").innerHTML='NGN '+v;if(document.forms.F){document.forms.F.bJ.disabled=false;}if(document.getElementById("statMsg")){document.getElementById("statMsg").innerHTML='';}if(document.forms.F){document.forms.F.J.value='Teams & Groups';document.forms.F.hBp.value= 3.50;document.forms.F.T.value=v;}}else if(V>= 1000 &&V< 10000){document.getElementById("J").innerHTML="Business Standard";document.getElementById("hBp").innerHTML="NGN 2.20";v=V* 2.20;v=v.toFixed(2);document.getElementById("T").innerHTML='NGN '+v;if(document.forms.F){document.forms.F.bJ.disabled=false;}if(document.getElementById("statMsg")){document.getElementById("statMsg").innerHTML='';}if(document.forms.F){document.forms.F.J.value='Business Standard';document.forms.F.hBp.value= 2.20;document.forms.F.T.value=v;}}else if(V>= 10000 &&V< 50000){document.getElementById("J").innerHTML="Business Professional";document.getElementById("hBp").innerHTML="NGN 2.00";v=V* 2.00;v=v.toFixed(2);document.getElementById("T").innerHTML='NGN '+v;if(document.forms.F){document.forms.F.bJ.disabled=false;}if(document.getElementById("statMsg")){document.getElementById("statMsg").innerHTML='';}if(document.forms.F){document.forms.F.J.value='Business Professional';document.forms.F.hBp.value= 2.00;document.forms.F.T.value=v;}}else if(V>= 50000){document.getElementById("J").innerHTML="Business Premium";document.getElementById("hBp").innerHTML="NGN 1.85";v=V* 1.85;v=v.toFixed(2);document.getElementById("T").innerHTML='NGN '+v;if(document.forms.F){document.forms.F.bJ.disabled=false;}if(document.getElementById("statMsg")){document.getElementById("statMsg").innerHTML='';}if(document.forms.F){document.forms.F.J.value='Business Premium';document.forms.F.hBp.value= 1.85;document.forms.F.T.value=v;}}} </script>
<div align="left" id="pricelist">
<div align="right"><div id="price_tag"><img src="http://www.bbnplace.com/accounts_v2/style/price_tag.png" align="top" /> Bundles & Pricing</div></div>
<form id="aN" name="aN" method="post" action="">
<div style="margin: 0 0 5px;">
<label for="select"></label> <b>Currency</b> <select
name="aD" id="aD" onchange="ck(this.value)" disabled="disabled">
<option value="ngn" selected="selected" >NGN</option>
<option value="usd" >USD</option>
<option value="eur" >EUR</option>
</select>
</div>
<div>
<table cellpadding="3" cellspacing="0" width="100%" id="price_tags">
<tr>
<th align="left">Bundle</th>
<th width="120" align="right">Min. Volume</th>
<th width="80" align="right">Unit Price</th>
</tr> <tr>
<td style="background-color: #FFF; white-space:nowrap; overflow:hidden; text-overflow: clip;">Teams & Groups</td>
<td align="right" style="background-color: #FFF;">50</td>
<td align="right" style="background-color: #FFF;">3.50</td>
</tr> <tr>
<td style="background-color: #FAFAFA; white-space:nowrap; overflow:hidden; text-overflow: clip;">Business Standard</td>
<td align="right" style="background-color: #FAFAFA;">1,000</td>
<td align="right" style="background-color: #FAFAFA;">2.20</td>
</tr> <tr>
<td style="background-color: #FFF; white-space:nowrap; overflow:hidden; text-overflow: clip;">Business Professional</td>
<td align="right" style="background-color: #FFF;">10,000</td>
<td align="right" style="background-color: #FFF;">2.00</td>
</tr> <tr>
<td style="background-color: #FAFAFA; white-space:nowrap; overflow:hidden; text-overflow: clip;">Business Premium</td>
<td align="right" style="background-color: #FAFAFA;">50,000</td>
<td align="right" style="background-color: #FAFAFA;">1.85</td>
</tr> </table>
</div>
<div style="margin: 30px 0;">
Specify sms volume in the space below to get pricing <br />
<div>
<table width="100%" border="0" cellpadding="2" cellspacing="0"
id="aH">
<tr>
<th width="80">Volume</th>
<th width="">Bundle</th>
<th width="80" align="right">Unit Price</th>
<th width="" align="right">Price</th>
</tr>
<tr>
<td bgcolor="#FEFEFE"><input name="df" type="text" value=""
size="10" maxlength="10" onkeyup="dJ()" /></td>
<td bgcolor="#FEFEFE"><div id="J">
<input name="J" type="hidden" id="J" value="" />
</div></td>
<td align="right"><div id="hBp">
<input name="hBp" type="hidden" id="hBp" value="" />
</div></td>
<td align="right"><div id="T"><a name="how_to_recharge"></a>
<input name="T" type="hidden" id="T" value="" />
</div></td>
</tr>
</table>
</div>
</div>
</form>
IMPORTANT: *<strong>Business Premium bundle</strong> is the only <span
style="color: red; font-weight: bold;">negotiable</span> bundle.
</div><div align="left" style="margin: 50px 0 0;">
<h2>How to Recharge</h2>
<p>Pay online with any Nigerian debit/credit card, or at any branch of the listed banks:</p>
<div>
<img alt="web payment" src="http://www.bbnplace.com/checkout/image/webpaymentgateways.gif" height="55" />
</div>
<div style="font-size: 14px;">
Account Name: <b>Browser Based Nigeria</b>
</div>
<div style="clear: both; width: 100%;">
<div id="bM" align="center">
<img src="http://www.bbnplace.com/sms/media/images/zenith.jpeg"
alt="Zenith Bank" width="50" height="50" /><br />1012259075
</div>
<div id="bM" align="center">
<img src="http://www.bbnplace.com/sms/media/images/gtblogo.gif"
alt="Guaranty Trust Bank" width="50" height="50" /><br />0008382123
</div>
<div id="bM" align="center">
<img src="http://www.bbnplace.com/sms/media/images/diamondbank.jpg"
alt="Diamond Bank Plc" width="50" height="50" /><br />0010549507
</div>
<div id="ibM" align="center">
<img src="http://www.bbnplace.com/sms/media/images/accessbank_logo.png"
alt="Access Bank Plc" height="30" style="padding: 5px 0;" /><br />0049632011</div>
</div>
</div></div></div>
<div id="form_pane">
<div id="form_panel"><div align="left" id="signup">
<form name="signup_init" method="post" onsubmit = "return false;" autocomplete="off">
<div align="right"><h2>Get Started!</h2></div>
<div id="signup_init_error_message"></div>
<div><b>Email</b></div>
<div>
<input name="new_user_email" type="email" id="new_user_email" onkeypress="quickSubmit(event, 'signup_init', 'signup_init_error_message')" title="Type Email" size="45" style="width:100%" />
<span style="display: none;">User Email</span>
</div>
<div>
<input type="hidden" name="validemail" value="0" />
<input type="hidden" name="bservice" value="prepaidsms" />
<input type="button" name="button2" id="aC" value="Sign Up" onclick="processFrm('signup_init', 'signup_init_error_message');" />
</div>
</form>
</div></div>
<div id="forms_seperator">OR</div>
<div id="form_panel"><div>
<form name="blogin" method="post" onsubmit="return false;" autocomplete="off">
<div align="right"> <h2>Login</h2> </div>
<div id="errormsg"></div>
<div align="left">Email<br />
<label for="username"></label>
<input type="email" name="busername" id="busername" class="frmfield" value="" onkeypress="quickSubmit(event, 'blogin', 'errormsg')" title="This should be your email" />
</div>
<div align="left">Password<br />
<input type="password" name="passwd" id="passwd" class="frmfield" value="" onkeypress="quickSubmit(event, 'blogin', 'errormsg')" />
</div>
<div>
<input type="hidden" name="bservice" id="bservice" value="prepaidsms" />
<input type="hidden" name="returnto" id="returnto" value="http://sms.bbnplace.com/sentdir.php" />
<input type="hidden" name="errordiv" id="errordiv" value="errormsg" />
<input type="hidden" name="ipaddress" id="ipaddress" value="41.58.242.131" />
</div>
<div align="left">
<label><input name="rememberme" type="checkbox" id="rememberme" value="1" /> Remember Me!</label>
<input type="button" name="button" id="button" class="K" value="Login" onclick="processFrm('blogin','errormsg')" />
</div>
<div style="padding: 5px 0;">Can't access my account</div>
</form>
</div></div>
<div style="margin: 18px 0;">
<div style="margin: 0 0 5px;">Join our social conversation</div>
<div class="fb-like" data-href="http://www.facebook.com/bbnsms"
data-send="false" data-layout="button_count" data-width="250"
data-show-faces="false"></div>
</div>
</div>
</div></div>
<div style="clear:both;"> </div><div id="statsbar"></div>
</div>
<div id="aj" align="center"></div>
<div align="center" id="bn">
<a href="//bs.bbnplace.com" title="Business Solutions">Business
Solutions</a> | Developers | Documentation
| Newsroom
| Blog | <a
href="//www.bbnplace.com?entry=contact" target="_blank">Contact
Us</a> <a href="//twitter.com/bbnplace" title="on Twitter"
target="_blank"><img src="//www.bbnplace.com/accounts_v2/style/twitter.png" alt="on Twitter"
width="16" height="16" border="0" /></a> <a
href="//www.facebook.com/bbnplace" title="on Facebook"
target="_blank"><img src="//www.bbnplace.com/accounts_v2/style/facebook.png" alt="on Facebook"
width="16" height="16" border="0" /></a> <a
href="//www.linkedin.com/groups?gid=4620527" title="at LinkedIn"
target="_blank"><img src="//www.bbnplace.com/accounts_v2/style/linkedin.png" alt="at LinkedIn"
width="16" height="16" border="0" /></a> <a
href="//www.youtube.com/user/bbnplace" title="on Youtube"
target="_blank"><img src="//www.bbnplace.com/accounts_v2/style/youtube.png" alt="on Youtube"
width="16" height="16" border="0" /></a> <a
href="//newsroom.bbnplace.com" title="at the Newsroom"
target="_blank"><img src="//www.bbnplace.com/accounts_v2/style/radio.png" alt="Newsroom" width="16"
height="16" border="0" /></a>
<br /> Copyright © 2008 - 2016 <a
href="//www.bbnplace.com" title="Browser Based Network Ltd">Browser
Based Network Ltd</a>. <img src="//www.bbnplace.com/accounts_v2/style/ngr.png" align="absmiddle"
alt="Nigeria" /> Nigeria. All rights reserved.
<br />Terms of Service • Privacy Policy Statement
<br /> <b>Desktop | Mobile Edition
</b>
</div>
<div
style="float: right; position: fixed; z-index: 100000; top: 230px; right: 0px;">
<img src="//www.bbnplace.com/accounts_v2/style/feedback_button.png" onclick="bB()"
style="cursor: pointer;" />
</div><div id="t">
<div id="j">
<form name="t" action="return false;" method="post">
<input type="hidden" name="an" value="Mobile Messaging Solutions" />
<h1 style="color: #555;">Send Us Feedback</h1>
<div>If you notice something is not working properly or you have a
suggestions. We appreciate hearing from you</div>
<div id="V"></div>
<div>
<b>Full Name:</b><br /> <input type="text" name="ag"
style="width: 95%;" />
</div>
<div>
<b>Email:</b><br /> <input type="text" name="bI" style="width: 95%;" />
</div>
<div>
<b>Phone:</b> <i>(optional)</i><br /> <input type="text" name="aR"
style="width: 200px;" />
</div>
<div>
<b>Suggestion:</b><br />
<textarea name="av" rows="10"
style="width: 95%; min-width: 95%; max-width: 95%; height: 100px; max-height: 100px;"></textarea>
</div>
<div>
Read our <a href="http://docs.bbnplace.com/?article=privacy_policy"
target="_blank">Privacy Policy</a> statement
</div>
<div style="margin: 10px 0 0;">
<span id="aC" onclick="aN()">Submit Feedback</span> <span class="K"
onclick="ar()">Cancel</span>
</div>
</form>
</div>
<div id="h">
<div id="bv">
<img src="style/correct_marking.gif" align="absmiddle" /> Your
feedback has been received. Thank you for the time taken
</div>
<div style="margin: 50px 0 0;">
<span class="K" onclick="ar()">Close</span>
</div>
</div>
</div><div id="cs"><div style="text-align:center;white-space:nowrap;"> <div><img alt="Live chat" src="https://cbi.boldchat.com/aid/4518259610467392165/bc.cbi?cbdid=6794783422064795815" border="0"/></div></div></div></body>
</html>
You just need to post to the correct url, once successfully logged in you will be able to get whatever page you like, this is a working example using requests:
import requests
from bs4 import BeautifulSoup
post = "http://www.bbnplace.com/accounts_v2/source/loginp.php"
data = {"eml": "your_email",
"p": "your_pass"}
# use a Session to persist cookies.
with requests.Session() as s:
r = s.post(post, data=data) # log us in
print(r.content) # will output "successful" for correct login
r = s.get("http://sms.bbnplace.com/account.php") # get account page
soup = BeautifulSoup(r.content)
print(soup.title.text)
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()