[英]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.