我编写了一个脚本来搜索innerHTML中的String,并在字符串具有正确值时单击该按钮。
当我搜索“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"> </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('ctl00$ContentMiddle$TicketList1$GridView1$ctl03$LinkButton1','')">In den 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('ctl00$ContentMiddle$TicketList1$GridView1$ctl04$LinkButton1','')">In den 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('ctl00$ContentMiddle$TicketList1$GridView1$ctl05$LinkButton1','')">In den 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('ctl00$ContentMiddle$TicketList1$GridView1$ctl06$LinkButton1','')">In den Warenkorb</a>
</span>
</p>
<hr />
</td>
</tr>
</table>
</div>
</div>
以下是控制台所说的内容:
(未知)错误:执行脚本'Zweitmarkt_Preis_test'失败了!无法读取未定义的属性“子”
答案 0 :(得分:0)
使用return
可以获得我们的功能,并且不会执行重载。
使用break
退出while循环
有一个<p>
标记符合zeilen
类标准。 p没有4个孩子,因此脚本会抛出错误。
答案 1 :(得分:0)
问题是CREATE TABLE #tmpProjects
(
ProjectId INT
, Label VARCHAR(1000)
)
SELECT [P].ProjectId, [PR].Label
INTO #tmpProjects
FROM [MySchema].[ProjectResources] [PR]
INNER JOIN dbo.Project [P] ON [P].ProjectId= [PR].ProjectId
返回两个段落,但是只有第一个段落有DOM子句。因此,您的脚本在处理第二段时会出错。一种解决方法是首先检查孩子是否存在:
getElementsByClassName
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();
})();
答案 2 :(得分:0)
我能够修复你的代码。这是一个工作版本。
只需删除该行的评论
即可 //element.click();
好吧没有它是如何工作的?
首先,我们搜索具有这三个类v.content-card-entry.fcb-row.fcb-clear
的所有元素,如下所示:
rows = document.querySelectorAll('.content-card-entry.fcb-row.fcb-clear');
然后我们搜索具有父span
且span
的元素。这将您的children
实施替换为:
row.querySelectorAll('span > span');
现在我们可以获得此标签的ID。请记住,根据您的HTML,您会找到4种标签,Label4
是包含价格的标签。然后验证它是否具有innerHTML值并完成。
var IDString = label.id;
if (IDString.includes("Label4")) {
if (label.innerHTML == amount) {
...
}
}
您可以将变量amount = '60,00 €';
替换为任何其他值。
VielGlück。
(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"> </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('ctl00$ContentMiddle$TicketList1$GridView1$ctl03$LinkButton1','')">In den 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('ctl00$ContentMiddle$TicketList1$GridView1$ctl04$LinkButton1','')">In den 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('ctl00$ContentMiddle$TicketList1$GridView1$ctl05$LinkButton1','')">In den 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('ctl00$ContentMiddle$TicketList1$GridView1$ctl06$LinkButton1','')">In den Warenkorb</a>
</span>
</p>
<hr />
</td>
</tr>
</table>
</div>