简体   繁体   English

JavaScript 从 chrome 控制台按名称单击按钮

[英]JavaScript click a button by name from chrome console

I'm trying to click programmatically a button from a web page using chrome console.我正在尝试使用 Chrome 控制台以编程方式单击网页中的按钮。

(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.页面上还有几个按钮具有value= '>' ,所以我想我需要使用名称单击它。 I tried document.querySelector('input[name="BTN_CHOICE4"]').click();我试过document.querySelector('input[name="BTN_CHOICE4"]').click(); and it didn't work.它没有用。 How do I click this button using JS?如何使用 JS 单击此按钮?

If you have ID attribute you can use如果您有 ID 属性,则可以使用

$('#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.但是,如果您按 Id 而不是按名称使用,就好像多个控件具有相同的名称属性和值一样,那么所有这些控件的单击事件都将被调用。

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>

regarding the following HTML:关于以下 HTML:

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

you should be aware of:你应该知道:

  1. the character > should be entity encoded &gt;字符>应该是实体编码的&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/"> .当属性不使用引号( "' )分隔其值时,第一个空格或>之前的所有字符都被解释为值。在您的情况下,这意味着name=BTN_CHOICE5/>转换为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.如果点击按钮的练习是为了提交<form> ,你应该知道像.click()这样的合成事件不会触发默认操作。 May I suggest submitting the form instead?我可以建议提交表格吗?

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

Like Rakesh said, $('#btnId').click();就像 Rakesh 所说的, $('#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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM