[新手求助] puppeteer page.$eval 判断与 page.click 执行
发布于 8 个月前 作者 MoroseDog 2500 次浏览 来自 问答

前言

小白刚踏入Node.js很多还不熟悉,请各位大大多多包涵。

提问

目前page.click是直接针对child位置来抓取,但是如果位置变动,就会点击错误。 请问该如何判断Reload按钮是在IESC底下的按钮,并且Click点击。

main.js

const puppeteer = require('puppeteer');

(async () => {
 const browser = await puppeteer.launch();
 const page = await browser.newPage();
 await page.goto('http://admin:admin@127.0.0.1:8080/manager');

 await page.click('body > table:nth-child(9) > tbody > tr:nth-child(5) > td:nth-child(6) > form:nth-child(3) > small > input[type=submit]');

 // console.log(await page.content());

 await browser.close();
})();

Tomcat manager index.html image

image.png

Tomcat manager index.html code

<html><head>
<style>
H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}
  table {
    width: 100%;
  }
  td.page-title {
    text-align: center;
    vertical-align: top;
    font-family:sans-serif,Tahoma,Arial;
    font-weight: bold;
    background: white;
    color: black;
  }
  td.title {
    text-align: left;
    vertical-align: top;
    font-family:sans-serif,Tahoma,Arial;
    font-style:italic;
    font-weight: bold;
    background: #D2A41C;
  }
  td.header-left {
    text-align: left;
    vertical-align: top;
    font-family:sans-serif,Tahoma,Arial;
    font-weight: bold;
    background: #FFDC75;
  }
  td.header-center {
    text-align: center;
    vertical-align: top;
    font-family:sans-serif,Tahoma,Arial;
    font-weight: bold;
    background: #FFDC75;
  }
  td.row-left {
    text-align: left;
    vertical-align: middle;
    font-family:sans-serif,Tahoma,Arial;
    color: black;
  }
  td.row-center {
    text-align: center;
    vertical-align: middle;
    font-family:sans-serif,Tahoma,Arial;
    color: black;
  }
  td.row-right {
    text-align: right;
    vertical-align: middle;
    font-family:sans-serif,Tahoma,Arial;
    color: black;
  }
  TH {
    text-align: center;
    vertical-align: top;
    font-family:sans-serif,Tahoma,Arial;
    font-weight: bold;
    background: #FFDC75;
  }
  TD {
    text-align: center;
    vertical-align: middle;
    font-family:sans-serif,Tahoma,Arial;
    color: black;
  }
  form {
    margin: 1;
  }
  form.inline {
    display: inline;
  }
</style>
<title>/manager</title>
</head>

<body bgcolor="#FFFFFF">

<table cellspacing="4" border="0">
 <tbody><tr>
  <td colspan="2">
   <a href="http://www.apache.org/">
    <img border="0" alt="The Apache Software Foundation" align="left" src="/manager/images/asf-logo.gif">
   </a>
   <a href="http://tomcat.apache.org/">
    <img border="0" alt="The Tomcat Servlet/JSP Container" align="right" src="/manager/images/tomcat.gif">
   </a>
  </td>
 </tr>
</tbody></table>
<hr size="1" noshade="noshade">
<table cellspacing="4" border="0">
 <tbody><tr>
  <td class="page-title" bordercolor="#000000" align="left" nowrap="">
   <font size="+2">Tomcat Web Application Manager</font>
  </td>
 </tr>
</tbody></table>
<br>

<table border="1" cellspacing="0" cellpadding="3">
 <tbody><tr>
  <td class="row-left" width="10%"><small><strong>Message:</strong></small>&nbsp;</td>
  <td class="row-left"><pre>OK</pre></td>
 </tr>
</tbody></table>
<br>

<table border="1" cellspacing="0" cellpadding="3">
<tbody><tr>
 <td colspan="4" class="title">Manager</td>
