噩梦。点击事件onclick
问题描述:
我试图点击'onclick'元素。目标网站的来源是噩梦。点击事件onclick
<td class="td-03">
<p class="td-03">hogehoge</p>
</td>
<td class="td-04">
<p class="td-04">
<input class="btn btn-sm btn-success" onclick="this.form.f_comp_no.value=117111;this.form.f_acnt_no.value=431174;" type="submit" value="select1">
</p>
</td>
</tr>
<tr>
<td class="td-01">
<p class="td-01">2</p>
</td>
<td class="td-02">
<p class="td-02">162343</p>
</td>
<td class="td-03">
<p class="td-03">foofoo</p>
</td>
<td class="td-04">
<p class="td-04">
<input class="btn btn-sm btn-success" onclick="this.form.f_comp_no.value=11143;this.form.f_acnt_no.value=423271;" type="submit" value="select2">
</p>
</td>
</tr>
我想点击'select2'。
我尝试这样做,但它不工作:
.click('[input[onclick=this.form.f_comp_no.value=11143;this.form.f_acnt_no.value=423271;]')
如何点击.select2
元素?
感谢。
答
“如何点击.select2的元素?”
$(".btn[value='select2']").click();
答
一(明显)的东西:
代码可以是内部evaluate()
:
nightmare
.goto(page)
.evaluate(() => {
/* Inside browser context, no access to variables from the nodejs
program unless they are passed as an argument */
/* using jQuery if it's loaded in the page */
$(".btn[value='select1']")[1].click();
/* using native JS api */
document.querySelectorAll("input[value='select1']")[1].click();
/* In both cases the [1] refers to the second element matching the css
selector */
/* If returning a value here, will be the parameter of the callback
function of the next .then() */
}).doStuff....
看看在evaluate()
文档:如何传递参数给它,如何拿回值从中。
如果你想从nightmare
本身做click()
,它更复杂,你必须找到精确的CSS选择器来直接找到你的元素。
所以,你可以这样做:
nightmare
.goto(page)
.click(".btn[value='select1']:nth-of-type(2)")
.doStuff....
所有这一切都是假设你有一些有价值的select1
两个元素,想要点击第二个,如果你的第二个元素实际上的价值select2
是那么这会工作:
nightmare
.goto(page)
.click(".btn[value='select2']")
.doStuff....
发布正确且完整的html – guradio
调用函数onClick的.select2元素将会非常简单(也是更好的做法),然后在需要的地方调用该函数您代码中的其他地方。 –
我很抱歉有错。 相同的值名称。 差异是onclick元素... – rluisr