Javascript 在 innerHTML 中搜索字符串时停止

Javascript stops when searching for string in innerHTML

我编写了一个脚本来在 innerHTML 中搜索字符串,并在字符串具有正确值时单击按钮。

当我搜索“313”时一切正常。 当找不到任何内容时脚本会重新加载,并在 innerHTML 匹配 313 时单击我的按钮。

问题在这里:

当我尝试将 innerHTML 与“35,00 €”进行比较时,脚本会在未找到任何内容时正常重新加载。 但是当找到例如“60,00 €”时,脚本停止。随后不会重新加载页面。

谁能告诉我为什么????

这是我的脚本:

(function() {
  'use strict';
  var ClickID, IDString, anfang, clickstring, element, ende, i, zeile, zeilen;
  zeilen = document.getElementsByClassName('content-card-entry fcb-row fcb-clear');
  i = 0;
  while (i < zeilen.length) {
    zeile = zeilen[i];
    if (zeile.children[3].children[0].innerHTML == "35,00 €") {
      IDString = zeile.children[0].children[0].id;
      ClickID = IDString.substr(42, 5);
      anfang = 'ctl00_ContentMiddle_TicketList1_GridView1_';
      ende = '_LinkButton1';
      clickstring = anfang + ClickID + ende;
      element = document.getElementById(clickstring);
      element.click();
      return;
    }
    i++;
}
  location.reload();
  
})();

这是它运行的html:

