JavaScript click a button by name from chrome console

后端 未结 4 536
刺人心
刺人心 2021-01-01 04:30

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

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

<         


        
相关标签:
4条回答
  • 2021-01-01 04:47

    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()
    
    0 讨论(0)
  • 2021-01-01 04:52

    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>

    0 讨论(0)
  • 2021-01-01 05:02

    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.

    0 讨论(0)
  • 2021-01-01 05:10

    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();
    
    0 讨论(0)
提交回复
热议问题