</tr>
 <tr>
  <td class="row-left"><a href="/manager/html/list?org.apache.catalina.filters.CSRF_NONCE=AA7CC0E4CCA8F6D7F026B2509DE78A72">List Applications</a></td>
  <td class="row-center"><a href="/manager/../docs/html-manager-howto.html?org.apache.catalina.filters.CSRF_NONCE=AA7CC0E4CCA8F6D7F026B2509DE78A72">HTML Manager Help</a></td>
  <td class="row-center"><a href="/manager/../docs/manager-howto.html?org.apache.catalina.filters.CSRF_NONCE=AA7CC0E4CCA8F6D7F026B2509DE78A72">Manager Help</a></td>
  <td class="row-right"><a href="/manager/status?org.apache.catalina.filters.CSRF_NONCE=AA7CC0E4CCA8F6D7F026B2509DE78A72">Server Status</a></td>
 </tr>
</tbody></table>
<br>

<table border="1" cellspacing="0" cellpadding="3">
<tbody><tr>
 <td colspan="6" class="title">Applications</td>
</tr>
<tr>
 <td class="header-left"><small>Path</small></td>
 <td class="header-left"><small>Version</small></td>
 <td class="header-center"><small>Display Name</small></td>
 <td class="header-center"><small>Running</small></td>
 <td class="header-left"><small>Sessions</small></td>
 <td class="header-left"><small>Commands</small></td>
</tr>
<tr>
 <td class="row-left" bgcolor="#FFFFFF" rowspan="2"><small><a href="/">/</a></small></td>
 <td class="row-left" bgcolor="#FFFFFF" rowspan="2"><small><i>None specified</i></small></td>
 <td class="row-left" bgcolor="#FFFFFF" rowspan="2"><small>Welcome to Tomcat</small></td>
 <td class="row-center" bgcolor="#FFFFFF" rowspan="2"><small>true</small></td>
 <td class="row-center" bgcolor="#FFFFFF" rowspan="2"><small><a href="/manager/html/sessions?path=/&amp;org.apache.catalina.filters.CSRF_NONCE=AA7CC0E4CCA8F6D7F026B2509DE78A72">0</a></small></td>
 <td class="row-left" bgcolor="#FFFFFF">
  &nbsp;<small>Start</small>&nbsp;
  <form class="inline" method="POST" action="/manager/html/stop?path=/&amp;org.apache.catalina.filters.CSRF_NONCE=AA7CC0E4CCA8F6D7F026B2509DE78A72">  <small><input type="submit" value="Stop"></small>  </form>
  <form class="inline" method="POST" action="/manager/html/reload?path=/&amp;org.apache.catalina.filters.CSRF_NONCE=AA7CC0E4CCA8F6D7F026B2509DE78A72">  <small><input type="submit" value="Reload"></small>  </form>
  <form class="inline" method="POST" action="/manager/html/undeploy?path=/&amp;org.apache.catalina.filters.CSRF_NONCE=AA7CC0E4CCA8F6D7F026B2509DE78A72">  <small><input type="submit" value="Undeploy"></small>  </form>
 </td>
 </tr><tr>
 <td class="row-left" bgcolor="#FFFFFF">
  <form method="POST" action="/manager/html/expire?path=/&amp;org.apache.catalina.filters.CSRF_NONCE=AA7CC0E4CCA8F6D7F026B2509DE78A72">
  <small>
  &nbsp;<input type="submit" value="Expire sessions">&nbsp;with idle ≥&nbsp;<input type="text" name="idle" size="5" value="30">&nbsp;minutes&nbsp;
  </small>
  </form>
 </td>
