How to select radio buttons and submit a form - python

This is the HTML:
<form method="post" action="./xxxxx" name="form1">
<input type="hidden" name="requestKey" value="52032a76cf53340b07052fd143feddc4a9d8f18130c80ec86003ab56e0d91b36"/>
<input type="hidden" name="option" value="txnlist">
<input type="hidden" name="account" value='1336169999'>
<input type="hidden" name="app" value="D">
<input type="hidden" name="lockType" value="">
<p><span class="header2"> Statement options </span>
<table summary="" width="100%" border="0" cellpadding="1" cellspacing="1">
<tr>
<td width="20"></td>
<td width="20"><input type="radio" name="optDateFilter" value="1"></td>
<td>Transactions for the last 30 days</td>
<td></td>
</tr>
<tr>
<td width="20"></td>
<td width="20"><input type="radio" name="optDateFilter" value="2"></td>
<td>Transactions for the last 90 days</td>
<td></td>
</tr>
<tr>
<td width="20"> </td>
<td width="20"><input type="radio" name="optDateFilter" checked='checked' value="3" style="vertical-align: middle"></td>
<td>
<div>
<font style="vertical-align: middle;">Transactions from </font>
<script src='jquery/js/ui/min/jquery.ui.datepicker.min-1.8.10.js'></script>
<script type='text/javascript'>$(function() {$('#datepicker1').datepicker( { changeMonth : true, changeYear : true, dateFormat : 'dd/mm/yy', showOn: 'both', buttonImage: 'images/calendar.png', buttonImageOnly: true }); }); </script>
<input type='text' id='datepicker1' name='txtFromDate' size='11' maxlength='10' value='14/12/2012' onchange="checkFromDate(document.form1.txtFromDate.value);document.form1.optDateFilter[2].checked = true;">
<font style="vertical-align: middle;">to</font>
<script type='text/javascript'>$(function() {$('#datepicker2').datepicker( { changeMonth : true, changeYear : true, dateFormat : 'dd/mm/yy', showOn: 'both', buttonImage: 'images/calendar.png', buttonImageOnly: true }); }); </script>
<input type='text' id='datepicker2' name='txtToDate' size='11' maxlength='10' value='01/01/2013' onchange="checkToDate(document.form1.txtFromDate.value,document.form1.txtToDate.value);document.form1.optDateFilter[2].checked = true;">
</div>
</td>
<td><font style="font-size: 8pt; vertical-align: middle;"><i>(maximum date range 3 months)</i></font></td>
</tr>
</table>
<table summary="" cellpadding="1" cellspacing="1" border="0">
<tr>
<td width="20"> </td>
<td width="20">
<a href="javascript:if(document.form1.optDateFilter[2].checked==true){CheckAndSubmit(document.form1.txtFromDate.value,document.form1.txtToDate.value);}else{showOverlay();document.form1.submit();};">
<img src="images/proceed.gif" alt="Show the selected range of transactions" border="0" width="22" height="22" />
</a>
</td>
<td>
Show the selected range of transactions
</td>
</tr>
</table>
</form>
This is the code i have, and it is not working:
self.browser.select_form('form1')
self.browser.form.set_all_readonly(False)
self.browser['txtFromDate'] = '28/12/12'
self.browser['txtToDate'] = '01/01/13'
resp = self.browser.submit()
html = resp.read()

I figured that i had to do this
self.browser.select_form('form1')
self.browser['optDateFilter'] = ['2']
self.browser.form.find_control(name="txtFromDate", id="datepicker1").value = "13/10/12"
self.browser.form.find_control(name="txtToDate", id="datepicker2").value = "01/01/13"
resp = self.browser.submit()

mechanize has a strange quirk: since there can be multiple radio button elements in a HTML document with the same name it insists that you use a list as value, even though only a single radio button may be selected at any time. That means that you need to use this:
self.browser.form.find_control(name='optDateFilter').value = ['2']
The same holds for checkboxes where it makes more sense because a user is allowed to select multiple values.

Related

Why request.form from input fields not work?

