如何在 Javascript 中的 html 编码中正确使用 .click on this select 类型?
How do I correctly use .click on this select type from html coding in Javascript?
<select name="s" id="s">
<option value="64752">Small</option>
<option value="64753">Medium</option>
</select>
这是来自网站的 select 类型的 HTML 代码
.realClick('select#s', {x: 1, y: 1})
.insert('input#s', size)
这是我试过的部分编码,我收到错误 "cannot find element: "select#s"
我不太确定还能尝试什么 我对自动化还很陌生。我将 PhantomJS 与 real-click PhantomJS 一起使用。任何想法都会很棒。谢谢
我认为您无法使用 select 上的点击。我记得当我经常使用 PhantomJS 时,我必须设置我试图选择的选项的 selected 索引:
page.evaluate(function() {
// Set the selected index
var select = document.getElementById('s');
select.selectedIndex = 1;
// Trigger a change event
var onChange = document.createEvent('HTMLEvents');
onChange.initEvent('change', false, true);
select.dispatchEvent(onChange);
});
这应该select选项Medium
。
More info 关于 onChange
参数。
<select name="s" id="s">
<option value="64752">Small</option>
<option value="64753">Medium</option>
</select>
这是来自网站的 select 类型的 HTML 代码
.realClick('select#s', {x: 1, y: 1})
.insert('input#s', size)
这是我试过的部分编码,我收到错误 "cannot find element: "select#s"
我不太确定还能尝试什么 我对自动化还很陌生。我将 PhantomJS 与 real-click PhantomJS 一起使用。任何想法都会很棒。谢谢
我认为您无法使用 select 上的点击。我记得当我经常使用 PhantomJS 时,我必须设置我试图选择的选项的 selected 索引:
page.evaluate(function() {
// Set the selected index
var select = document.getElementById('s');
select.selectedIndex = 1;
// Trigger a change event
var onChange = document.createEvent('HTMLEvents');
onChange.initEvent('change', false, true);
select.dispatchEvent(onChange);
});
这应该select选项Medium
。
More info 关于 onChange
参数。