</tr>
<tr>
 <td class="row-left" bgcolor="#C3F3C3" rowspan="2"><small><a href="/IESC/">/IESC</a></small></td>
 <td class="row-left" bgcolor="#C3F3C3" rowspan="2"><small><i>None specified</i></small></td>
 <td class="row-left" bgcolor="#C3F3C3" rowspan="2"><small>Interfly Enterprise Service Center</small></td>
 <td class="row-center" bgcolor="#C3F3C3" rowspan="2"><small>true</small></td>
 <td class="row-center" bgcolor="#C3F3C3" rowspan="2"><small><a href="/manager/html/sessions?path=/IESC&amp;org.apache.catalina.filters.CSRF_NONCE=AA7CC0E4CCA8F6D7F026B2509DE78A72">0</a></small></td>
 <td class="row-left" bgcolor="#C3F3C3">
  &nbsp;<small>Start</small>&nbsp;
  <form class="inline" method="POST" action="/manager/html/stop?path=/IESC&amp;org.apache.catalina.filters.CSRF_NONCE=AA7CC0E4CCA8F6D7F026B2509DE78A72">  <small><input type="submit" value="Stop"></small>  </form>
  <form class="inline" method="POST" action="/manager/html/reload?path=/IESC&amp;org.apache.catalina.filters.CSRF_NONCE=AA7CC0E4CCA8F6D7F026B2509DE78A72">  <small><input type="submit" value="Reload"></small>  </form>
  <form class="inline" method="POST" action="/manager/html/undeploy?path=/IESC&amp;org.apache.catalina.filters.CSRF_NONCE=AA7CC0E4CCA8F6D7F026B2509DE78A72">  <small><input type="submit" value="Undeploy"></small>  </form>
 </td>
 </tr><tr>
 <td class="row-left" bgcolor="#C3F3C3">
  <form method="POST" action="/manager/html/expire?path=/IESC&amp;org.apache.catalina.filters.CSRF_NONCE=AA7CC0E4CCA8F6D7F026B2509DE78A72">
  <small>
  &nbsp;<input type="submit" value="Expire sessions">&nbsp;with idle ≥&nbsp;<input type="text" name="idle" size="5" value="30">&nbsp;minutes&nbsp;
  </small>
  </form>
 </td>
</tr>
<tr>
 <td class="row-left" bgcolor="#FFFFFF" rowspan="2"><small><a href="/LineBot/">/LineBot</a></small></td>
 <td class="row-left" bgcolor="#FFFFFF" rowspan="2"><small><i>None specified</i></small></td>
 <td class="row-left" bgcolor="#FFFFFF" rowspan="2"><small>LineBot</small></td>
 <td class="row-center" bgcolor="#FFFFFF" rowspan="2"><small>true</small></td>
 <td class="row-center" bgcolor="#FFFFFF" rowspan="2"><small><a href="/manager/html/sessions?path=/LineBot&amp;org.apache.catalina.filters.CSRF_NONCE=AA7CC0E4CCA8F6D7F026B2509DE78A72">0</a></small></td>
 <td class="row-left" bgcolor="#FFFFFF">
  &nbsp;<small>Start</small>&nbsp;
  <form class="inline" method="POST" action="/manager/html/stop?path=/LineBot&amp;org.apache.catalina.filters.CSRF_NONCE=AA7CC0E4CCA8F6D7F026B2509DE78A72">  <small><input type="submit" value="Stop"></small>  </form>
  <form class="inline" method="POST" action="/manager/html/reload?path=/LineBot&amp;org.apache.catalina.filters.CSRF_NONCE=AA7CC0E4CCA8F6D7F026B2509DE78A72">  <small><input type="submit" value="Reload"></small>  </form>
  <form class="inline" method="POST" action="/manager/html/undeploy?path=/LineBot&amp;org.apache.catalina.filters.CSRF_NONCE=AA7CC0E4CCA8F6D7F026B2509DE78A72">  <small><input type="submit" value="Undeploy"></small>  </form>
 </td>
 </tr><tr>
 <td class="row-left" bgcolor="#FFFFFF">
  <form method="POST" action="/manager/html/expire?path=/LineBot&amp;org.apache.catalina.filters.CSRF_NONCE=AA7CC0E4CCA8F6D7F026B2509DE78A72">
  <small>
  &nbsp;<input type="submit" value="Expire sessions">&nbsp;with idle ≥&nbsp;<input type="text" name="idle" size="5" value="30">&nbsp;minutes&nbsp;
  </small>
  </form>
 </td>
</tr>
<tr>
 <td class="row-left" bgcolor="#C3F3C3" rowspan="2"><small><a href="/manager/">/manager</a></small></td>
 <td class="row-left" bgcolor="#C3F3C3" rowspan="2"><small><i>None specified</i></small></td>
 <td class="row-left" bgcolor="#C3F3C3" rowspan="2"><small>Tomcat Manager Application</small></td>
 <td class="row-center" bgcolor="#C3F3C3" rowspan="2"><small>true</small></td>
 <td class="row-center" bgcolor="#C3F3C3" rowspan="2"><small><a href="/manager/html/sessions?path=/manager&amp;org.apache.catalina.filters.CSRF_NONCE=AA7CC0E4CCA8F6D7F026B2509DE78A72">3</a></small></td>
 <td class="row-left" bgcolor="#C3F3C3">
  <small>
  &nbsp;Start&nbsp;
  &nbsp;Stop&nbsp;
  &nbsp;Reload&nbsp;
  &nbsp;Undeploy&nbsp;
  </small>
 </td>