I have an HTML page with a form compiled yet with default values as a modify form, in python program i want to get the modified information, if there are. i have a ID to use for update the data in my DB with pymssql. When i get data from the form in HTML page, the ID it's get, but the CF block the program and give me the error: POST/[name_page] HTTP/1.1" 400
PYTHON
#app.route('/salva_modifiche_paziente',methods=['POST'])
def dati_paziente_modificato():
id = dett_id_paz()
cf = request.form.get['cf']
nome = request.form['nome']
cognome = request.form['cognome']
data_nascita = request.form['data_nascita']
residenza = request.form['residenza']
grado_dolore = request.form['grado_dolore']
sintomi = request.form['sintomi']
data_ricovero = request.form['data_ricovero']
data_dimissione = request.form['data_dimissione']
reparto = request.form['reparto']
n_stanza = request.form['n_stanza']
n_letto = request.form['n_letto']
modifica_paziente(id, cf, nome, cognome, data_nascita, residenza, grado_dolore, sintomi, data_ricovero,
data_dimissione, reparto, n_stanza, n_letto)
dett = dettagli_paziente_ricoverato()
return render_template('dettagli_paziente_ricoverato.html', det_paz=dett)
def modifica_paziente(id,cf,nome,cognome,data_nascita,residenza,grado_dolore,sintomi,data_ricovero,data_dimissione,reparto,n_stanza,n_letto):
connection1 = pymssql.connect(database="UNICLINIC")
connection2 = pymssql.connect(database="UNICLINIC")
cursor1 = connection1.cursor()
cursor2 = connection2.cursor()
cursor1.execute("UPDATE paziente SET CF = (%s), nome = (%s), cognome = (&s), \
data_di_nascita = (%s), residenza = (%s), grado_dolore = (%d), sintomi = (%s) WHERE ID_paziente = %d",(cf,nome,cognome,data_nascita,residenza,grado_dolore,sintomi, int(id) ))
cursor2.execute("UPDATE ricoverato SET data_ricovero = (6s), \
data_dimissione = (%s), COD_reparto = (%s), n_stanza = (%s), n_letto = (%s) \
WHERE ID_paziente = (%d)"
,(data_ricovero, data_dimissione, reparto, n_stanza, n_letto,int(id)))
connection1.commit()
connection2.commit()
cursor1.close()
cursor2.close()
connection1.close()
connection2.close()
def dett_id_paz():
id = request.form['id_paziente']
print(id)
return id
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Modifica paziente | UNICLINIC</title>
<link rel="stylesheet" href={{ url_for('static', filename='style.css') }} type="text/css" media="all"/>
</head>
<body>
<div>
<table class="head_banner">
<tr>
<td><img id="logo" src={{ url_for('static', filename='immagini/logo_uniclinic.png') }} alt="Logo UNICLINIC"/></td>
<td class="menu"><button class="menu_btn">HOME</button></td>
<td class="menu"><button class="menu_btn">LOGIN</button></td>
<td class="menu"><button class="menu_btn">CONTACTS</button></td>
</tr>
</table>
<table class="tab_hd">
<thead>
<th>
<td colspan="2" id="p_tit">Dati profilo di {{det_paz.nome}} {{det_paz.cognome}}<td>
</th>
</thead>
</table>
<table class="tab_profilo">
<form id="modifica" method="post" action="salva_modifiche_paziente" class="form">
<tr>
<td class="p_dn">ID paziente:</td>
<td class="p_dv">
{{det_paz.id}}
<input style="display:none" id="id_paziente" name="id_paziente" type="text" value="{{det_paz.id}}" />
</td>
</tr>
<tr>
<td class="p_dn">Codice Fiscale:</td>
<td class="p_dv">
<input type="text" id="cf" name="cf " value="{{det_paz.cf}}"/>
</td>
</tr>
<tr>
<td class="p_dn">Nome:</td>
<td class="p_dv">
<input type="text" id="nome" name="nome" value="{{det_paz.nome}}"/>
</td>
</tr>
<tr>
<td class="p_dn">Cognome:</td>
<td class="p_dv">
<input type="text" id="cognome" name="cognome" value="{{det_paz.cognome}}"/>
</td>
</tr>
<tr>
<td class="p_dn">Data di nascita:</td>
<td class="p_dv">
<input type="date" id="data_nascita" name="data_nascita" value="{{det_paz.data_nascita}}"/>
</td>
</tr>
<tr>
<td class="p_dn">Residenza:</td>
<td class="p_dv">
<input type="text" id="residenza" name="residenza" value="{{det_paz.residenza}}"/>
</td>
</tr>
<tr>
<td class="p_dn">Grado dolore:</td>
<td class="p_dv">
<select name="grado_dolore" id="grd_d">
{% for g in range(10) %}
<option type="text" id="grado_dolore" name="grado_dolore" value="{{g+1}}">Grado {{g+1}}</option>
{% endfor %}
</select>
</td>
</tr>
<tr>
<td class="p_dn">Sintomi:</td>
<td class="p_dv">
<input type="text" id="sintmi" name="sintomi" value="{{det_paz.sintomi}}"/>
</td>
</tr>
<tr>
<td class="p_dn">Data ricovero:</td>
<td class="p_dv">
<input type="date" id="data_ricovero" name="data_ricovero" value="{{det_paz.data_ricovero}}"/>
</td>
</tr>
<tr>
<td class="p_dn">Data dimissione:</td>
<td class="p_dv">
<input type="date" id="data_dimissione" name="data_dimissione" value="{{det_paz.data_dimissione}}"/>
</td>
</tr>
<tr>
<td class="p_dn">Reparto ricovero:</td>
<td class="p_dv">
<label for="reparto"></label>
<select name="reparto" id="rep" class="p_dv">
{% for reparto in rep %}
<option type="text" id="reparto" name="reparto" value="{{reparto.COD_reparto}}">{{reparto.COD_reparto}} - {{reparto.nome_reparto}}</option>
{% endfor %}
</select>
</td>
</tr>
<tr>
<td class="p_dn">Stanza nr.:</td>
<td class="p_dv">
<input type="text" id="n_stanza" name="n_stanza" value="{{det_paz.n_stanza}}"/>
</td>
</tr>
<tr>
<td class="p_dn">Letto nr.:</td>
<td class="p_dv">
<input type="text" id="n_letto" name="n_letto" value="{{det_paz.n_letto}}"/>
</td>
</tr>
<tr class="r_bd">
<td class="b_dn"><button type="submit" form="modifica" class="menu_btn_d">CONFERMA</button></td>
<td class="b_dn"><button type="reset" class="menu_btn_d">ANNULLA</button></td>
</tr>
</form>
</table>
</div>
</body>
</html>

How to use XPath get content of same field?

I am a beginner of Xpath and just can not match content correctly. Here is my question:
How to use XPath to get the date '2010.09.07' (after 申请日:) and '2009.09.03'? Actually, there are 10 same items (g_item) below g_list hierarchy, here I just listed two of them. I try to copy Xpath from Chorm, while it doesn't work.
Also, I try to use regex as below,however, it just matches the first one. Is there a way to return all dates of all items?
Thanks!
s.find(string=('申请日:')).find_next().text.replace('\n', '').strip()
<div class="g_list">
<div class="g_item">
<div class="g_tit">
<ul>
<li class="g_li0">
<input id="CN201010274593.21" name="recordno" type="checkbox" value="CN201010274593.2" pnm="CN102403785B" sysid="B58C6C20BB7D5998B03811E0866F5981" appid="201010274593.2" sectionName="FMSQ" onclick="checkall()"/></li>
<input id="tifPath1" name="tifPath" type="hidden" tifvalue="BOOKS/SD/2014/20140716/201010274593.2,12,CN201010274593.2" xmlvalue="FMSQ,CN201010274593.2,2014.07.16" pdfvalue="Granted_patent_for_invention/2014/20140716/CN102403785B/PDF_PID/CN102010000274593CN00001024037850BPDFZH20140716CN008.PDF,CN201010274593.2" pdfvalue2="CN102403785B,2014.07.16"/>
<li class="g_li" onclick="viewDetail(0)" style="cursor:pointer" name='patti' title="电源管理装置及其电源管理方法">
1.电源管理装置及其电源管理方法</li>
<li class="g_li1">发明授权 </li>
<li class="g_li2 cor3">无效</li>
<li class="g_li3">下载</li>
</ul>
<div class="clear"></div>
</div>
<div class="g_cont">
<div class="g_cont_left">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td><span>申请号:</span> CN201010274593.2 </td>
<td><span>申请日:</span> 2010.09.07 </td>
</tr>
<tr>
<td><span>公开(公告)号:</span> CN102403785B </td>
<td><span>公开(公告)日:</span> 2014.07.16 </td>
</tr>
<tr>
<td><span>同日申请:
</td>
<td><span>分案原申请号:
</td>
</tr>
<tr>
<td colspan="2" style="width:610px;word-break:break-all;"><span>申请(专利权)人:</span> 鸿富锦精密工业(深圳)有限公司;鸿海精密工业股份有限公司 </td>
</tr>
<tr>
<td colspan="2" style="width:610px;word-break:break-all;"><span>分类号:</span> H02J13/00(2006.01) </td>
</tr>
<tr>
<td colspan="2" style="width:610px;word-break:break-all;"><span>优先权:</span></td>
</tr>
<tr>
<td colspan="2"><span>摘要:</span><span name="patab" style="font-weight:normal"></span>
<a name="abmtlink" href="javascript:return false;" style="color:blue">机器翻译</a></td>
</tr>
</table>
</div>
<div class="g_cont_rig" id="pic1">
<img name="tifpath" src="http://pic.cnipr.com/XmlData/SQ\20140716\201010274593.2/201010274593.gif" class="imgstyle"/>
</div>
<div class="clear"></div>
</div>
</div>
<div class="g_item">
<div class="g_tit">
<ul>
<li class="g_li0">
<input id="CN200910171675.12" name="recordno" type="checkbox" value="CN200910171675.1" pnm="CN102006581B" sysid="E7025BBD105585DF6CE4193E52ECC322" appid="200910171675.1" sectionName="FMSQ" onclick="checkall()"/></li>
<input id="tifPath2" name="tifPath" type="hidden" tifvalue="BOOKS/SD/2013/20130911/200910171675.1,21,CN200910171675.1" xmlvalue="FMSQ,CN200910171675.1,2013.09.11" pdfvalue="Granted_patent_for_invention/2013/20130911/CN102006581B/PDF_PID/CN102009000171675CN00001020065810BPDFZH20130911CN008.PDF,CN200910171675.1" pdfvalue2="CN102006581B,2013.09.11"/>
<li class="g_li" onclick="viewDetail(1)" style="cursor:pointer" name='patti' title="IP地址强制续约的方法及装置">
2.IP地址强制续约的方法及装置</li>
<li class="g_li1">发明授权 </li>
<li class="g_li2 cor3">无效</li>
<li class="g_li3">下载</li>
</ul>
<div class="clear"></div>
</div>
<div class="g_cont">
<div class="g_cont_left">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td><span>申请号:</span> CN200910171675.1 </td>
<td><span>申请日:</span> 2009.09.03 </td>
</tr>
<tr>
<td><span>公开(公告)号:</span> CN102006581B </td>
<td><span>公开(公告)日:</span> 2013.09.11 </td>
</tr>
<tr>
<td><span>同日申请:
</td>
<td><span>分案原申请号:
</td>
</tr>
<tr>
<td colspan="2" style="width:610px;word-break:break-all;"><span>申请(专利权)人:</span> 中兴通讯股份有限公司 </td>
</tr>
<tr>
<td colspan="2" style="width:610px;word-break:break-all;"><span>分类号:</span> H04W8/08(2009.01);H04W36/14(2009.01);H04W84/12(2009.01);H04L29/12(2006.01) </td>
</tr>
<tr>
<td colspan="2" style="width:610px;word-break:break-all;"><span>优先权:</span></td>
</tr>
<tr>
<td colspan="2"><span>摘要:</span><span name="patab" style="font-weight:normal"></span>
<a name="abmtlink" href="javascript:return false;" style="color:blue">机器翻译</a></td>
</tr>
</table>
</div>
<div class="g_cont_rig" id="pic2">
<img name="tifpath" src="http://pic.cnipr.com/XmlData/SQ/20130911/200910171675.1/200910171675.gif" class="imgstyle"/>
</div>
<div class="clear"></div>
</div>
</div>
</div>
</div>
Your HTML has multiple markup validation errors, you can check for the errors using W3 validator. However, if you fix the following errors is possible to parse the string using lxml.
Unclosed element span.
From line 43, column 37; to line 43, column 42
Unclosed element span.
From line 46, column 37; to line 46, column 42
Unclosed element span.
From line 119, column 37; to line 119, column 42
Unclosed element span.
From line 122, column 37; to line 122, column 42
Stray end tag div.
From line 159, column 5; to line 159, column 10
from lxml import etree
pageHTML = """
<div class="g_list">
<div class="g_item">
...
...
"""
root = etree.fromstring(pageHTML)
dateList = root.xpath("//*[#class='g_cont_left']/table/tr[1]/td[2]/text()")
print(dateList)
#[' 2010.09.07 ', ' 2009.09.03 ']
If you still want to use a regex (which I would advise against, given the whole discussion about applying regular expressions over an HTML, XML, etc. or use a parser specific for that grammar) you can define a capturing group allowing only digits and a literal . (([\d\.]+)) surrounded by the exact words you expect to find.
import re
pageHTML = """
<div class="g_list">
<div class="g_item">
...
...
"""
date_Regex = re.findall("申请日:\s*</span>\s*([\d\.]+)\s*</td>", pageHTML)
print(date_Regex)
# ['2010.09.07', '2009.09.03']