<div>
  <table cellspacing="0" rules="rows" id="ctl00_ContentMiddle_TicketList1_GridView1" style="border-width:0px;width:100%;border-collapse:collapse;">
   <tr>
    <th scope="col">&nbsp;</th>
   </tr><tr>
    <td>   
                                   <p id="ctl00_ContentMiddle_TicketList1_GridView1_ctl03_P1" class="content-card-entry fcb-row fcb-clear">
                                         <span class="content-card-entry-value fcb-gr-3 fcb-gr-3@small">
                                            <span id="ctl00_ContentMiddle_TicketList1_GridView1_ctl03_Label1">101</span>
                                         </span>
                                        <span class="content-card-entry-value fcb-gr-3 fcb-gr-3@small">
                                            <span id="ctl00_ContentMiddle_TicketList1_GridView1_ctl03_Label2">16</span>
                                         </span>
                                        <span class="content-card-entry-value fcb-gr-3 fcb-gr-3@small">
                                            <span id="ctl00_ContentMiddle_TicketList1_GridView1_ctl03_Label3">21</span>
                                         </span>
                                         <span class="content-card-entry-value fcb-gr-3 fcb-gr-3@small">
                                            <span id="ctl00_ContentMiddle_TicketList1_GridView1_ctl03_Label4">70,00 €</span>
                                         </span>
                                    </p>                     
                                        <p id="ctl00_ContentMiddle_TicketList1_GridView1_ctl03_P5" class="content-card-entry fcb-row fcb-clear" style="text-align:right;">
                                            <span class="content-card-entry-value fcb-gr-12 fcb-gr-12@small">
                                                <a id="ctl00_ContentMiddle_TicketList1_GridView1_ctl03_LinkButton1" class="button small cart-buttons-second-button" Autopostback="false" href="javascript:__doPostBack(&#39;ctl00$ContentMiddle$TicketList1$GridView1$ctl03$LinkButton1&#39;,&#39;&#39;)">In den&nbsp;Warenkorb</a>
                                            </span>
                                        </p>
                                <hr />
                            </td>
   </tr><tr>
    <td>   
                                   <p id="ctl00_ContentMiddle_TicketList1_GridView1_ctl04_P1" class="content-card-entry fcb-row fcb-clear">
                                         <span class="content-card-entry-value fcb-gr-3 fcb-gr-3@small">
                                            <span id="ctl00_ContentMiddle_TicketList1_GridView1_ctl04_Label1">107</span>
                                         </span>
                                        <span class="content-card-entry-value fcb-gr-3 fcb-gr-3@small">
                                            <span id="ctl00_ContentMiddle_TicketList1_GridView1_ctl04_Label2">14</span>
                                         </span>
                                        <span class="content-card-entry-value fcb-gr-3 fcb-gr-3@small">
                                            <span id="ctl00_ContentMiddle_TicketList1_GridView1_ctl04_Label3">23</span>
                                         </span>
                                         <span class="content-card-entry-value fcb-gr-3 fcb-gr-3@small">
                                            <span id="ctl00_ContentMiddle_TicketList1_GridView1_ctl04_Label4">70,00 €</span>
                                         </span>
                                    </p>                     
                                        <p id="ctl00_ContentMiddle_TicketList1_GridView1_ctl04_P5" class="content-card-entry fcb-row fcb-clear" style="text-align:right;">
                                            <span class="content-card-entry-value fcb-gr-12 fcb-gr-12@small">
                                                <a id="ctl00_ContentMiddle_TicketList1_GridView1_ctl04_LinkButton1" class="button small cart-buttons-second-button" Autopostback="false" href="javascript:__doPostBack(&#39;ctl00$ContentMiddle$TicketList1$GridView1$ctl04$LinkButton1&#39;,&#39;&#39;)">In den&nbsp;Warenkorb</a>
                                            </span>
                                        </p>
                                <hr />
                            </td>
   </tr><tr>
    <td>   
                                   <p id="ctl00_ContentMiddle_TicketList1_GridView1_ctl05_P1" class="content-card-entry fcb-row fcb-clear">
                                         <span class="content-card-entry-value fcb-gr-3 fcb-gr-3@small">
                                            <span id="ctl00_ContentMiddle_TicketList1_GridView1_ctl05_Label1">326</span>
                                         </span>
                                        <span class="content-card-entry-value fcb-gr-3 fcb-gr-3@small">
                                            <span id="ctl00_ContentMiddle_TicketList1_GridView1_ctl05_Label2">6</span>
                                         </span>
                                        <span class="content-card-entry-value fcb-gr-3 fcb-gr-3@small">
                                            <span id="ctl00_ContentMiddle_TicketList1_GridView1_ctl05_Label3">16</span>
                                         </span>
                                         <span class="content-card-entry-value fcb-gr-3 fcb-gr-3@small">
                                            <span id="ctl00_ContentMiddle_TicketList1_GridView1_ctl05_Label4">60,00 €</span>
                                         </span>
                                    </p>                     
                                        <p id="ctl00_ContentMiddle_TicketList1_GridView1_ctl05_P5" class="content-card-entry fcb-row fcb-clear" style="text-align:right;">
                                            <span class="content-card-entry-value fcb-gr-12 fcb-gr-12@small">
                                                <a id="ctl00_ContentMiddle_TicketList1_GridView1_ctl05_LinkButton1" class="button small cart-buttons-second-button" Autopostback="false" href="javascript:__doPostBack(&#39;ctl00$ContentMiddle$TicketList1$GridView1$ctl05$LinkButton1&#39;,&#39;&#39;)">In den&nbsp;Warenkorb</a>
                                            </span>
                                        </p>
                                <hr />
                            </td>
   </tr><tr>
    <td>   
                                   <p id="ctl00_ContentMiddle_TicketList1_GridView1_ctl06_P1" class="content-card-entry fcb-row fcb-clear">
                                         <span class="content-card-entry-value fcb-gr-3 fcb-gr-3@small">
                                            <span id="ctl00_ContentMiddle_TicketList1_GridView1_ctl06_Label1">313</span>
                                         </span>
                                        <span class="content-card-entry-value fcb-gr-3 fcb-gr-3@small">
                                            <span id="ctl00_ContentMiddle_TicketList1_GridView1_ctl06_Label2">18</span>
                                         </span>
                                        <span class="content-card-entry-value fcb-gr-3 fcb-gr-3@small">
                                            <span id="ctl00_ContentMiddle_TicketList1_GridView1_ctl06_Label3">18</span>
                                         </span>
                                         <span class="content-card-entry-value fcb-gr-3 fcb-gr-3@small">
                                            <span id="ctl00_ContentMiddle_TicketList1_GridView1_ctl06_Label4">35,00 €</span>
                                         </span>
                                    </p>                     
                                        <p id="ctl00_ContentMiddle_TicketList1_GridView1_ctl06_P5" class="content-card-entry fcb-row fcb-clear" style="text-align:right;">
                                            <span class="content-card-entry-value fcb-gr-12 fcb-gr-12@small">
                                                <a id="ctl00_ContentMiddle_TicketList1_GridView1_ctl06_LinkButton1" class="button small cart-buttons-second-button" Autopostback="false" href="javascript:__doPostBack(&#39;ctl00$ContentMiddle$TicketList1$GridView1$ctl06$LinkButton1&#39;,&#39;&#39;)">In den&nbsp;Warenkorb</a>
                                            </span>
                                        </p>
                                <hr />
                            </td>
   </tr>
  </table>
 </div> 
           </div> 

