[js] update implementation for submitting forms
@@ -2765,13 +2765,17 @@ class WebElement {
* when the form has been submitted.
*/
submit() {
- const form = this.findElement({ xpath: './ancestor-or-self::form' })
- this.driver_.executeScript(
- "var e = arguments[0].ownerDocument.createEvent('Event');" +
- "e.initEvent('submit', true, true);" +
- 'if (arguments[0].dispatchEvent(e)) { arguments[0].submit() }',
- form
- )
+ const script = "var form = arguments[0];\n" +
+ "while (form.nodeName != \"FORM\" && form.parentNode) {\n" +
+ " form = form.parentNode;\n" +
+ "}\n" +
+ "if (!form) { throw Error('Unable to find containing form element'); }\n" +
+ "if (!form.ownerDocument) { throw Error('Unable to find owning document'); }\n" +
+ "var e = form.ownerDocument.createEvent('Event');\n" +
+ "e.initEvent('submit', true, true);\n" +
+ "if (form.dispatchEvent(e)) { HTMLFormElement.prototype.submit.call(form) }\n";
+
+ this.driver_.executeScript(script, this);
}
/**