</tr><tr>
 <td class="row-left" bgcolor="#C3F3C3">
  <form method="POST" action="/manager/html/expire?path=/manager&amp;org.apache.catalina.filters.CSRF_NONCE=AA7CC0E4CCA8F6D7F026B2509DE78A72">
  <small>
  &nbsp;<input type="submit" value="Expire sessions">&nbsp;with idle ≥&nbsp;<input type="text" name="idle" size="5" value="30">&nbsp;minutes&nbsp;
  </small>
  </form>
 </td>
</tr>
<tr>
 <td class="row-left" bgcolor="#FFFFFF" rowspan="2"><small><a href="/oas/">/oas</a></small></td>
 <td class="row-left" bgcolor="#FFFFFF" rowspan="2"><small><i>None specified</i></small></td>
 <td class="row-left" bgcolor="#FFFFFF" rowspan="2"><small>oas</small></td>
 <td class="row-center" bgcolor="#FFFFFF" rowspan="2"><small>true</small></td>
 <td class="row-center" bgcolor="#FFFFFF" rowspan="2"><small><a href="/manager/html/sessions?path=/oas&amp;org.apache.catalina.filters.CSRF_NONCE=AA7CC0E4CCA8F6D7F026B2509DE78A72">14</a></small></td>
 <td class="row-left" bgcolor="#FFFFFF">
  &nbsp;<small>Start</small>&nbsp;
  <form class="inline" method="POST" action="/manager/html/stop?path=/oas&amp;org.apache.catalina.filters.CSRF_NONCE=AA7CC0E4CCA8F6D7F026B2509DE78A72">  <small><input type="submit" value="Stop"></small>  </form>
  <form class="inline" method="POST" action="/manager/html/reload?path=/oas&amp;org.apache.catalina.filters.CSRF_NONCE=AA7CC0E4CCA8F6D7F026B2509DE78A72">  <small><input type="submit" value="Reload"></small>  </form>
  <form class="inline" method="POST" action="/manager/html/undeploy?path=/oas&amp;org.apache.catalina.filters.CSRF_NONCE=AA7CC0E4CCA8F6D7F026B2509DE78A72">  <small><input type="submit" value="Undeploy"></small>  </form>
 </td>
 </tr><tr>
 <td class="row-left" bgcolor="#FFFFFF">
  <form method="POST" action="/manager/html/expire?path=/oas&amp;org.apache.catalina.filters.CSRF_NONCE=AA7CC0E4CCA8F6D7F026B2509DE78A72">
  <small>
  &nbsp;<input type="submit" value="Expire sessions">&nbsp;with idle ≥&nbsp;<input type="text" name="idle" size="5" value="30">&nbsp;minutes&nbsp;
  </small>
  </form>
 </td>
</tr>
</tbody></table>
<br>
<table border="1" cellspacing="0" cellpadding="3">
<tbody><tr>
 <td colspan="2" class="title">Deploy</td>
</tr>
<tr>
 <td colspan="2" class="header-left"><small>Deploy directory or WAR file located on server</small></td>
</tr>
<tr>
 <td colspan="2">
<form method="post" action="/manager/html/deploy?org.apache.catalina.filters.CSRF_NONCE=AA7CC0E4CCA8F6D7F026B2509DE78A72">
<table cellspacing="0" cellpadding="3">
<tbody><tr>
 <td class="row-right">
  <small>Context Path (required):</small>
 </td>
 <td class="row-left">
  <input type="text" name="deployPath" size="20">
 </td>
</tr>
<tr>
 <td class="row-right">
  <small>XML Configuration file URL:</small>
 </td>
 <td class="row-left">
  <input type="text" name="deployConfig" size="20">
 </td>
</tr>
<tr>
 <td class="row-right">
  <small>WAR or Directory URL:</small>
 </td>
 <td class="row-left">
  <input type="text" name="deployWar" size="40">
 </td>
</tr>
<tr>
 <td class="row-right">
  &nbsp;
 </td>
 <td class="row-left">
  <input type="submit" value="Deploy">
 </td>