Updating the same site with Python's Flask module

Background:
I'm fairly new to developing front end and the Flask module in Python. I am difficulty updating the same page.
What I want to achieve:
There are three parts to what I want to achieve
Part/Step 1: I can create a form such as the one below
Part/Step 2: When the form is filled in as below, and the [verify] button is hit it goes to Step 3
Part/Step 3: Once the verify button is hit, just underneath it the information that was passed gets placed underneath it. There are also two buttons, with [commit] and [cancel] button. If [cancel] is hit, the site resets to as it were in Step 1. If [commit] is hit, it goes to a different website in step 4
Part/Step 4: If [commit] is hit from the image above, another site is retrieved with the following message.
Note:
Happy to share my code, but I just have the empty form in step 1. I can't proceed after that. As mentioned, I'm very new to front end stuff.
Update:
I'm having difficulties with 2 things currently:
When I click [cancel], the whole form shifts to the left (How can I make it centered?)
How can I print multiple things (ideally in a centered table) after hitting the [verify] button
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script>
$(document).ready(function() {
document.getElementById("verify").style.display = "none";
document.getElementById("committed").style.display = "none";
})
function onVerify() {
document.getElementById("verifyName").innerHTML = 'First Name: ' + document.getElementById("first_name").value
document.getElementById("verify").style.display = "block";
}
function onCommit() {
document.getElementById("form").style.display = "none";
document.getElementById("verify").style.display = "none";
document.getElementById("committed").style.display = "block";
}
function onCancel() {
document.getElementById("form").style.display = "block";
document.getElementById("verify").style.display = "none";
document.getElementById("name").value = ""
}
</script>
<style>
h3 {text-align: center;}
.right {
text-align: right;
margin-right: 1em;
}
.center {
display: flex;
justify-content: center;
align-items: center;
}
</style>
</head>
<h3>Personnel Details</h3>
<div class="center" id="form">
<table>
<tr>
<td class="right">
<label>Salutation:</label>
</td>
<td>
<select name="gender">
<option value=""></option>
<option value="male">Mr.</option>
<option value="female">Ms.</option>
</select>
</td>
</tr>
<tr>
<td class="right">
<label>First Name:</label>
</td>
<td>
<input id="first_name">
</td>
</tr>
<tr>
<td class="right">
<label>Middle Name:</label>
</td>
<td>
<input id="middle_name">
</td>
</tr>
<tr>
<td class="right">
<label>Last Name:</label>
</td>
<td>
<input id="last_name">
</td>
</tr>
<tr>
<td class="right">
<label>Email:</label>
</td>
<td>
<input id="email">
</td>
</tr> <tr>
<td class="right">
<label>DOB:</label>
</td>
<td>
<input type="date" name="issue_date" value="" min="1900-01-01" max="2100-12-31">
</td>
</tr>
<tr>
<td>
</td>
<td class="center">
<div>
<button onclick="onVerify()">Verify</button>
</div>
</td>
</tr>
</table>
</div>
<div id="verify">
<div id="verifyName">
</div>
<button onclick="onCommit()">Commit</button>
<button onclick="onCancel()">Cancel</button>
</div>
<div id="committed">
Committed! :)
</div>
</html>
This is just the minimal code, I have used to just give an idea, how to achieve the similar behavior, with just one input field. You can easily extend this with other fields.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script>
$(document).ready(function() {
document.getElementById("verify").style.display = "none";
document.getElementById("committed").style.display = "none";
})
function onVerify() {
document.getElementById("verifyName").innerHTML = 'First Name: ' + document.getElementById("name").value
document.getElementById("verify").style.display = "block";
}
function onCommit() {
document.getElementById("form").style.display = "none";
document.getElementById("verify").style.display = "none";
document.getElementById("committed").style.display = "block";
}
function onCancel() {
document.getElementById("form").style.display = "block";
document.getElementById("verify").style.display = "none";
document.getElementById("name").value = ""
}
</script>
<div id="form">
<label> First Name:
<input id="name">
</label>
<div>
<button onclick="onVerify()">Veryfy</button>
</div>
</div>
<div id="verify">
<div id="verifyName">
</div>
<button onclick="onCommit()">Commit</button>
<button onclick="onCancel()">Cancel</button>
</div>
<div id="committed">
Committed! :)
</div>

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

