简体   繁体   English

Puppeteer Select 表行/选项

[英]Puppeteer Select Table row/option

Working on some code that will select a table option that gets built out after an option is selected from a drop-down menu.处理一些代码,它将 select 从下拉菜单中选择一个选项后构建一个表格选项。 Don't have access to github atm to check puppeteer documentation so I'm looking for how to adapt a line similar to无权访问 github atm 来检查 puppeteer 文档,所以我正在寻找如何调整类似于
let optionValue = await page.$$eval('option', options => options.find(o => o.innerText === "Quality")?.value)

await page.select('#selDept', optionValue); in order to select the proper table row using either the id tag or the innerText of either "Stephen_Test" or the hidden cell measure id "1640".为了 select 使用“Stephen_Test”或隐藏单元格测量 id“1640”的 id 标签或 innerText 的正确表格行。 I believe selecting the measure id 1640 would be preferable so that I can also save that id as a variable that could be used elsewhere later on in the project if needed.我相信选择度量 id 1640 会更好,这样我还可以将该 id 保存为变量,如果需要,以后可以在项目的其他地方使用。 I just don't have prior experience with nodeJS/puppeteer to know how to adjust this line to what I'm looking for so any help is appreciated.我只是没有使用 nodeJS/puppeteer 的经验,不知道如何将这条线调整为我正在寻找的内容,因此不胜感激。

Current puppeteer code当前的木偶代码

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 is built with this loop:表是用这个循环构建的:

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 generated after option is selected (contains about 10 rows, just showing the one I want to select)选择选项后生成的HTML (大约10行,只显示我要选择的那一行)

<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>

After coming back to this issue the following day with a fresh start, here's how I accomplished the task:在第二天以全新的开始回到这个问题之后,这是我完成任务的方式:

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')

First, I set up a try statement that will let me know whether or not a desired measure/report exists in the table.首先,我设置了一个 try 语句,让我知道表中是否存在所需的度量/报告。

Next, I set up a $$eval that looks at the td tag type (I understand that that's what this part of the $$eval statement means now), and for each tag it will use the .find function to look for the tag with an innerText of 1640 , when it finds this, ?.id has it return the id of that tag.接下来,我设置了一个$$eval来查看td标签类型(我知道这就是$$eval语句的这一部分现在的含义),并且对于每个标签,它将使用.find function 来查找标签使用1640innerText ,当它找到这个时, ?.id让它返回该标签的 id。 This allows me to use the measureId in the future if needed.如果需要,这使我可以在将来使用 measureId。

Once this is done, I then use page.click('#row0') to select the corresponding row (I use #row0 for the whole row, as opposed to trying to use the inner id's of individual cells), and this brings up the proper information for that report完成后,我使用page.click('#row0')到 select 对应的行(我对整行使用#row0 ,而不是尝试使用单个单元格的内部 id),这会带来该报告的正确信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM