2016-06-07 91 views
0

我试图点击'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元素?

感谢。

+1

发布正确且完整的html – guradio

+1

调用函数onClick的.select2元素将会非常简单(也是更好的做法),然后在需要的地方调用该函数您代码中的其他地方。 –

+0

我很抱歉有错。 相同的值名称。 差异是onclick元素... – rluisr

回答

2

“如何点击.select2的元素?”

$(".btn[value='select2']").click(); 
+0

谢谢回复。 我很抱歉有错。 相同的值名称。 不同之处在于onclick元素... – rluisr

+0

@ coyotte508感谢您的回复! 我试过'.click('input [value ='select1'] [1 ]')'不起作用... – rluisr

+0

@ coyotte508谢谢你的回复,我试过'.click(“input [type ='submit']”)[1]'但这是错误的,我用了Nightmare。点击'方法不支持'[i]'抱歉...请教我... – rluisr

1

一(明显)的东西:

代码可以是内部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选择器来直接找到你的元素。

幸运地this is possible in CSS3

所以,你可以这样做:

nightmare 
    .goto(page) 
    .click(".btn[value='select1']:nth-of-type(2)") 
    .doStuff.... 

所有这一切都是假设你有一些有价值的select1两个元素,想要点击第二个,如果你的第二个元素实际上的价值select2是那么这会工作:

nightmare 
    .goto(page) 
    .click(".btn[value='select2']") 
    .doStuff....