By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,767 Members | 1,357 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,767 IT Pros & Developers. It's quick & easy.

possible issue with mechanize/python parsing

P: n/a
hi...

it appears that i'm running into a possible problem with
mechanize/browser/python rgarding the "select_form" method. i've tried the
following and get the error listed:

br.select_form(nr = 1)
br.select_form(name="foo")
br.select_form(name=foo)
br.select_form(name="foo")
here's a short test app, as well as the html to be placed in a test data
file....

everything is straight forward...

any thoughts/comments/ideas would be helpful. i have the latest mechanize
from the svn repos.

thanks

-bruce
the error i get is:
Traceback (most recent call last):
File "./axess.py", line 127, in ?
br.select_form(name = "main")
File "build/bdist.linux-i686/egg/mechanize/_mechanize.py", line 354, in
select_form
mechanize._mechanize.BrowserStateError: not viewing HTML

test code
-------------------------------
#! /usr/bin/env python
#test python script
import re
import libxml2dom
import urllib
import urllib2
import sys, string
#import numarray
import httplib
from mechanize import Browser
import mechanize

########################
#
# Parsing App Information
########################


# datafile
tfile = open("stanford.dat", 'wr+')

cj = mechanize.CookieJar()
br = Browser()
if __name__ == "__main__":
# main app
#----------------------------
# start trying to get the stanford pages
cj = mechanize.CookieJar()
br = Browser()

fh = open('axess.dat')
s = fh.read()
fh.close()
br.open("file:///home/test/axess.dat")

print "foo"

# particular cookiejar)
br.set_cookiejar(cj)
# Log information about HTTP redirects and Refreshes.
##br.set_debug_redirects(True)
# Log HTTP response bodies (ie. the HTML, most of the time).

#WARNING!!!!!! using this will apparently
#kill the Browser instance!!!
#br.set_debug_responses(True)
# Print HTTP headers.
# br.set_debug_http(True)
# br.set_handle_redirect(True)
# br.set_handle_referer(True)

response = br.response() # this is a copy of response

#get the option/semester name
snamepath =
"/html/body[@class='PSPAGE']/form[2]/table/tr[7]/td[3]/select/@name"

#get the form name
fnamepath = "/html/body[@class='PSPAGE']/form[2]/attribute::name"

s = response.read()
print response.read()
print s
#we now have the semester page...
d = libxml2dom.parseString(s, html=1)

#get option name
sem_optname = d.xpath(snamepath)
sem_optname = sem_optname[0].nodeValue

print "sem = ",sem_optname

ff = d.xpath(fnamepath)
fname = ff[0].nodeValue
print "fname = ",fname
br.select_form(name = "main")

print "ssssss"
sys.exit()