Beautiful Soup Table, stop getting info

Hey everyone I have some html that I am parsing, here it is:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<table class="dayinner">
<tr class="lun">
<td class="mealname" colspan="3">LUNCH</td>
</tr>
<tr class="lun">
<td class="station"> Deli</td>
<td class="menuitem">
<div class="menuitem">
<input class="chk" id="S1L0000010000047598_35356" onclick=
"rptlist(this);" onmouseout="wschk(0);" onmouseover=
"wschk(1);" type="checkbox" /> <span class="ul" onclick=
"nf('0000047598_35356');" onmouseout="pcls(this);"
onmouseover="ws(this);">Made to Order Deli Core</span>
</div>
</td>
<td class="price"></td>
</tr>
<tr class="lun">
<td class="station"> </td>
<td class="menuitem">
<div class="menuitem">
<input class="chk" id="S1L0000020000046033_63436" onclick=
"rptlist(this);" onmouseout="wschk(0);" onmouseover=
"wschk(1);" type="checkbox" /> <span class="ul" onclick=
"nf('0000046033_63436');" onmouseout="pcls(this);"
onmouseover="ws(this);">Chicken Caesar Wrap</span>
</div>
</td>
<td class="price"></td>
</tr>
<tr class="lun">
<td colspan="3" style="height:3px;"></td>
</tr>
<tr class="lun">
<td colspan="3" style="background-color:#c0c0c0; height:1px;"></td>
</tr>
<tr class="lun">
<td class="station"> Dessert</td>
<td class="station"> </td>
<td class="menuitem">
<div class="menuitem">
<input class="chk" id="S1L0000020000046033_63436" onclick=
"rptlist(this);" onmouseout="wschk(0);" onmouseover=
"wschk(1);" type="checkbox" /> <span class="ul" onclick=
"nf('0000046033_63436');" onmouseout="pcls(this);"
onmouseover="ws(this);">Chicken Caesar Wrap</span>
</div>
</td>
</tr>
</table>
</body>
</html>
Here is the code I have, I want just the items under the deli section, and normally I won't know how many there are is there a way to do this?
soup = BeautifulSoup(open("upperMenu.html"))
title = soup.find('td', class_='station').text.strip()
spans = soup.find_all('span', class_='ul')[:2]
but this only works if there are two items, how can I have it work if the number of items is unknown?
Thanks in advance
You can use the text attribute in find_all function to 1. find all the rows whose station column contains the substring Deli.. 2. Loop through every row and find the spans within that row whose class is ul.
import re
soup = BeautifulSoup(text)
tds_deli = soup.find_all(name='td', attrs={'class':'station'}, text=re.compile('Deli'))
for td in tds_deli:
try:
tr = td.find_parent()
spans = tr.find_all('span', {'class':'ul'})
for span in spans:
# do something
print span.text
print '------------one row -------------'
except:
pass
Sample Output in this case:
Made to Order Deli Core
------------one row -------------
Not sure if I am understanding the problem correctly but I think my code might help you get started.

Categories