控制台显示如下:

(未知)错误:脚本执行 'Zweitmarkt_Preis_test' 失败!无法读取未定义的 属性 'children'

return 的使用使您无法使用该功能,并且不会执行重新加载。 使用 break 退出 while 循环

有一个 <p> 标签符合 zeilen class 条件。 p 没有 4 children,因此脚本会抛出错误。

https://jsfiddle.net/zw2x6ur2/

问题是getElementsByClassNamereturns两段,然而只有第一段有DOMchildren。因此,您的脚本在处理第二段时会出错。一种解决方法是首先检查 children 是否存在:

if (zeile.children[3] && zeile.children[3].children[0].innerHTML == "35,00 €") {

(function() {
  'use strict';
  var ClickID, IDString, anfang, clickstring, element, ende, i, zeile, zeilen;
  zeilen = document.getElementsByClassName('content-card-entry fcb-row fcb-clear');
  i = 0;
  while (i < zeilen.length) {
    zeile = zeilen[i];    
    if (zeile.children[3] && zeile.children[3].children[0].innerHTML == "35,00 €") {
      IDString = zeile.children[0].children[0].id;
      ClickID = IDString.substr(42, 5);
      anfang = 'ctl00_ContentMiddle_TicketList1_GridView1_';
      ende = '_LinkButton1';
      clickstring = anfang + ClickID + ende;
      element = document.getElementById(clickstring);
      element.click();
      return;
    }
    i++;
}
  //location.reload();
  
})();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<td>
  <p id="ctl00_ContentMiddle_TicketList1_GridView1_ctl03_P1" class="content-card-entry fcb-row fcb-clear">
    <span class="content-card-entry-value fcb-gr-3 fcb-gr-3@small">
                                            <span id="ctl00_ContentMiddle_TicketList1_GridView1_ctl03_Label1">101</span>
    </span>
    <span class="content-card-entry-value fcb-gr-3 fcb-gr-3@small">
                                            <span id="ctl00_ContentMiddle_TicketList1_GridView1_ctl03_Label2">16</span>
    </span>
    <span class="content-card-entry-value fcb-gr-3 fcb-gr-3@small">
                                            <span id="ctl00_ContentMiddle_TicketList1_GridView1_ctl03_Label3">21</span>
    </span>
    <span class="content-card-entry-value fcb-gr-3 fcb-gr-3@small">
                                            <span id="ctl00_ContentMiddle_TicketList1_GridView1_ctl03_Label4">70,00 €</span>
    </span>
  </p>
  <p id="ctl00_ContentMiddle_TicketList1_GridView1_ctl03_P5" class="content-card-entry fcb-row fcb-clear" style="text-align:right;">
    <span class="content-card-entry-value fcb-gr-12 fcb-gr-12@small">
                                                <a id="ctl00_ContentMiddle_TicketList1_GridView1_ctl03_LinkButton1" class="button small cart-buttons-second-button" Autopostback="false" href="javascript:__doPostBack(&#39;ctl00$ContentMiddle$TicketList1$GridView1$ctl03$LinkButton1&#39;,&#39;&#39;)">In den&nbsp;Warenkorb</a>
                                            </span>
  </p>
  <hr />
</td>

我能够修复您的代码。这是一个工作版本。

去掉

行的注释即可
 //element.click();

好的,不,它是如何工作的?

  1. 首先,我们搜索所有具有这三个 类 v.content-card-entry.fcb-row.fcb-clear 的元素,如下所示:

    rows = document.querySelectorAll('.content-card-entry.fcb-row.fcb-clear');
    
  2. 然后我们搜索具有父元素 span 并且是 span 的元素。这将您的 children 实现替换为:

    row.querySelectorAll('span > span');
    
  3. 现在我们可以得到这个标签的id了。记住根据你的 HTML 你会找到 4 种标签, Label4 是包含价格的标签。然后你验证它有一个内部HTML值并完成。

    var IDString = label.id;
        if (IDString.includes("Label4")) {
        if (label.innerHTML == amount) {
            ...
        }
    }
    

您可以用任何其他值替换变量 amount = '60,00 €';

维尔·格吕克。

(function() {
  'use strict';
  var ClickID, IDString, anfang, clickstring, ende, amount, rows;
  anfang = 'ctl00_ContentMiddle_TicketList1_GridView1_';
  ende = '_LinkButton1';
  amount = '60,00 €';
  
  rows = document.querySelectorAll('.content-card-entry.fcb-row.fcb-clear');
  rows.forEach(function(row) {
    var labels = row.querySelectorAll('span > span');
    labels.forEach(function(label) {
      var IDString = label.id;
      if (IDString.includes("Label4")) {
        if (label.innerHTML == amount) {
          ClickID = IDString.substr(42, 5);
          clickstring = anfang + ClickID + ende;        
          var element = document.getElementById(clickstring);
          console.log('Click on: ' + clickstring);
          //element.click();
          return;
        }
      }
    });
  });
 location.reload();
})();
<div>
  <table cellspacing="0" rules="rows" id="ctl00_ContentMiddle_TicketList1_GridView1" style="border-width:0px;width:100%;border-collapse:collapse;">
    <tr>
      <th scope="col">&nbsp;</th>
    </tr>
    <tr>
      <td>
        <p id="ctl00_ContentMiddle_TicketList1_GridView1_ctl03_P1" class="content-card-entry fcb-row fcb-clear">
          <span class="content-card-entry-value fcb-gr-3 fcb-gr-3@small">
                                            <span id="ctl00_ContentMiddle_TicketList1_GridView1_ctl03_Label1">101</span>
          </span>
          <span class="content-card-entry-value fcb-gr-3 fcb-gr-3@small">
                                            <span id="ctl00_ContentMiddle_TicketList1_GridView1_ctl03_Label2">16</span>
          </span>
          <span class="content-card-entry-value fcb-gr-3 fcb-gr-3@small">
                                            <span id="ctl00_ContentMiddle_TicketList1_GridView1_ctl03_Label3">21</span>
          </span>
          <span class="content-card-entry-value fcb-gr-3 fcb-gr-3@small">
                                            <span id="ctl00_ContentMiddle_TicketList1_GridView1_ctl03_Label4">70,00 €</span>
          </span>
        </p>
        <p id="ctl00_ContentMiddle_TicketList1_GridView1_ctl03_P5" class="content-card-entry fcb-row fcb-clear" style="text-align:right;">
          <span class="content-card-entry-value fcb-gr-12 fcb-gr-12@small">
                                                <a id="ctl00_ContentMiddle_TicketList1_GridView1_ctl03_LinkButton1" class="button small cart-buttons-second-button" Autopostback="false" href="javascript:__doPostBack(&#39;ctl00$ContentMiddle$TicketList1$GridView1$ctl03$LinkButton1&#39;,&#39;&#39;)">In den&nbsp;Warenkorb</a>
                                            </span>
        </p>
        <hr />
      </td>
    </tr>
    <tr>
      <td>
        <p id="ctl00_ContentMiddle_TicketList1_GridView1_ctl04_P1" class="content-card-entry fcb-row fcb-clear">
          <span class="content-card-entry-value fcb-gr-3 fcb-gr-3@small">
                                            <span id="ctl00_ContentMiddle_TicketList1_GridView1_ctl04_Label1">107</span>
          </span>
          <span class="content-card-entry-value fcb-gr-3 fcb-gr-3@small">
                                            <span id="ctl00_ContentMiddle_TicketList1_GridView1_ctl04_Label2">14</span>
          </span>
          <span class="content-card-entry-value fcb-gr-3 fcb-gr-3@small">
                                            <span id="ctl00_ContentMiddle_TicketList1_GridView1_ctl04_Label3">23</span>
          </span>
          <span class="content-card-entry-value fcb-gr-3 fcb-gr-3@small">
                                            <span id="ctl00_ContentMiddle_TicketList1_GridView1_ctl04_Label4">70,00 €</span>
          </span>
        </p>
        <p id="ctl00_ContentMiddle_TicketList1_GridView1_ctl04_P5" class="content-card-entry fcb-row fcb-clear" style="text-align:right;">
          <span class="content-card-entry-value fcb-gr-12 fcb-gr-12@small">
                                                <a id="ctl00_ContentMiddle_TicketList1_GridView1_ctl04_LinkButton1" class="button small cart-buttons-second-button" Autopostback="false" href="javascript:__doPostBack(&#39;ctl00$ContentMiddle$TicketList1$GridView1$ctl04$LinkButton1&#39;,&#39;&#39;)">In den&nbsp;Warenkorb</a>
                                            </span>
        </p>
        <hr />
      </td>
    </tr>
    <tr>
      <td>
        <p id="ctl00_ContentMiddle_TicketList1_GridView1_ctl05_P1" class="content-card-entry fcb-row fcb-clear">
          <span class="content-card-entry-value fcb-gr-3 fcb-gr-3@small">
                                            <span id="ctl00_ContentMiddle_TicketList1_GridView1_ctl05_Label1">326</span>
          </span>
          <span class="content-card-entry-value fcb-gr-3 fcb-gr-3@small">
                                            <span id="ctl00_ContentMiddle_TicketList1_GridView1_ctl05_Label2">6</span>
          </span>
          <span class="content-card-entry-value fcb-gr-3 fcb-gr-3@small">
                                            <span id="ctl00_ContentMiddle_TicketList1_GridView1_ctl05_Label3">16</span>
          </span>
          <span class="content-card-entry-value fcb-gr-3 fcb-gr-3@small">
                                            <span id="ctl00_ContentMiddle_TicketList1_GridView1_ctl05_Label4">60,00 €</span>
          </span>
        </p>
        <p id="ctl00_ContentMiddle_TicketList1_GridView1_ctl05_P5" class="content-card-entry fcb-row fcb-clear" style="text-align:right;">
          <span class="content-card-entry-value fcb-gr-12 fcb-gr-12@small">
                                                <a id="ctl00_ContentMiddle_TicketList1_GridView1_ctl05_LinkButton1" class="button small cart-buttons-second-button" Autopostback="false" href="javascript:__doPostBack(&#39;ctl00$ContentMiddle$TicketList1$GridView1$ctl05$LinkButton1&#39;,&#39;&#39;)">In den&nbsp;Warenkorb</a>
                                            </span>
        </p>
        <hr />
      </td>
    </tr>
    <tr>
      <td>
        <p id="ctl00_ContentMiddle_TicketList1_GridView1_ctl06_P1" class="content-card-entry fcb-row fcb-clear">
          <span class="content-card-entry-value fcb-gr-3 fcb-gr-3@small">
                                            <span id="ctl00_ContentMiddle_TicketList1_GridView1_ctl06_Label1">313</span>
          </span>
          <span class="content-card-entry-value fcb-gr-3 fcb-gr-3@small">
                                            <span id="ctl00_ContentMiddle_TicketList1_GridView1_ctl06_Label2">18</span>
          </span>
          <span class="content-card-entry-value fcb-gr-3 fcb-gr-3@small">
                                            <span id="ctl00_ContentMiddle_TicketList1_GridView1_ctl06_Label3">18</span>
          </span>
          <span class="content-card-entry-value fcb-gr-3 fcb-gr-3@small">
                                            <span id="ctl00_ContentMiddle_TicketList1_GridView1_ctl06_Label4">35,00 €</span>
          </span>
        </p>
        <p id="ctl00_ContentMiddle_TicketList1_GridView1_ctl06_P5" class="content-card-entry fcb-row fcb-clear" style="text-align:right;">
          <span class="content-card-entry-value fcb-gr-12 fcb-gr-12@small">
                                                <a id="ctl00_ContentMiddle_TicketList1_GridView1_ctl06_LinkButton1" class="button small cart-buttons-second-button" Autopostback="false" href="javascript:__doPostBack(&#39;ctl00$ContentMiddle$TicketList1$GridView1$ctl06$LinkButton1&#39;,&#39;&#39;)">In den&nbsp;Warenkorb</a>
                                            </span>
        </p>
        <hr />
      </td>
    </tr>
  </table>
</div>