There are two buttons present in a form, and I would like to use GreaseMonkey to automatically click one of the buttons when the page loads. The two buttons are:
<td rowspan="2"><input class="first action" name="first" type="submit" value="First Action" style="font-size: 10pt;" /></td>
<td rowspan="2"><input class="second action" name="second" type="submit" value="Second Action" style="font-size: 10pt;" /></td>
I have tried the two below codes, but neither are working. The first selects the form and tries to submit it, and I get an error that says "No Action Permitted." The second tries to click() the button.
First:
var button = document.getElementById('units_form');
window.location(button);
Second:
var button = document.getElementByClassName('first action');
button.click();
First off, the generic solution:
document.getElementsByName("class_name")[0]
. The last part [0]
gives you the first of all elements that have this classPutting it all together:
var button_element = document.getElementsByName("button-class-name")[0];
var event_object = new MouseEvent('click', {
'view': window,
'bubbles': true,
'cancelable': true
});
button_element.dispatchEvent(event_object);
Also, most sites use jQuery library, for which it boils down to:
$(".class_name").click();
Note that this will fire on all elements with that class name.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.