data file
-------------------------------
<html lang='en'>
<!-- Copyright (c) 2000-2005 PeopleSoft, Inc. All Rights Reserved. -->
<! IE/6.0/WINNT; ToolsRel=8.22.05; Page=CLASS_SRCH_ENTRY;
Component=CLASS_SEARCH; Menu=SA_LEARNER_SERVICES; -->
<HEAD>
<script language='JavaScript'>
var totalTimeoutMilliseconds = 1200000;
var warningTimeoutMilliseconds = 1080000;
var timeOutURL =
'https://axess.stanford.edu/psp/psp_prd/?cmd=expire&languageCd=ENG';
var timeoutWarningPageURL =
'https://psweb.stanford.edu/servlets/iclientservlet/a2k_prd/?ICType=Script&I
CScriptProgramName=WEBLIB_TIMEOUT.PT_TIMEOUTWARNIN G.FieldFormula.IScript_TIM
EOUTWARNING';
</script>
<TITLE>View Schedule of Classes</TITLE>
<LINK REL=STYLESHEET TYPE='TEXT/CSS'
HREF='/servlets/cs/a2k_prd/cache/PSSTYLEDEF_STF_ENG_1.css'>
<SCRIPT language='JavaScript'>
var baseKey_main = "";
var altKey_main = "05678\xbc\xbe\xbf\xde";
var ctrlKey_main = "JK";
var bTabOverTB_main = false;
var bTabOverPg_main = false;
var bTabOverNonPS_main = false;
</SCRIPT>
<SCRIPT LANGUAGE='javascript'
SRC='/servlets/cs/a2k_prd/cache/PT_SCRIPTIE600_ENG_main_1.js'>
</SCRIPT>
<SCRIPT LANGUAGE='JavaScript'>
document.domain = "stanford.edu";
</SCRIPT>
<SCRIPT LANGUAGE='javascript'
SRC='/servlets/cs/a2k_prd/cache/PT_PAGESCRIPT_ENG_main_1.js'>
</SCRIPT>
<SCRIPT LANGUAGE='javascript'
SRC='/servlets/cs/a2k_prd/cache/PT_SAVEWARNINGSCRIPT_ENG_main_1.js'>
</SCRIPT>
<SCRIPT LANGUAGE='javascript'
SRC='/servlets/cs/a2k_prd/cache/PT_ISCROSSDOMAIN_ENG_main_1.js'>
</SCRIPT>
<SCRIPT LANGUAGE='JavaScript'>
function submitAction_main(form, name)
{
form.ICAction.value=name;
form.ICXPos.value=getScrollX();
form.ICYPos.value=getScrollY();
form.submit();
}
</SCRIPT>
<SCRIPT LANGUAGE='javascript'
SRC='/servlets/cs/a2k_prd/cache/PT_EDITSCRIPT_ENG_main_1.js'>
</SCRIPT>
</HEAD>
<BODY CLASS='PSPAGE' onLoad="
setFocus_main('STF_AX_CLAS_DRV_STRM',-1);
setEventHandlers_main('ICFirstAnchor_main', 'ICLastAnchor_main', false);
setupTimeout();
setKeyEventHandler_main();
"
onunload=""
>
<a name='ICFirstAnchor_main'></a>
<FORM NAME='HelpURL' METHOD=POST Action=""><INPUT TYPE=hidden NAME=ICHelpUrl
VALUE="http://psrepos.stanford.edu:9070/PSOL/htmldoc/f1search.htm?ContextID=
CLASS_SRCH_ENTRY&LangCD=ENG"></FORM>
<table cols='2' width='100%' cellpadding='0' cellspacing='0' hspace='0'
vspace='0'>
<tr>
<td width='90%'></td><td width='10%' nowrap='nowrap' align='right'><a
href="http://psrepos.stanford.edu:9070/PSOL/htmldoc/f1search.htm?ContextID=C
LASS_SRCH_ENTRY&LangCD=ENG" target='help' accesskey='9' tabindex='1'
class='PSHYPERLINK'>Help</a></td></tr>
</table>
<br />
<FORM NAME='main' METHOD=POST
Action="/servlets/iclientservlet/a2k_prd/?ICType=Panel&Menu=SA_LEARNER_SERVI
CES&Market=GBL&PanelGroupName=CLASS_SEARCH" autocomplete=off>
<INPUT TYPE=hidden NAME=ICType VALUE=Panel>
<INPUT TYPE=hidden NAME=ICElementNum VALUE="0">
<INPUT TYPE=hidden NAME=ICStateNum VALUE="2">
<INPUT TYPE=hidden NAME=ICAction VALUE=None>
<INPUT TYPE=hidden NAME=ICXPos VALUE=0>
<INPUT TYPE=hidden NAME=ICYPos VALUE=0>
<INPUT TYPE=hidden NAME=ICFocus VALUE="">
<input type=hidden name=ICChanged value='-1' />
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 COLS=9 WIDTH=553>
<TR>
<TD WIDTH=8 HEIGHT=8></TD>
<TD WIDTH=4></TD>
<TD WIDTH=8></TD>
<TD WIDTH=4></TD>
<TD WIDTH=1></TD>
<TD WIDTH=95></TD>
<TD WIDTH=36></TD>
<TD WIDTH=148></TD>
<TD WIDTH=249></TD>
</TR>
<TR>
<TD HEIGHT=24></TD>
<TD COLSPAN=8 VALIGN=TOP ALIGN=LEFT>
<LABEL FOR='DERIVED_AS_LBL_TITLE_NAME_PART' CLASS='PATRANSACTIONTITLE'
>Class Search</LABEL>
</TD>
</TR>
<TR>
<TD HEIGHT=29 COLSPAN=2></TD>
<TD COLSPAN=7 VALIGN=TOP ALIGN=LEFT>
<LABEL FOR='DERIVED_AS_LBL_CLASS_LBL' CLASS='PAPAGETITLE' >Select
Term</LABEL>
</TD>
</TR>
<TR>
<TD HEIGHT=20 COLSPAN=5></TD>
<TD COLSPAN=4 VALIGN=TOP ALIGN=LEFT>
<DIV CLASS='PAPAGEINSTRUCTIONS' >&nbsp;</DIV>
</TD>
</TR>
<TR>
<TD HEIGHT=20 COLSPAN=5></TD>
<TD COLSPAN=4 VALIGN=TOP ALIGN=LEFT>
<DIV CLASS='PAPAGEINSTRUCTIONS' >Select the term you wish to search, and
then click Basic Search,</DIV>
</TD>
</TR>
<TR>
<TD HEIGHT=43 COLSPAN=5></TD>
<TD COLSPAN=4 VALIGN=TOP ALIGN=LEFT>
<DIV CLASS='PAPAGEINSTRUCTIONS' >Advanced Search, or Independent Study
Search to continue.</DIV>
</TD>
</TR>
<TR>
<TD HEIGHT=55 COLSPAN=4></TD>
<TD COLSPAN=2 VALIGN=TOP ALIGN=LEFT>
<LABEL FOR='STF_AX_CLAS_DRV_STRM' CLASS='PSDROPDOWNLABEL' >*Select a
Term:</LABEL>
</TD>
<TD COLSPAN=3 VALIGN=TOP ALIGN=LEFT>
<SELECT NAME='STF_AX_CLAS_DRV_STRM' ID='STF_AX_CLAS_DRV_STRM' SIZE=1
TABINDEX=15 CLASS='PSDROPDOWNLIST' STYLE="width:207px; " >
<OPTION VALUE="1068">(1068)&nbsp; 2005-2006 Summer
<OPTION VALUE="1066">(1066)&nbsp; 2005-2006 Spring
<OPTION VALUE="1064">(1064)&nbsp; 2005-2006 Winter
<OPTION VALUE="1062">(1062)&nbsp; 2005-2006 Autumn
<OPTION VALUE="" SELECTED>
</SELECT>
</TD>
</TR>
<TR>
<TD HEIGHT=122 COLSPAN=3></TD>
<TD COLSPAN=4 NOWRAP VALIGN=TOP ALIGN=LEFT>
<INPUT TYPE=BUTTON NAME='CLASS_SRCH_BASIC' ID='CLASS_SRCH_BASIC' TABINDEX=17
VALUE="Basic Search" CLASS='PSPUSHBUTTON' STYLE="width:124px; "
ONCLICK="submitAction_main(this.form,this.name);">
</TD>
<TD NOWRAP VALIGN=TOP ALIGN=LEFT>
<INPUT TYPE=BUTTON NAME='CLASS_SRCH_ADV' ID='CLASS_SRCH_ADV' TABINDEX=18
VALUE="Advanced Search" CLASS='PSPUSHBUTTON' STYLE="width:124px; "
ONCLICK="submitAction_main(this.form,this.name);">
</TD>
<TD NOWRAP VALIGN=TOP ALIGN=LEFT>
<INPUT TYPE=BUTTON NAME='CLASS_SRCH_ADV1' ID='CLASS_SRCH_ADV1' TABINDEX=16
VALUE="Independent Study Search" CLASS='PSPUSHBUTTON' STYLE="width:152px; "
ONCLICK="submitAction_main(this.form,this.name);">
</TD>
</TR>
</TABLE>
</FORM>
<a name='ICLastAnchor_main'></a>
</BODY>
</HTML>

Jul 10 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.