I am a bit out of sync with web programming and getting into HTMLUnit is a bit more confusing than I thought it would be.
Essentially I missed registering a class and need to be notified the moment a space opens but prior to getting to that page dump I need to submit a form with two radio inputs ( with the options "Spring Semester 2019" and the "All Classes").
I am in a weird spot where I want to learn more but also need a working script, so a combination of an answer plus some resources I might not be utilizing would be awesome! For example when I do get to the next page how do I download the html file raw and access the required data like number of spots filled and available in xyz class.
https://mystudentrecord.ucmerced.edu/pls/PROD/xhwschedule.p_selectsubject
Here is the monkey little program I wrote to get my feet a bit wet:
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlRadioButtonInput;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
import java.io.IOException;
import java.net.MalformedURLException;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebClient;
public class hateMerced {
public void submittingForm() throws Exception {
}
public static void main(final String[] args) throws IOException {
final WebClient webClient = new WebClient();
// Get the first page
HtmlPage page1 = webClient.getPage("https://mystudentrecord.ucmerced.edu/pls/PROD/xhwschedule.p_selectsubject");
// Get the form that we are dealing with and within that form,
// find the submit button and the field that we want to change.
final HtmlForm form = page1.getFormByName("xhwschedule.P_ViewSchedule");
HtmlRadioButtonInput radioButton = (HtmlRadioButtonInput) page1.getElementById("201910");
radioButton.setChecked(true);
HtmlRadioButtonInput radioButton2 = (HtmlRadioButtonInput) page1.getElementById("N");
radioButton2.setChecked(true);
final HtmlSubmitInput button = form.getInputByName("View Class Schedule");
// Now submit the form by clicking the button and get back the second page.
// final HtmlPage page2 = button.click();
webClient.close();
}
}
And Here is the lovely error I get:
Jan 16, 2019 1:09:57 AM com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter error
SEVERE: error: message=[illegally formed XML syntax] sourceName=[script in https://mystudentrecord.ucmerced.edu/pls/PROD/xhwschedule.p_selectsubject from (11, 54) to (39, 10)] line=[38] lineSource=[// End script hiding -->] lineOffset=[24]
Exception in thread "main" ======= EXCEPTION START ========
Exception class=[net.sourceforge.htmlunit.corejs.javascript.EvaluatorException]
com.gargoylesoftware.htmlunit.ScriptException: illegally formed XML syntax (script in https://mystudentrecord.ucmerced.edu/pls/PROD/xhwschedule.p_selectsubject from (11, 54) to (39, 10)#38)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:892)
at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:616)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:534)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.compile(JavaScriptEngine.java:723)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.compile(JavaScriptEngine.java:689)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:735)
at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScript(HtmlPage.java:922)
at com.gargoylesoftware.htmlunit.html.HtmlScript.executeInlineScriptIfNeeded(HtmlScript.java:316)
at com.gargoylesoftware.htmlunit.html.HtmlScript.executeScriptIfNeeded(HtmlScript.java:396)
at com.gargoylesoftware.htmlunit.html.HtmlScript$2.execute(HtmlScript.java:246)
at com.gargoylesoftware.htmlunit.html.HtmlScript.onAllChildrenAddedToPage(HtmlScript.java:267)
at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.endElement(HTMLParser.java:802)
at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.endElement(HTMLParser.java:758)
at net.sourceforge.htmlunit.cyberneko.HTMLTagBalancer.callEndElement(HTMLTagBalancer.java:1194)
at net.sourceforge.htmlunit.cyberneko.HTMLTagBalancer.endElement(HTMLTagBalancer.java:1134)
at net.sourceforge.htmlunit.cyberneko.filters.DefaultFilter.endElement(DefaultFilter.java:221)
at net.sourceforge.htmlunit.cyberneko.filters.NamespaceBinder.endElement(NamespaceBinder.java:314)
at net.sourceforge.htmlunit.cyberneko.HTMLScanner$ContentScanner.scanEndElement(HTMLScanner.java:3179)
at net.sourceforge.htmlunit.cyberneko.HTMLScanner$ContentScanner.scan(HTMLScanner.java:2132)
at net.sourceforge.htmlunit.cyberneko.HTMLScanner.scanDocument(HTMLScanner.java:939)
at net.sourceforge.htmlunit.cyberneko.HTMLConfiguration.parse(HTMLConfiguration.java:452)
at net.sourceforge.htmlunit.cyberneko.HTMLConfiguration.parse(HTMLConfiguration.java:403)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.parse(HTMLParser.java:1001)
at com.gargoylesoftware.htmlunit.html.HTMLParser.parse(HTMLParser.java:250)
at com.gargoylesoftware.htmlunit.html.HTMLParser.parseHtml(HTMLParser.java:196)
at com.gargoylesoftware.htmlunit.DefaultPageCreator.createHtmlPage(DefaultPageCreator.java:267)
at com.gargoylesoftware.htmlunit.DefaultPageCreator.createPage(DefaultPageCreator.java:158)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:531)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:398)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:315)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:466)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:448)
at FuckMerced.main(FuckMerced.java:34)
Caused by: net.sourceforge.htmlunit.corejs.javascript.EvaluatorException: illegally formed XML syntax (script in https://mystudentrecord.ucmerced.edu/pls/PROD/xhwschedule.p_selectsubject from (11, 54) to (39, 10)#38)
at com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter.error(StrictErrorReporter.java:65)
at net.sourceforge.htmlunit.corejs.javascript.Parser.addError(Parser.java:260)
at net.sourceforge.htmlunit.corejs.javascript.Parser.addError(Parser.java:232)
at net.sourceforge.htmlunit.corejs.javascript.Parser.addError(Parser.java:228)
at net.sourceforge.htmlunit.corejs.javascript.TokenStream.getNextXMLToken(TokenStream.java:1287)
at net.sourceforge.htmlunit.corejs.javascript.TokenStream.getFirstXMLToken(TokenStream.java:1136)
at net.sourceforge.htmlunit.corejs.javascript.Parser.xmlInitializer(Parser.java:2666)
at net.sourceforge.htmlunit.corejs.javascript.Parser.unaryExpr(Parser.java:2641)
at net.sourceforge.htmlunit.corejs.javascript.Parser.mulExpr(Parser.java:2568)
at net.sourceforge.htmlunit.corejs.javascript.Parser.addExpr(Parser.java:2552)
at net.sourceforge.htmlunit.corejs.javascript.Parser.shiftExpr(Parser.java:2533)
at net.sourceforge.htmlunit.corejs.javascript.Parser.relExpr(Parser.java:2508)
at net.sourceforge.htmlunit.corejs.javascript.Parser.eqExpr(Parser.java:2480)
at net.sourceforge.htmlunit.corejs.javascript.Parser.bitAndExpr(Parser.java:2469)
at net.sourceforge.htmlunit.corejs.javascript.Parser.bitXorExpr(Parser.java:2458)
at net.sourceforge.htmlunit.corejs.javascript.Parser.bitOrExpr(Parser.java:2447)
at net.sourceforge.htmlunit.corejs.javascript.Parser.andExpr(Parser.java:2436)
at net.sourceforge.htmlunit.corejs.javascript.Parser.orExpr(Parser.java:2425)
at net.sourceforge.htmlunit.corejs.javascript.Parser.condExpr(Parser.java:2389)
at net.sourceforge.htmlunit.corejs.javascript.Parser.assignExpr(Parser.java:2345)
at net.sourceforge.htmlunit.corejs.javascript.Parser.expr(Parser.java:2324)
at net.sourceforge.htmlunit.corejs.javascript.Parser.statementHelper(Parser.java:1282)
at net.sourceforge.htmlunit.corejs.javascript.Parser.statement(Parser.java:1136)
at net.sourceforge.htmlunit.corejs.javascript.Parser.parse(Parser.java:673)
at net.sourceforge.htmlunit.corejs.javascript.Parser.parse(Parser.java:594)
at net.sourceforge.htmlunit.corejs.javascript.Context.compileImpl(Context.java:2601)
at net.sourceforge.htmlunit.corejs.javascript.Context.compileString(Context.java:1583)
at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory$TimeoutContext.compileString(HtmlUnitContextFactory.java:216)
at net.sourceforge.htmlunit.corejs.javascript.Context.compileString(Context.java:1572)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$1.doRun(JavaScriptEngine.java:714)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:877)
... 34 more
Enclosed exception:
net.sourceforge.htmlunit.corejs.javascript.EvaluatorException: illegally formed XML syntax (script in https://mystudentrecord.ucmerced.edu/pls/PROD/xhwschedule.p_selectsubject from (11, 54) to (39, 10)#38)
at com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter.error(StrictErrorReporter.java:65)
at net.sourceforge.htmlunit.corejs.javascript.Parser.addError(Parser.java:260)
at net.sourceforge.htmlunit.corejs.javascript.Parser.addError(Parser.java:232)
at net.sourceforge.htmlunit.corejs.javascript.Parser.addError(Parser.java:228)
at net.sourceforge.htmlunit.corejs.javascript.TokenStream.getNextXMLToken(TokenStream.java:1287)
at net.sourceforge.htmlunit.corejs.javascript.TokenStream.getFirstXMLToken(TokenStream.java:1136)
at net.sourceforge.htmlunit.corejs.javascript.Parser.xmlInitializer(Parser.java:2666)
at net.sourceforge.htmlunit.corejs.javascript.Parser.unaryExpr(Parser.java:2641)
at net.sourceforge.htmlunit.corejs.javascript.Parser.mulExpr(Parser.java:2568)
at net.sourceforge.htmlunit.corejs.javascript.Parser.addExpr(Parser.java:2552)
at net.sourceforge.htmlunit.corejs.javascript.Parser.shiftExpr(Parser.java:2533)
at net.sourceforge.htmlunit.corejs.javascript.Parser.relExpr(Parser.java:2508)
at net.sourceforge.htmlunit.corejs.javascript.Parser.eqExpr(Parser.java:2480)
at net.sourceforge.htmlunit.corejs.javascript.Parser.bitAndExpr(Parser.java:2469)
at net.sourceforge.htmlunit.corejs.javascript.Parser.bitXorExpr(Parser.java:2458)
at net.sourceforge.htmlunit.corejs.javascript.Parser.bitOrExpr(Parser.java:2447)
at net.sourceforge.htmlunit.corejs.javascript.Parser.andExpr(Parser.java:2436)
at net.sourceforge.htmlunit.corejs.javascript.Parser.orExpr(Parser.java:2425)
at net.sourceforge.htmlunit.corejs.javascript.Parser.condExpr(Parser.java:2389)
at net.sourceforge.htmlunit.corejs.javascript.Parser.assignExpr(Parser.java:2345)
at net.sourceforge.htmlunit.corejs.javascript.Parser.expr(Parser.java:2324)
at net.sourceforge.htmlunit.corejs.javascript.Parser.statementHelper(Parser.java:1282)
at net.sourceforge.htmlunit.corejs.javascript.Parser.statement(Parser.java:1136)
at net.sourceforge.htmlunit.corejs.javascript.Parser.parse(Parser.java:673)
at net.sourceforge.htmlunit.corejs.javascript.Parser.parse(Parser.java:594)
at net.sourceforge.htmlunit.corejs.javascript.Context.compileImpl(Context.java:2601)
at net.sourceforge.htmlunit.corejs.javascript.Context.compileString(Context.java:1583)
at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory$TimeoutContext.compileString(HtmlUnitContextFactory.java:216)
at net.sourceforge.htmlunit.corejs.javascript.Context.compileString(Context.java:1572)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$1.doRun(JavaScriptEngine.java:714)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:877)
at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:616)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:534)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.compile(JavaScriptEngine.java:723)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.compile(JavaScriptEngine.java:689)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:735)
at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScript(HtmlPage.java:922)
at com.gargoylesoftware.htmlunit.html.HtmlScript.executeInlineScriptIfNeeded(HtmlScript.java:316)
at com.gargoylesoftware.htmlunit.html.HtmlScript.executeScriptIfNeeded(HtmlScript.java:396)
at com.gargoylesoftware.htmlunit.html.HtmlScript$2.execute(HtmlScript.java:246)
at com.gargoylesoftware.htmlunit.html.HtmlScript.onAllChildrenAddedToPage(HtmlScript.java:267)
at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.endElement(HTMLParser.java:802)
at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.endElement(HTMLParser.java:758)
at net.sourceforge.htmlunit.cyberneko.HTMLTagBalancer.callEndElement(HTMLTagBalancer.java:1194)
at net.sourceforge.htmlunit.cyberneko.HTMLTagBalancer.endElement(HTMLTagBalancer.java:1134)
at net.sourceforge.htmlunit.cyberneko.filters.DefaultFilter.endElement(DefaultFilter.java:221)
at net.sourceforge.htmlunit.cyberneko.filters.NamespaceBinder.endElement(NamespaceBinder.java:314)
at net.sourceforge.htmlunit.cyberneko.HTMLScanner$ContentScanner.scanEndElement(HTMLScanner.java:3179)
at net.sourceforge.htmlunit.cyberneko.HTMLScanner$ContentScanner.scan(HTMLScanner.java:2132)
at net.sourceforge.htmlunit.cyberneko.HTMLScanner.scanDocument(HTMLScanner.java:939)
at net.sourceforge.htmlunit.cyberneko.HTMLConfiguration.parse(HTMLConfiguration.java:452)
at net.sourceforge.htmlunit.cyberneko.HTMLConfiguration.parse(HTMLConfiguration.java:403)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.parse(HTMLParser.java:1001)
at com.gargoylesoftware.htmlunit.html.HTMLParser.parse(HTMLParser.java:250)
at com.gargoylesoftware.htmlunit.html.HTMLParser.parseHtml(HTMLParser.java:196)
at com.gargoylesoftware.htmlunit.DefaultPageCreator.createHtmlPage(DefaultPageCreator.java:267)
at com.gargoylesoftware.htmlunit.DefaultPageCreator.createPage(DefaultPageCreator.java:158)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:531)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:398)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:315)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:466)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:448)
at FuckMerced.main(FuckMerced.java:34)
== CALLING JAVASCRIPT ==
<!-- Hide JavaScript from older browsers
var submitcount=0;
function checkSubmit() {
if (submitcount == 0)
{
submitcount++;
return true;
}
else
{
alert("Your changes have already been submitted.");
return false;
}
}
// End script hiding -->
<script type="text/javascript">
<!-- Hide JavaScript from older browsers
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-31337262-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
// End script hiding -->
======= EXCEPTION END ========
And here is the HTML of the form I am accessing in case the link is being weird:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML lang="en">
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<META HTTP-EQUIV="Pragma" NAME="Cache-Control" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" NAME="Cache-Control" CONTENT="no-cache">
<LINK REL="stylesheet" HREF="/css/web_defaultapp.css" TYPE="text/css">
<LINK REL="stylesheet" HREF="/css/web_defaultprint.css" TYPE="text/css" media="print">
<TITLE>Search Courses by Subject</TITLE>
<META HTTP-EQUIV="Content-Script-Type" NAME="Default_Script_Language" CONTENT="text/javascript">
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!-- Hide JavaScript from older browsers
var submitcount=0;
function checkSubmit() {
if (submitcount == 0)
{
submitcount++;
return true;
}
else
{
alert("Your changes have already been submitted.");
return false;
}
}
// End script hiding -->
<script type="text/javascript">
<!-- Hide JavaScript from older browsers
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-31337262-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
// End script hiding -->
</script>
</HEAD>
<BODY>
<DIV class="headerwrapperdiv">
<TABLE CLASS="plaintable" SUMMARY="This table displays Menu Items and Banner Search textbox."
WIDTH="100%">
<TR>
<TD CLASS="pldefault"></TD>
<TD CLASS="pldefault"><p class="rightaligntext"></p>
</TD></TR></TABLE>
</DIV>
<DIV class="pagetitlediv">
<TABLE CLASS="plaintable" SUMMARY="This table displays title and static header displays."
WIDTH="100%">
<TR>
<TD CLASS="pldefault"><br /><br /><br /></TD>
<TD CLASS="pldefault"> </TD>
<TD CLASS="pldefault"><p class="rightaligntext"></p>
<DIV class="staticheaders">
</div>
</TD></TR><TR>
<TD width="100%" colSpan=3> </TD>
</TR></TABLE>
</DIV>
<DIV class="pagebodydiv">
UC Merced Schedule--Search Courses by Term or Subject <H4>
Interested in UC Online courses offered at other UC campuses? Check out information at UC Online.
<FORM ACTION="xhwschedule.P_ViewSchedule" METHOD="post">
<TABLE CLASS="plaintable" >
<TR>
<TD COLSPAN="2" CLASS="pldefault">Select a Term:</TD>
</TR>
<TR>
<TD CLASS="pldefault">
<INPUT TYPE="radio" NAME="validterm" VALUE="201820" CHECKED>
<TD CLASS="pldefault">Summer Semester 2018 - All Courses</TD>
</TR>
<TR>
<TD CLASS="pldefault">
<INPUT TYPE="radio" NAME="validterm" VALUE="201820 - S6" CHECKED>
<TD CLASS="pldefault">Summer Semester 2018 - First 6-week Summer Session</TD>
</TR>
<TR>
<TD CLASS="pldefault">
<INPUT TYPE="radio" NAME="validterm" VALUE="201820 - S62" CHECKED>
<TD CLASS="pldefault">Summer Semester 2018 - Second 6-week Summer Session</TD>
</TR>
<TR>
<TD CLASS="pldefault">
<INPUT TYPE="radio" NAME="validterm" VALUE="201820 - S8" CHECKED>
<TD CLASS="pldefault">Summer Semester 2018 - 8-week Summer Session</TD>
</TR>
<TR>
<TD CLASS="pldefault">
<INPUT TYPE="radio" NAME="validterm" VALUE="201830" CHECKED>
<TD CLASS="pldefault">Fall Semester 2018</TD>
</TR>
<TR>
<TD CLASS="pldefault">
<INPUT TYPE="radio" NAME="validterm" VALUE="201910" CHECKED>
<TD CLASS="pldefault">Spring Semester 2019</TD>
</TR>
</SELECT>
<BR>
<TR>
<TD CLASS="pldefault"> </TD>
</TR>
<TR>
<TD CLASS="pldefault">Subject:</TD>
<TD CLASS="pldefault">
<SELECT NAME="subjcode">
<OPTION VALUE="ALL">All Subjects
<OPTION VALUE="ANTH">Anthropology
<OPTION VALUE="BEST">Bio Engin Small Scale Tech
<OPTION VALUE="BIOE">Bioengineering
<OPTION VALUE="BIO">Biological Sciences
<OPTION VALUE="CHEM">Chemistry
<OPTION VALUE="CCST">Chicano Chicana Studies
<OPTION VALUE="CHN">Chinese
<OPTION VALUE="COGS">Cognitive Science
<OPTION VALUE="CRS">Community Research and Service
<OPTION VALUE="CSE">Computer Science & Engineering
<OPTION VALUE="CORE">Core
<OPTION VALUE="CRES">Critical Race & Ethnic Studies
<OPTION VALUE="ESS">Earth Systems Science
<OPTION VALUE="ECON">Economics
<OPTION VALUE="EDUC">Education
<OPTION VALUE="EECS">Elect. Engr. & Comp. Sci.
<OPTION VALUE="ENGR">Engineering
<OPTION VALUE="ENG">English
<OPTION VALUE="ENVE">Environmental Engineering
<OPTION VALUE="ES">Environmental Systems (GR)
<OPTION VALUE="FRE">French
<OPTION VALUE="GEOG">Geography
<OPTION VALUE="GASP">Global Arts Studies Program
<OPTION VALUE="HIST">History
<OPTION VALUE="HBIO">Human Biology
<OPTION VALUE="IH">Interdisciplinary Humanities
<OPTION VALUE="JPN">Japanese
<OPTION VALUE="MGMT">Management
<OPTION VALUE="MBSE">Materials & BioMat Sci & Engr
<OPTION VALUE="MSE">Materials Science & Engr
<OPTION VALUE="MATH">Mathematics
<OPTION VALUE="ME">Mechanical Engineering
<OPTION VALUE="MIST">Mgmt of Innov, Sust, and Tech
<OPTION VALUE="NSUS">Nat Sciences Undergrad Studies
<OPTION VALUE="NSED">Natural Sciences Education
<OPTION VALUE="PHIL">Philosophy
<OPTION VALUE="PHYS">Physics
<OPTION VALUE="POLI">Political Science
<OPTION VALUE="PSY">Psychology
<OPTION VALUE="PH">Public Health
<OPTION VALUE="PUBP">Public Policy
<OPTION VALUE="QSB">Quantitative & Systems Biology
<OPTION VALUE="SCS">Social Sciences
<OPTION VALUE="SOC">Sociology
<OPTION VALUE="SPAN">Spanish
<OPTION VALUE="SPRK">Spark
<OPTION VALUE="USTU">Undergraduate Studies
<OPTION VALUE="WCH">World Cultures & History
<OPTION VALUE="WH">World Heritage
<OPTION VALUE="WRI">Writing
</SELECT>
</TR>
<TR>
<TD CLASS="pldefault">
<INPUT TYPE="radio" NAME="openclasses" VALUE="Y" CHECKED>
<TD CLASS="pldefault">Open Classes Only</TD>
</TR>
<TR>
<TD CLASS="pldefault">
<INPUT TYPE="radio" NAME="openclasses" VALUE="N">
<TD CLASS="pldefault">All Classes</TD>
</TR>
</TABLE>
<BR>
<BR>
<INPUT TYPE="submit" VALUE="View Class Schedule">
</FORM>
<!-- ** START OF twbkwbis.P_CloseDoc ** -->
<TABLE CLASS="plaintable" SUMMARY="This is table displays line separator at end of the page."
WIDTH="100%" cellSpacing=0 cellPadding=0 border=0><TR><TD class="bgtabon" width="100%" colSpan=2><IMG SRC="/wtlgifs/web_transparent.gif" ALT="Transparent Image" CLASS="headerImg" TITLE="Transparent Image" NAME="web_transparent" HSPACE=0 VSPACE=0 BORDER=0 HEIGHT=3 WIDTH=10></TD></TR></TABLE>
Skip to top of page
</DIV>
<DIV class="footerbeforediv">
</DIV>
<DIV class="footerafterdiv">
</DIV>
<DIV class="globalafterdiv">
</DIV>
<DIV class="globalfooterdiv">
</DIV>
<DIV class="pagefooterdiv">
<SPAN class="releasetext">Release: 7.3 - Developed by UCM SIS</SPAN>
</DIV>
<DIV class="poweredbydiv">
</DIV>
<DIV class="div1"></DIV>
<DIV class="div2"></DIV>
<DIV class="div3"></DIV>
<DIV class="div4"></DIV>
<DIV class="div5"></DIV>
<DIV class="div6"></DIV>
<div class="banner_copyright"> <br><h5>© 2019 Ellucian Company L.P. and its affiliates.<br></h5></div>
</BODY>
</HTML>
Sorry for the lengthy question, hopefully I can pay it back to the community one day :)
I am a bit out of sync with web programming and getting into HTMLUnit
is a bit more confusing than I thought it would be.
If you like to automate web page these days you need a basic understanding of web technologies at least Html, Javascript and HTTP itself to be able to figure out what to do.
Lets start at the top - with your
lovely error
As a starting point open the page with a real browser and have a look at the web console. You will see the same error there; that means the page you are trying to automate has an error (at least one) and your browser simply ignores this.
HtmlUnit was created as a test tool; because of this it is more picky about errors. You have to disable this.
webClient.getOptions().setThrowExceptionOnScriptError(false);
Next step:
You are trying to access the form on the page
<FORM ACTION="xhwschedule.P_ViewSchedule" METHOD="post">
As the method name implies 'getFormByName()' is able to find forms having the right name attribute - but your form does not have one.
Next step:
<INPUT TYPE="radio" NAME="validterm" VALUE="201910" CHECKED>
As the method name implies 'getElementById("201910")' is able to find elements having the right id attribute - but your radio button does not have one.
And the same for the button.
Below you can find a quick hack that does the work. It might help to read at least the HtmlUnit - Getting Started with HtmlUnit page. There is also the javadoc available with detailed descriptions.
Hope that helps
public static void main(String[] args) throws IOException {
String url = "https://mystudentrecord.ucmerced.edu/pls/PROD/xhwschedule.p_selectsubject";
try (final WebClient webClient = new WebClient()) {
webClient.getOptions().setThrowExceptionOnScriptError(false);
HtmlPage page = webClient.getPage(url);
webClient.waitForBackgroundJavaScript(1000);
page = (HtmlPage) webClient.getCurrentWindow().getEnclosedPage();
final HtmlForm form = page.getForms().get(0);
for (DomElement elem : form.getElementsByTagName("INPUT")) {
if (elem instanceof HtmlRadioButtonInput) {
HtmlRadioButtonInput radioButton = (HtmlRadioButtonInput) elem;
if ("201910".equals(radioButton.getValueAttribute())
|| "N".equals(radioButton.getValueAttribute())) {
radioButton.setChecked(true);
}
}
}
for (DomElement elem : form.getElementsByTagName("INPUT")) {
if (elem instanceof HtmlSubmitInput) {
if ("View Class Schedule".equals(elem.getAttribute("value"))) {
elem.click();
}
}
}
webClient.waitForBackgroundJavaScript(1000);
page = (HtmlPage) webClient.getCurrentWindow().getEnclosedPage();
System.out.println("----------------");
System.out.println(page.asXml());
}
}
Related
This is my jsp page that retrieve the list of items from database using for loop
<%
itemManager mgr = new itemManager();
Item[] items = mgr.getAllItems();
for(int i = 0; i < items.length; i++){
%>
<tr>
<td> <img border="0" src="<%=items[i].getItemImage() %>" width="100" height="100">
</td>
<td>
<%=items[i].getItemName()%>
<input type="text" name="itemID" value="<%=items[i].getItemID()%>">
<br/>
<%=items[i].getItemDesc()%>
<br/>
Start Bid : <%=items[i].getStartBid()%>
<br/>
Buy it now : <%=items[i].getEndBid()%>
<br/>
Bidding close on : <%=items[i].getDuration()%>
<br/>
<input type="submit" value="View">
<%
}
%></table>
This is the jsp page that link to the item you selected previously
<table border="1" align="center">
<%
itemManager mgr = new itemManager();
Item items = mgr.getItem((Integer)session.getAttribute("ITEM_DATA"));
%>
<tr>
<td> <b> <%=items.getItemName() %></b> </td>
</tr>
</table>
This is the servlet to store the session of the selected item id and forward to the correct item jsp page.
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession(true);
int id = Integer.parseInt(request.getParameter("itemID"));
session.setAttribute("ITEM_DATA",id);
RequestDispatcher rd = request.getRequestDispatcher("viewItem.jsp");
rd.forward(request, response);
}
However, after I clicked the view button. It keeps linking to the itemID = 1.
The URL dispalys "/ItemServlet?itemID=1&itemID=2" .
In fact, if I click on itemID=2 the URL should display like this:
"/ItemServlet?itemID=2"
As a result, how can I achieve this? Thanks in advance!
The problem in your code is you are using a single form and dynamically creating Input box inside the form. So all the input box will be having same name. That's why when you submit the form all the input box values are sent as request parameters. I just reduced some part in your code for better under standing. Take this as your code
<form action="item" method="get">
<table>
<%
ItemManager mgr = new ItemManager();
Item[] items = mgr.getAllItems();
for(int i = 0; i < items.length; i++){
%>
<tr>
<td>
<%=items[i].getItemName()%>
<input type="text" name="itemID" value="<%=items[i].getItemId()%>">
<input type="submit" value="View"> </td></tr>
<%
}
%></table>
</form>
When you run this code and if you check the rendered HTML code it will be look
<form action="item" method="get">
<table>
<tr><td>
aaa
<input type="text" name="itemID" value="1">
<input type="submit" value="View"> </td></tr>
<tr>
<td>
bbb
<input type="text" name="itemID" value="2">
<input type="submit" value="View"> </td></tr>
<tr><td>
ccc
<input type="text" name="itemID" value="3">
<input type="submit" value="View"> </td></tr>
</table>
</form>
Here All the Input box having same name as "itemID". As a solution you can create the form inside the for loop, so that while submitting only one Input box value will be sent as request.
Create form inside your for loop like below code.
<table>
<%
ItemManager mgr = new ItemManager();
Item[] items = mgr.getAllItems();
for(int i = 0; i < items.length; i++){
%>
<form action="item" method="get">
<tr>
<td>
<%=items[i].getItemName()%>
<input type="text" name="itemID" value="<%=items[i].getItemId()%>">
<input type="submit" value="View"> </td></tr>
</form>
<%
}
%></table>
Hope This will help you.
change the name of text field dynamically.
And use getQueryString() in servlet to find the itemId name and value. by using EL.I hope this will help you
I have a multi-select Box and I'm doing some javascript to sort the order of the elements in the box. I want to submit the whole array after sorting back to the Java and not just the select items. How can I achieve this?
JSP:
<script type="text/javascript">
$(document).ready(function(){
$("#mup")[0].attachEvent("onclick", moveUpItem);
$("#mdown")[0].attachEvent("onclick", moveDownItem);
});
function moveUpItem(){
$('#list option:selected').each(function(){
$(this).insertBefore($(this).prev());
});
}
function moveDownItem(){
$('#list option:selected').each(function(){
$(this).insertAfter($(this).next());
});
}
</script>
<table width="100%">
<tr>
<td width="50%" align="center">
<h1>DET Column Maintenance</h1>
</td>
</tr>
</table>
<form action="process.det_column_order" method="post" name="detColumnSortorder" >
<table class="data_table">
<tr align="center">
<td>
<select id="list" name="fieldNames" size="35" multiple="multiple" style="width: 250px;">
<c:forEach var="field" items="${detFields}">
<option value="${field.fieldName}">${field.displayName}</option>
</c:forEach>
</select>
</td>
<tr>
<td style="text-align: center;">
<button id="mup">Move Up</button>
<button id="mdown">Move Down</button>
</td>
</tr>
<tr>
<td style="text-align: center;">
<input name="action" type="submit" value="Submit"/>
</td>
</tr>
</table>
</form>
FORM:
private String[] fieldNames;
public String[] getFieldNames() { return this.fieldNames; }
public void setFieldNames(String[] val) { this.fieldNames = val; }
Since forms only submit the selected values, you'll need a little more JS and another form field.
Introduce a hidden form field that will hold the values you care about:
<input type="hidden" name="fieldNamesOrder" id="fieldNamesOrder"/>
And after every click of Move Up/Move Down:
var order = [], sel = document.getElementById("list");
for(var i = 0, len = sel.options.length; i < len; i++) {
order.push(sel.options(i).value);
}
document.getElementById("fieldNamesOrder").value = order.join(",");
Then, on the server side, you can read your ordered field names out of that posted field.
You either need to create a hidden text field in your HTML form that stores the values from each of your options... or you need to programatically select all of the options before the form is submitted.
$('form#injuryReportSubmit').submit(function(){
if(($('form#injuryReportSubmit textarea.description').val().length <=0) || ($('form#injuryReportSubmit select.part').val()== "choose one...") ||($('form#injuryReportSubmit select.type').val() == "choose one...") || ($('form#injuryReportSubmit select.weather').val()=="choose one..."));
{
$('div#errorMessage').show();
return false;
}
});
The code above is used to validate a form before it submits. The problem is the form will not submit even when all the test are false. Can someone help?
the form is on a jsp and looks like
<form id ="injuryReportSubmit" method ="post" action="injuryReportingPage.html" >
<p class ="first" >Date Of Injury</p>
<p class ="second">Date Reported to Manager</p>
<input type="text" id="dateOfInjury" name="dateOfInjury">
<input type="text" id="dateReported" name ="dateReported">
<p class ="weather">Weather</p>
<p class ="injuryType">Injury Type</p>
<p class ="bodyPart">Body Part</p>
<p class ="time">Time Injury Occurred</p>
<p class ="description">Description</p>
<select class ="weather" name="weather">
<%if(InjuryReportController.getWeatherList() != null){ %>
<% for(Weather weather : InjuryReportController.getWeatherList()){%>
<option><%= weather.getWeatherCondition() %></option>
<%} }%>
<option >choose one...</option>
</select>
<select class ="type" name="injuryType">
<%if(InjuryReportController.getInjuryTypeList() != null){ %>
<% for(InjuryType injuryType : InjuryReportController.getInjuryTypeList()){%>
<option><%= injuryType.getInjuryTypeName() %></option>
<%} }%>
<option>choose one...</option>
</select>
<select class ="part" name="bodyPart">
<%if(InjuryReportController.getBodyPartList() != null){ %>
<% for(BodyPart bodyPart : InjuryReportController.getBodyPartList()){%>
<option><%= bodyPart.getBodyPartName() %></option>
<%} }%>
<option >choose one...</option>
</select>
<input type="text" id="timeP" name ="timeOfInjury" value="01:00 AM">
<textarea class ="description" rows="120" cols="670" name="description"></textarea>
<input id ="report" value="Submit Report" type ="submit">
</form>
Remove the semicolon from the end of line 5 of your code.
Currently what you've got is like this:
if (/*your conditions*/); // <- note the semicolon
{
...
return false;
}
This means that the block with the curly braces is not associated with the if statement and so will execute every time. Obviously then returning false every time cancels every submit.
I can`t seem to send the right entity key to servlet in my web app. I am using javascript method to submit the form with the data via a button.
The code is divide into jstl code:
<c:if test="${!empty MOFornecedorList}">
<div id="RightColumn">
<%-- Search Box --%>
<div class="searchform">
<form id="formsearch" name="formsearch" method="post" action="<c:url value='FProcurar'/>">
<span>
<input name="searchBox" class="editbox_search" id="editbox_search" size="80" maxlength="100" value="Pesquisa" type="text" />
</span>
<input name="btnsearch" class="button_search" value="Pesquisa" type="button"/>
</form>
<div class="clr"></div>
<h>Criterio de Pesquisa: </h>
<select name="Type">
<option value="1">ID</option>
<option value="2">Nome</option>
<option value="3">Email</option>
<option value="4">Fax</option>
<option value="5">Endereço</option>
</select>
</div>
<%-- END Search Box --%>
<div class="clr"></div>
<table id="ProductTable" class="detailsTable">
<tr class="header">
<th colspan="9" >Modificar Fornecedor</th>
</tr>
<tr class="tableHeading">
<td>ID</td>
<td>Nome</td>
<td>Endereço</td>
<td>Nº de Celular</td>
<td>Nº de Telefone</td>
<td>Email</td>
<td>Fax</td>
<td>Descrição</td>
<td></td>
</tr>
<c:forEach var="MOForn" items="${MOFornecedorList}" varStatus="iter">
<tr class="${'white'} tableRow">
<td>${MOForn.getFid()}</td>
<td>${MOForn.getFNome()}</td>
<td>${MOForn.getFEndereco()}</td>
<td>${MOForn.getFNCel()}</td>
<td>${MOForn.getFNTel()}</td>
<td>${MOForn.getFEmail()}</td>
<td>${MOForn.getFFax()}</td>
<td>${MOForn.getFDescricao()}</td>
<td>
<form action="<c:url value='FMOb'/>" method="post" name="FModifi">
<input type="hidden"
name="MOForn"
value="${MOForn.fid}">
<input type="button"
value="Modificar" onclick="ModF()">
</form>
</td>
</tr>
</c:forEach>
</table>
</div>
</c:if>
the javascript method
function ModF() {
jConfirm('Modificar o Fornecedor?', 'Confirmação', function(r) {
if (r == true) {
$("form[name='FModifi']").submit();
} else {
return false;
}
});
}
and the controller code:
//Check if fornecedor as been selected
int Fid = Integer.parseInt(request.getParameter("MOForn"));
//Get fornecedor object and set it to variable
Forn = transManager.getEnt(Fid,"fornecedor");
request.setAttribute("Forn",Forn);
PagesInF="FModificar";
request.setAttribute("PagesInF", PagesInF);
userPath = "/Fornecedor";
Now when i test the code the jstl will read 5 records in the item MOFornecedorList in ascending order and a button will be created in the last column.
When the button is pressed for example in the third record the JavaScript method Modf() is invoked and a confirm dialog is shown.
When the user presses the OK button the form FModifi is submitted.
Then the servlet will receive a the request to open the page FMOb where the hidden input for the button pressed will be retrieved and put in a variable type int and some other code will execute.
But the value that the form submit's is the wrong one. ex:
1 - button - MOforn = 1
2 - button - MOforn = 2
3 - button - MOforn = 3 (clicked)
4 - button - MOforn = 4
5 - button - MOforn = 5
The Form should send the value of 3 but sends the value of 5.
So please if anyone as any ideas please share.
You've multiple forms with the same name. Your JS function isn't submitting the form from which it was been called, but it is submitting the last occurrence of the form with that name in the HTML DOM tree.
You need to replace
<input type="button" value="Modificar" onclick="ModF()">
by
<input type="button" value="Modificar" onclick="confirmSubmit(this.form)">
and rewrite the function as follows:
function confirmSubmit(form) {
jConfirm('Modificar o Fornecedor?', 'Confirmação', function(confirmed) {
if (confirmed) {
form.submit();
}
}
}
I'd also suggest to use more self-documenting variable and function names like presented above, so that your code is easier understandable and maintainable in long term (not only for yourself, but also for others, for example the ones on Stackoverflow.com from who you expect an answer when you post a question...)
Still having issues with this problem. Please help if you can.
So I am trying to fix a piece of code using the Geocortex IMF framework. I get an error on line 40 which is basically pulling a null exception. It is a mix of java and html. For some reason I can't seem to find out why the error is pulling up a null. Even if I load the variable with data, it still stops at rs = activeLayer.getRecordset();
Here is the Address Form they fill out and submit
<%# page errorPage="imfError.jsp" %>
<%
/*
Program: afoAddressForm.jsp
Purpose: Displays a page to the user to input address values for a
USAddress type of geocoding query.
Usage: </>
History:
*/
String layerId = request.getParameter("layerid");
String scale = request.getParameter("scale");
if (layerId == null) {
throw new Exception("Missing layerid parameter.");
}
if (scale == null) {
throw new Exception("Missing scale parameter.");
}
%>
<jsp:include page="/imfCopyright.jsp"/>
<html>
<head>
<title></title>
<meta http-equiv="Content-Style-Type" content="text/css">
<link href="../../../imfStyle.css" rel="stylesheet" type="text/css">
<script language="JavaScript" type="text/javascript">
function doNothing() {
}
function submitForm() {
var strStreetName = document.frm.streetName.value;
if (strStreetName == "") {
alert("Please enter street name." );
document.frm.streetNumber.focus();
} else {
document.frm.action = "afoAddress.jsp?streetName="+strStreetName;
document.frm.submit();
}
}
</script>
</head>
<body bgcolor="#FFFFFF" alink="#ff0000" link="#ff0000" vlink="#ff0000">
<form name="frm" action="JavaScript:doNothing()" method="post">
<input type="hidden" name="layerid" value="<%= layerId %>">
<input type="hidden" name="scale" value="<%= scale %>">
<table width="95%" border="0" cellspacing="0" cellpadding="0">
<center>
<tr><td align="left" class="bb11">Zoom To Street<hr></td></tr>
<tr><td height="10"></td></tr>
<tr>
<td align="left" valign="top" class="bn8">
Enter the street name where you wish to centre the map.
If matching streets are found, you will be shown a list
of matching street names for you to choose where to
zoom the map to.
</td>
</tr>
<tr><td height="10"></td></tr>
<tr><td align="center" class="bb8">Street Name</td></tr>
<tr><td align="center" class="bb8"><input name="streetName" size="15" maxLength=40 value=""></td></tr>
<tr><td height="10"></td></tr>
<tr><td align="center" ><input name="btn" type="button" value="Submit" onclick="JavaScript:submitForm()"></td></tr>
<tr><td height="10"></td></tr>
</center>
</table>
</form>
</body>
</html>
Here is what the address form submits to
<%# page import="com.moximedia.aims.*" %>
<%
/*
Program: imfGeocodeUSAddress.jsp
An Internet Mapping Framework (IMF) system script
Copyright 2002 Province of British Columbia - all rights reserved
Purpose: Displays a page of positions matching the address
input by the user for USAddress geocoding styles.
History: 20020610 Cates: original coding
20030724 Cates: send user selection to separate script for labelling.
20040525 Cates: changed frame reference top to parent
20050103 Cates: added type to stylesheet link.
*/
String layerId = request.getParameter("layerid");
String scale = request.getParameter("scale");
String StreetName = request.getParameter("streetName");
AimsMap map = (AimsMap) (session.getAttribute("map"));
AimsFeatureLayer activeLayer = (AimsFeatureLayer) map.getLayers().getLayer(layerId);
AimsRecordset rs = null;
AimsFilter streetFilter = new AimsFilter();
if (activeLayer != null && activeLayer.getFilter()!= null) {
streetFilter = (AimsFilter) activeLayer.getFilter();
}
String query_String="";
if (StreetName == null) {
return;
}else{
StreetName = StreetName.toUpperCase();
query_String = "upper(FENAME) = '" + StreetName +"'";
//query_String = "FENAME like '%" + StreetName +"%'";
streetFilter.setWhereExpression(query_String);
}
// do the query, and whatever we need to do with the data
rs = activeLayer.getRecordset();
rs.clear();
rs.clearFilter();
rs.setMaximumResults(100);
rs.setBufferSize(rs.getMaximumResults());
rs.setFilter(streetFilter);
rs.query();
int count = 0;
rs.moveFirst();
while(!rs.EOF()) {
count++;
rs.moveNext();
}
%>
<jsp:include page="/imfCopyright.jsp"/>
<html>
<head>
<title></title>
<meta http-equiv="Content-Style-Type" content="text/css">
<link href="imfStyle.css" rel="stylesheet" type="text/css">
<script language="JavaScript" type="text/javascript">
function submitForm() {
document.query.submit();
}
</script>
</head>
<body onload="submitForm();">
<form name="query" method="post" action="afoSelectDefaultFind.jsp">
<input type="hidden" name="layerid" value="<%= layerId%>" >
<input type="hidden" name="rec" value="1" >
<input type="hidden" name="total" value="<%=count%>" >
<input type="hidden" name="query_String" value="<%=query_String%>" >
</form>
</body>
</html>
The error is when you hit submit on the form the java.lang.NullPointerException error pops up and put it on line 40 which is rs = activeLayer.getRecordset();. Any help with this would be great.
Well, my guess is that activeLayer is null and then you are calling getRecordset() on a null object reference. Can you try to debug
map.getLayers().getLayer(layerId);
To make sure that it is returning something?
As Chris Thompson points out, the issue is almost certainly that the layer is null. Check that your AXL file has the right layers and layerIds. (Sorry, I know ArcIMS, but I'm not familiar with the Geocortex framework.)
Also, you should add code to check if activeLayer is null and throw a different exception than NullPointerException, since that will make the error that much more obvious to the next programmer that comes along. Interesting that the streetFilter isn't applied if activeLayer is null, so somebody thought about this at some point.