12

I'm trying to click programmatically a button from a web page using chrome console.

(UPDATED)The code of the button looks like this :

<FORM METHOD=GET ACTION="/xxx/yyy/kkk.jsp" > <INPUT type="hidden" name="page" value="721"> <INPUT type="hidden" name="reqType" value="ReprintO"> <INPUT type="hidden" name="title" value="Reprint Label"> <INPUT type="hidden" name="system" value="reprint"> <td align=right width=25%><input type=submit value='>' name=BTN_CHOICE5/> <td>&nbsp;&nbsp;Reprint Label </form> 

There are a couple more buttons on the page that have value= '>', so I guess I need to click it using name. I tried document.querySelector('input[name="BTN_CHOICE4"]').click(); and it didn't work. How do I click this button using JS?

4
  • 1
    your html snippet does not appear to be valid... Commented Jun 6, 2016 at 21:32
  • You should wrap your attributes values in double quotes. <input type="submit" value=">" name="BTN_CHOICE4" /> Commented Jun 6, 2016 at 21:34
  • I think you can't for security reasons. But then again, the click event should be linked in some cases to a function you could call. Commented Jun 6, 2016 at 21:34
  • @Fabricator: I've just updated it with a source for the button Commented Jun 6, 2016 at 21:39

4 Answers 4

11

If you have ID attribute you can use

$('#btnId').click(); 

Or If you want to go by name, you can use

$('[name="someName"]').click(); 

But it would be good if you use by Id not by name as if multiple controls have same name attribute and value, then all those controls click event will be invoked.

Sign up to request clarification or add additional context in comments.

2 Comments

I don't have an ID. And it needs to be a JS, not jQuery
This approach works if you replace $ with vanilla JavaScript's document.querySelector or another call which will get the Element you're looking for.
5

You can use this

document.getElementsByName("BTN_CHOICE5")[0].click()
<FORM METHOD=GET ACTION="/xxx/yyy/kkk.jsp" > <INPUT type="hidden" name="page" value="721"/> <INPUT type="hidden" name="reqType" value="ReprintO"/> <INPUT type="hidden" name="title" value="Reprint Label"/> <INPUT type="hidden" name="system" value="reprint"/> <td align=right width=25%><input type=submit value='>' name=BTN_CHOICE5 onclick="alert('test')"/></td> <td>&nbsp;&nbsp;Reprint Label</td> </FORM>

Comments

3

regarding the following HTML:

<input type=submit value='>' name=BTN_CHOICE5/> 

you should be aware of:

  1. the character > should be entity encoded &gt;
  2. when an attribute doesn't use quotes (" or ') to delimit its value, all characters until the first space or > are interpreted as the value. In your case this means that name=BTN_CHOICE5/> translates to name="BTN_CJOICE5/">.

If the exercise of clicking the button is meant to submit the <form>, you should know that synthetic events like .click() do not trigger default actions. May I suggest submitting the form instead?

document.querySelector('form').submit() 

1 Comment

Thanks. The website was not written by me, I'm just trying to make a script that will perform a certain task on this website, and this button needs to be clicked(submitted).
3

Like Rakesh said, $('#btnId').click(); works.

Easy mode, though, is to right click the element in the browser, click inspect (or find it in the elements selector, then select it), then simply fire the event on the selected element:

$0.click(); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.