</tr>
</tbody></table>
</form>
</td>
</tr>
<tr>
 <td colspan="2" class="header-left"><small>WAR file to deploy</small></td>
</tr>
<tr>
 <td colspan="2">
<form method="post" action="/manager/html/upload?org.apache.catalina.filters.CSRF_NONCE=AA7CC0E4CCA8F6D7F026B2509DE78A72" enctype="multipart/form-data">
<table cellspacing="0" cellpadding="3">
<tbody><tr>
 <td class="row-right">
  <small>Select WAR file to upload</small>
 </td>
 <td class="row-left">
  <input type="file" name="deployWar" size="40">
 </td>
</tr>
<tr>
 <td class="row-right">
  &nbsp;
 </td>
 <td class="row-left">
  <input type="submit" value="Deploy">
 </td>
</tr>
</tbody></table>
</form>
</td>
</tr>
</tbody></table>
<br>

<table border="1" cellspacing="0" cellpadding="3">
<tbody><tr>
 <td colspan="2" class="title">Diagnostics</td>
</tr>
<tr>
 <td colspan="2" class="header-left"><small>Check to see if a web application has caused a memory leak on stop, reload or undeploy</small></td>
</tr>
<tr>
 <td colspan="2">
<form method="post" action="/manager/html/findleaks?org.apache.catalina.filters.CSRF_NONCE=AA7CC0E4CCA8F6D7F026B2509DE78A72">
<table cellspacing="0" cellpadding="3">
<tbody><tr>
 <td class="row-left">
  <input type="submit" value="Find leaks">
 </td>
 <td class="row-left">
  <small>This diagnostic check will trigger a full garbage collection. Use it with extreme caution on production systems.</small>
 </td>
</tr>
</tbody></table>
</form>
</td>
</tr>
</tbody></table>
<br><table border="1" cellspacing="0" cellpadding="3">
<tbody><tr>
 <td colspan="8" class="title">Server Information</td>
</tr>
<tr>
 <td class="header-center"><small>Tomcat Version</small></td>
 <td class="header-center"><small>JVM Version</small></td>
 <td class="header-center"><small>JVM Vendor</small></td>
 <td class="header-center"><small>OS Name</small></td>
 <td class="header-center"><small>OS Version</small></td>
 <td class="header-center"><small>OS Architecture</small></td>
 <td class="header-center"><small>Hostname</small></td>
 <td class="header-center"><small>IP Address</small></td>
</tr>
<tr>
 <td class="row-center"><small>Apache Tomcat/7.0.67</small></td>
 <td class="row-center"><small>1.8.0_201-b09</small></td>
 <td class="row-center"><small>Oracle Corporation</small></td>
 <td class="row-center"><small>Linux</small></td>
 <td class="row-center"><small>3.2.0-91-generic</small></td>
 <td class="row-center"><small>amd64</small></td>
 <td class="row-center"><small>s1.eventpal.com.tw</small></td>
 <td class="row-center"><small>127.0.1.1</small></td>
</tr>
</tbody></table>
<br>

<hr size="1" noshade="noshade">
<center><font size="-1" color="#525D76">
 <em>Copyright © 1999-2015, Apache Software Foundation</em></font></center>


</body></html>
1 回复

附上自己努力的结果,这样应该是可以解决掉我自己的问题。 如果有大大提供更好的解法,再麻烦赐教~

const puppeteer = require('puppeteer');

(async () => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto('http://admin:admin@127.0.0.1:8080/manager');

    await page.evaluate(() => {
        let elements = document.getElementsByClassName('inline');
        for (var element of elements)
            if (element.action.indexOf('/IESC') != -1 && element.action.indexOf('stop') != -1) {
                element.children[0].children[0].classList.add("IESC_Stop");
            }
    });

    try {
        await page.click('.IESC_Stop');
    } catch (e) {
        console.log('No Stop Btn');
    }

    await page.evaluate(() => {
        let elements = document.getElementsByClassName('inline');
        for (var element of elements)
            if (element.action.indexOf('/IESC') != -1 && element.action.indexOf('start') != -1) {
                element.children[0].children[0].classList.add("IESC_Start");
            }
    });

    try {
        await page.click('.IESC_Start');
    } catch (e) {
        console.log('No Start btn');
    }

    await browser.close();
})();
回到顶部