khe*_*hex 5 javascript html-select click phantomjs
有一个testkrok.org.ua页面,其中包含一致的参数选择.因此,我需要在5个相互依赖的选择框的每个选项上创建一系列5次点击.
document.querySelector('select.se1')[3]
document.querySelector('select.se2')[1]
document.querySelector('select.se3')[1]
document.querySelector('select.se4')[1]
document.querySelector('select.se5')[3]
Run Code Online (Sandbox Code Playgroud)
重定向到带有测试的页面.
但是在第一次单击后拍摄的快照上没有出现第二个面板?也许我没有击中元素?
var page = require('webpage').create();
page.open('https://testkrok.org.ua', function(status) {
console.log("Status: " + status);
if(status === "success") {
page.evaluate(function() {
var theEvent = document.createEvent("MouseEvent");
theEvent.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
var element = document.querySelector('select.se1')[3];
element.dispatchEvent(theEvent);
});
}
setTimeout( function() {
page.render('snapshot.png');
phantom.exit()
}, 5000);
});
Run Code Online (Sandbox Code Playgroud)
您无法在选择框的选项上单击(触发单击事件).您需要更改所选选项,然后触发更改事件.例如:
var sel = document.querySelector('select.se1');
sel.selectedIndex = 2;
var event = new UIEvent("change", {
"view": window,
"bubbles": true,
"cancelable": true
});
sel.dispatchEvent(event);
Run Code Online (Sandbox Code Playgroud)
您可以将其打包在一个函数中
function selectOption(selector, optionIndex) {
page.evaluate(function(selector, optionIndex){
var sel = document.querySelector(selector);
sel.selectedIndex = optionIndex;
var event = new UIEvent("change", {
"view": window,
"bubbles": true,
"cancelable": true
});
sel.dispatchEvent(event);
}, selector, optionIndex);
}
Run Code Online (Sandbox Code Playgroud)
然后你可以一个接一个地调用它
selectOption("select.se1", 2);
selectOption("select.se2", 0);
selectOption("select.se3", 0);
...
Run Code Online (Sandbox Code Playgroud)
你明白了.如果onChange选择框的事件需要远程数据,例如通过AJAX,那么您将需要在两次调用之间等待.使用静态等待时间(参见下面的示例)或使用waitFor().
setTimeout(function(){
selectOption("select.se1", 2);
}, 1000);
setTimeout(function(){
selectOption("select.se2", 0);
}, 2000);
setTimeout(function(){
selectOption("select.se3", 0);
}, 3000);
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3016 次 |
| 最近记录: |