木偶师 Select Table row/option
Puppeteer Select Table row/option
正在编写一些代码,这些代码将 select 一个 table 选项,该选项是在从下拉菜单中 select 编辑选项后构建的。无法访问 github atm 来检查人偶文档,所以我正在寻找如何调整类似于
的行
let optionValue = await page.$$eval('option', options => options.find(o => o.innerText === "Quality")?.value)
await page.select('#selDept', optionValue);
为了 select 使用 id 标签或“Stephen_Test”或隐藏单元格度量 id 的 innerText 的正确 table 行1640”。我相信 select 度量 ID 1640 会更可取,这样我也可以将该 ID 保存为一个变量,如果需要,以后可以在项目的其他地方使用。我之前没有使用 nodeJS/puppeteer 的经验,不知道如何将此行调整为我正在寻找的内容,因此不胜感激。
当前的 puppeteer 代码
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
await page.authenticate({'username': username, 'password': password});
await page.goto('http://10.10.4.80/index-test-sh.html') //this is an intranet site for the company I work at
await page.waitForTimeout(4000);
await page.waitForSelector('#selDept');
await page.waitForTimeout(4000);
let optionValue = await page.$$eval('option', options => options.find(o => o.innerText === "Quality")?.value)
await page.select('#selDept', optionValue);
await page.waitForTimeout(4000);
let measureValue = await page.$$eval('td', td => td.find(t => t.innerText === "Stephen_Test")?.value)
await page.select('#Output', measureValue);
await page.waitForTimeout(4000);
//await browser.close();
})();
Table 是用这个循环构建的:
for (var i = 0; i < arr.length; i++) {
txtTable = txtTable + "<tr id='row" + i + "'>"; //altered this to have unique row ID's
txtTable = txtTable + "<td style='width:30%;'>" + arr[i].departmentName + "</td>";
txtTable = txtTable + "<td id='measureId" + arr[i].measureId + "' style='display:none; width:10%;'>" + arr[i].measureId + "</td>"; //altered this to include an id using measureId
txtTable = txtTable + "<td style='width:40%;'>" + arr[i].qmsMeasure + "</td>";
txtTable = txtTable + "<td style='width:20%;'>" + arr[i].measureSltOwner + "</td>";
txtTable = txtTable + "</tr>";
};//End Loop
HTML选项是selected后生成的(包含大约10行,只显示我想要的那一行select)
<div class="OptionTable DisplayScrollBar">
<table id="Output">
<thead>
<tr>
<th style="width: 30%;">Department Name</th>
<th style="width: 10%;display:none;">Report ID</th>
<th style="width: 40%;">Measure Name</th>
<th style="width: 20%;">SLT Measure Owner</th>
</tr>
</thead>
<tbody>
<tr id="row0">
<td style="width:30%;">Quality</td>
<td id="measureId1640" style="display:none; width:10%;">1640</td>
<td style="width:40%;">Stephen_Test</td>
<td style="width:20%;">null</td>
</tr>
</tbody>
</div>
第二天重新开始讨论这个问题后,我是这样完成任务的:
try {
await page.$("#measureId1640");
console.log("It exists!");
} catch {
console.log("no dice");
}
let measureId = await page.$$eval('td', td => td.find(t => t.innerText === "1640")?.id); // same as optionValue line, this time we look for 1640, and then return the id attribute of that element
console.log(measureId);
await page.click('#row0')
首先,我设置了一个 try 语句,让我知道 table.
中是否存在所需的 measure/report
接下来,我设置了一个 $$eval
来查看 td
标记类型(我知道这就是 $$eval
语句的这一部分现在的意思),并且对于每个tag 它将使用 .find
函数来查找 innerText
为 1640
的标签,当它找到它时,?.id
拥有它的 return id那个标签。这允许我在将来需要时使用 measureId。
完成后,我使用 page.click('#row0')
到 select 相应的行(我对整行使用 #row0
,而不是尝试使用内部 ID单个单元格),这会显示该报告的正确信息
正在编写一些代码,这些代码将 select 一个 table 选项,该选项是在从下拉菜单中 select 编辑选项后构建的。无法访问 github atm 来检查人偶文档,所以我正在寻找如何调整类似于
的行
let optionValue = await page.$$eval('option', options => options.find(o => o.innerText === "Quality")?.value)
await page.select('#selDept', optionValue);
为了 select 使用 id 标签或“Stephen_Test”或隐藏单元格度量 id 的 innerText 的正确 table 行1640”。我相信 select 度量 ID 1640 会更可取,这样我也可以将该 ID 保存为一个变量,如果需要,以后可以在项目的其他地方使用。我之前没有使用 nodeJS/puppeteer 的经验,不知道如何将此行调整为我正在寻找的内容,因此不胜感激。
当前的 puppeteer 代码
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
await page.authenticate({'username': username, 'password': password});
await page.goto('http://10.10.4.80/index-test-sh.html') //this is an intranet site for the company I work at
await page.waitForTimeout(4000);
await page.waitForSelector('#selDept');
await page.waitForTimeout(4000);
let optionValue = await page.$$eval('option', options => options.find(o => o.innerText === "Quality")?.value)
await page.select('#selDept', optionValue);
await page.waitForTimeout(4000);
let measureValue = await page.$$eval('td', td => td.find(t => t.innerText === "Stephen_Test")?.value)
await page.select('#Output', measureValue);
await page.waitForTimeout(4000);
//await browser.close();
})();
Table 是用这个循环构建的:
for (var i = 0; i < arr.length; i++) {
txtTable = txtTable + "<tr id='row" + i + "'>"; //altered this to have unique row ID's
txtTable = txtTable + "<td style='width:30%;'>" + arr[i].departmentName + "</td>";
txtTable = txtTable + "<td id='measureId" + arr[i].measureId + "' style='display:none; width:10%;'>" + arr[i].measureId + "</td>"; //altered this to include an id using measureId
txtTable = txtTable + "<td style='width:40%;'>" + arr[i].qmsMeasure + "</td>";
txtTable = txtTable + "<td style='width:20%;'>" + arr[i].measureSltOwner + "</td>";
txtTable = txtTable + "</tr>";
};//End Loop
HTML选项是selected后生成的(包含大约10行,只显示我想要的那一行select)
<div class="OptionTable DisplayScrollBar">
<table id="Output">
<thead>
<tr>
<th style="width: 30%;">Department Name</th>
<th style="width: 10%;display:none;">Report ID</th>
<th style="width: 40%;">Measure Name</th>
<th style="width: 20%;">SLT Measure Owner</th>
</tr>
</thead>
<tbody>
<tr id="row0">
<td style="width:30%;">Quality</td>
<td id="measureId1640" style="display:none; width:10%;">1640</td>
<td style="width:40%;">Stephen_Test</td>
<td style="width:20%;">null</td>
</tr>
</tbody>
</div>
第二天重新开始讨论这个问题后,我是这样完成任务的:
try {
await page.$("#measureId1640");
console.log("It exists!");
} catch {
console.log("no dice");
}
let measureId = await page.$$eval('td', td => td.find(t => t.innerText === "1640")?.id); // same as optionValue line, this time we look for 1640, and then return the id attribute of that element
console.log(measureId);
await page.click('#row0')
首先,我设置了一个 try 语句,让我知道 table.
中是否存在所需的 measure/report接下来,我设置了一个 $$eval
来查看 td
标记类型(我知道这就是 $$eval
语句的这一部分现在的意思),并且对于每个tag 它将使用 .find
函数来查找 innerText
为 1640
的标签,当它找到它时,?.id
拥有它的 return id那个标签。这允许我在将来需要时使用 measureId。
完成后,我使用 page.click('#row0')
到 select 相应的行(我对整行使用 #row0
,而不是尝试使用内部 ID单个单元格),这会显示该报告的正确信息