@@ -1239,9 +1239,11 @@ public HtmlElement getSubmitButton(String buttonName) {
12391239 List <HtmlElement > btns = new LinkedList <HtmlElement >();
12401240 if (form != null ) {
12411241 btns .addAll (getForm ().getInputsByName (buttonName ));
1242+ btns .addAll (getForm ().getButtonsByName (buttonName ));
12421243 } else {
12431244 for (HtmlForm f : getCurrentPage ().getForms ()) {
12441245 btns .addAll (f .getInputsByName (buttonName ));
1246+ btns .addAll (f .getButtonsByName (buttonName ));
12451247 }
12461248 }
12471249 for (HtmlElement o : btns ) {
@@ -1276,9 +1278,11 @@ public HtmlElement getResetButton(String buttonName) {
12761278 List <HtmlElement > btns = new LinkedList <HtmlElement >();
12771279 if (form != null ) {
12781280 btns .addAll (getForm ().getInputsByName (buttonName ));
1281+ btns .addAll (getForm ().getButtonsByName (buttonName ));
12791282 } else {
12801283 for (HtmlForm f : getCurrentPage ().getForms ()) {
12811284 btns .addAll (f .getInputsByName (buttonName ));
1285+ btns .addAll (f .getButtonsByName (buttonName ));
12821286 }
12831287 }
12841288 for (HtmlElement o : btns ) {
@@ -1314,9 +1318,11 @@ public HtmlElement getSubmitButton(String buttonName,
13141318 List <HtmlElement > btns = new LinkedList <HtmlElement >();
13151319 if (form != null ) {
13161320 btns .addAll (getForm ().getInputsByName (buttonName ));
1321+ btns .addAll (getForm ().getButtonsByName (buttonName ));
13171322 } else {
13181323 for (HtmlForm f : getCurrentPage ().getForms ()) {
13191324 btns .addAll (f .getInputsByName (buttonName ));
1325+ btns .addAll (f .getButtonsByName (buttonName ));
13201326 }
13211327 }
13221328 for (HtmlElement o : btns ) {
@@ -1351,16 +1357,54 @@ public HtmlElement getSubmitButton(String buttonName,
13511357 }
13521358 return null ;
13531359 }
1360+
1361+ private HtmlElement getSubmitButton () {
1362+ List <HtmlElement > btns = new LinkedList <HtmlElement >();
1363+ if (form != null ) {
1364+ btns .addAll (getForm ().getElementsByAttribute ("input" , "type" , "submit" ));
1365+ btns .addAll (getForm ().getElementsByAttribute ("input" , "type" , "image" ));
1366+ btns .addAll (getForm ().getElementsByAttribute ("button" , "type" , "submit" ));
1367+ } else {
1368+ for (HtmlForm f : getCurrentPage ().getForms ()) {
1369+ btns .addAll (f .getElementsByAttribute ("input" , "type" , "submit" ));
1370+ btns .addAll (f .getElementsByAttribute ("input" , "type" , "image" ));
1371+ btns .addAll (f .getElementsByAttribute ("button" , "type" , "submit" ));
1372+ }
1373+ }
1374+ for (HtmlElement o : btns ) {
1375+ if (o instanceof HtmlSubmitInput ) {
1376+ HtmlSubmitInput btn = (HtmlSubmitInput ) o ;
1377+ if (form == null ) {
1378+ form = btn .getEnclosingFormOrDie ();
1379+ }
1380+ return btn ;
1381+ }
1382+ if (o instanceof HtmlImageInput ) {
1383+ HtmlImageInput btn = (HtmlImageInput ) o ;
1384+ if (form == null ) {
1385+ form = btn .getEnclosingFormOrDie ();
1386+ }
1387+ return btn ;
1388+ }
1389+ if (o instanceof HtmlButton ) {
1390+ HtmlButton btn = (HtmlButton ) o ;
1391+ if (btn .getTypeAttribute ().equals ("submit" )) {
1392+ if (form == null ) {
1393+ form = btn .getEnclosingFormOrDie ();
1394+ }
1395+ return btn ;
1396+ }
1397+ }
1398+ }
1399+ return null ;
1400+ }
13541401
13551402 /**
13561403 * {@inheritDoc}
13571404 */
13581405 @ Override
13591406 public boolean hasSubmitButton () {
1360- final HtmlForm htmlForm = getForm ();
1361- List <?> l = htmlForm .getByXPath ("//input[@type='submit' or @type='image']" );
1362- List <?> l2 = htmlForm .getByXPath ("//button[@type='submit']" );
1363- return (l .size () > 0 || l2 .size () > 0 );
1407+ return getSubmitButton () != null ;
13641408 }
13651409
13661410 /**
@@ -1589,36 +1633,20 @@ public boolean hasTable(String tableSummaryNameOrId) {
15891633 */
15901634 @ Override
15911635 public void submit () {
1636+ HtmlElement btn = getSubmitButton ();
1637+ if (btn == null ) {
1638+ throw new RuntimeException ("No submit button found in current form." );
1639+ }
15921640 try {
1593- Object [] inpt = getForm ().getHtmlElementsByTagName ("input" )
1594- .toArray ();
1595- for (int i = 0 ; i < inpt .length ; i ++) {
1596- if (inpt [i ] instanceof HtmlSubmitInput ) {
1597- ((HtmlSubmitInput ) inpt [i ]).click ();
1598- return ;
1599- }
1600- if (inpt [i ] instanceof HtmlImageInput ) {
1601- ((HtmlImageInput ) inpt [i ]).click ();
1602- return ;
1603- }
1604- if (inpt [i ] instanceof HtmlButton
1605- && ((HtmlButton ) inpt [i ]).getTypeAttribute ().equals (
1606- "submit" )) {
1607- ((HtmlButton ) inpt [i ]).click ();
1608- return ;
1609- }
1610- }
1611-
1641+ btn .click ();
16121642 } catch (FailingHttpStatusCodeException e ) {
16131643 throw new TestingEngineResponseException (
1614- e .getStatusCode (), e );
1644+ e .getStatusCode (), e );
16151645 } catch (IOException e ) {
16161646 throw new RuntimeException (
1617- "HtmlUnit Error submitting form using default submit button, "
1618- + "check that form has single submit button, otherwise use submit(name): \n " ,
1619- e );
1647+ "HtmlUnit Error submitting form using default submit button, "
1648+ + "check that form has single submit button, otherwise use submit(name): \n " , e );
16201649 }
1621- throw new RuntimeException ("No submit button found in current form." );
16221650 }
16231651
16241652 /**
@@ -1629,37 +1657,19 @@ public void submit() {
16291657 */
16301658 @ Override
16311659 public void submit (String buttonName ) {
1632- List <HtmlElement > l = new LinkedList <HtmlElement >();
1633- l .addAll (getForm ().getInputsByName (buttonName ));
1634- l .addAll (getForm ().getButtonsByName (buttonName ));
1660+ HtmlElement btn = getSubmitButton (buttonName );
1661+ if (btn == null ) {
1662+ throw new RuntimeException ("No submit button found in current form." );
1663+ }
16351664 try {
1636- for (HtmlElement o : l ) {
1637- if (o instanceof HtmlSubmitInput ) {
1638- HtmlSubmitInput inpt = (HtmlSubmitInput ) o ;
1639- inpt .click ();
1640- return ;
1641- }
1642- if (o instanceof HtmlImageInput ) {
1643- HtmlImageInput inpt = (HtmlImageInput ) o ;
1644- inpt .click ();
1645- return ;
1646- }
1647- if (o instanceof HtmlButton ) {
1648- HtmlButton inpt = (HtmlButton ) o ;
1649- if (inpt .getTypeAttribute ().equals ("submit" )) {
1650- inpt .click ();
1651- return ;
1652- }
1653- }
1654- }
1665+ btn .click ();
16551666 } catch (FailingHttpStatusCodeException e ) {
16561667 throw new TestingEngineResponseException (
1657- e .getStatusCode (), e );
1668+ e .getStatusCode (), e );
16581669 } catch (IOException e ) {
16591670 throw new RuntimeException (
1660- "HtmlUnit Error submitting form using default submit button" , e );
1671+ "HtmlUnit Error submitting form using default submit button" , e );
16611672 }
1662- throw new RuntimeException ("No submit button found in current form." );
16631673 }
16641674
16651675 /**
0 commit comments