简体   繁体   English

如何自动归档输入框并单击搜索按钮?

[英]How can I automatically file an input box and click a search button?

I tried to fill a search input field with some text and hit the search button with this code:我尝试用一些文本填充搜索输入字段,然后使用以下代码点击搜索按钮:

document.getElementsByName('search')[0].value = '1234978';
document.getElementsByClassName('js_search_button')[0].click();

I see my text in the box and the button gets hit.我在框中看到了我的文本,然后按钮被点击了。 But the data from the input box, was not submited.但是输入框的数据,没有提交。

Maybe the website has a protection, so that only keytrokes are accepted.也许该网站有保护措施,因此只接受击键。

To crosscheck this I tried this code:为了对此进行交叉检查,我尝试了以下代码:

var iButton = document.getElementsByClassName('js_search_button')[0];
iButton.addEventListener('click', simulateInput);


function simulateInput() {
  var inp = document.getElementsByName("search")[0];
  var ev = new Event('input');
  
  inp.value =inp.value + '1234978';
  inp.dispatchEvent(ev);
}


Here I get the right result if I manualy click the searchbutton with automaticly filled in the number.如果我手动点击搜索按钮并自动填写数字,我会得到正确的结果。

I it possible to automate the click in this scenario, too?我也可以在这种情况下自动点击吗?

This is not an answer, because of code sample I posted it as answer.这不是答案,因为代码示例我将其发布为答案。

Are you selecting the right element for clicking?您是否选择了正确的点击元素? As you can see I click the buttons programmatically.如您所见,我以编程方式单击按钮。

 function clicked(val) { console.log('button clicked:', val) } document.querySelectorAll(".txt")[0].value = 5; document.querySelectorAll('.btn')[1].click() document.querySelectorAll('.btn')[2].click() document.querySelectorAll('.btn')[0].click() document.getElementsByClassName('btn')[3].click()
 <input class='txt' type='text' /> <button class="btn" onclick="clicked(1)">a</button> <button class="btn" onclick="clicked(2)">b</button> <button class="btn" onclick="clicked(3)">c</button> <button class="btn" onclick="clicked(4)">d</button>

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

相关问题 当用户单击按钮时,如何使其自动在文本字段中输入特定的关键字? - How can I make it automatically input particular keyword into text field when a user click a button? 我可以在单击 input type=search 时向按钮添加事件吗? - Can I add event to the button on click of input type=search? 单击按钮时如何自动滚动到div? - How can I scroll to the div automatically when I click the button? 如何为按钮单击上的搜索框设置动画? - How to animate search box on button click? 单击按钮时如何将多个值传递给对话框 - How can I pass multiple values to dialog box on button click 如何通过单击将按钮的值添加到文本框? - How can I add the value from button to the text box by click? 当我单击下面给出的代码中的计算按钮时,有人可以帮助我如何更改添加到输入框中的伪跨度的颜色 - Can someone help me how to change the color of pseudo span added to the input box when I click on calculate button in code given below 如何在单击时将搜索结果的值分配给输入元素? - How can I assign value of search result to a input element on click? 单击按钮后如何清除输入值? - how can i clean the input value after i click the button? 如何自动单击此按钮? - How do I automatically click this button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM