2016-06-07 67 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....