简体   繁体   English

如何在操纵up的dom元素中导航?

[英]How do I navigate dom elements in puppeteer?

Background 背景

We are writing some automated end to end tests with puppeteer for the first time. 我们是第一次使用puppeteer编写一些自动化的端到端测试。 We've been digging through their APIs at great length, but we're stumped and struggling with what feels to us like basic uses for the system. 我们一直在仔细研究他们的API,但是对于我们感觉像系统的基本用途的东西,我们感到困惑和挣扎。

The Real Question 真正的问题

How does puppeteer want us to interact with the elements? 木偶戏如何让我们与元素互动?

  • get their attributes 得到他们的属性
  • set stuff on them 在他们身上放东西
  • find specific ancestors and descendants of them 找到他们的特定祖先和后代

Comments 评论

What I really want is either a more trimmed API document targeted at our kinds of uses or, even better, a really slick tutorial. 我真正想要的是针对我们的各种用途的更精简的API文档,或者甚至更好的是一个精妙的教程。 We've looked at quite a lot, but we just aren't getting these sorts of answers yet. 我们已经研究了很多,但是我们还没有得到这些答案。

What all the guides we have found are doing, that we do not want to do, is manually putting into the code lots and lots of IDs and selectors. 我们发现所有不想做的指南正在做的事情是手动将代码放入大量的ID和选择器中。 I understand why, but for our purposes we want to read from what is on the page and generate our behavior based on it's shape. 我知道为什么,但是出于我们的目的,我们想阅读页面上的内容,并根据页面的形状生成行为。

Thanks for your time! 谢谢你的时间!

Extra Credit 额外信用

How should I actually approach these code snippets? 我实际上应该如何处理这些代码段? What methods/structures? 什么方法/结构?

Here's one that wants to type a string into all the text inputs, but gets the values for all inputs. 这是一个想要在所有文本输入中键入一个字符串,但获取所有输入的值的方法。

const nodeList = await page.$$('input');
const result = nodeList.map(async node => {
    if(node.type === 'text'){
        await node.type('2018');
    }
    return await node.getAttribute('value')
})
return result

Here's one that wants to type a span label into any child input within that span's parent div. 这是一个想要在该跨度的父div内的任何子输入中键入跨度标签的控件。

const nodeList = await page.$$('span');
const result = nodeList.map(async node => {
    const parentDiv = node.NearestAncestor('div')
    const inputs = parentDiv.$$('input')
    **Use Code From Above**
})
return result

You can try to wrap your promises inside a Promise.all . 您可以尝试将自己的承诺包装在Promise.all

So instead of returning result , you return Promise.all(result) which will resolve once it get's all of the data. 因此,您返回Promise.all(result)而不是返回result ,一旦它获取了所有数据,它将立即解析。

Also, there are many different ways to go thru the list or selectors. 同样,有许多不同的方法可以遍历列表或选择器。

$$eval $$ EVAL

This runs Array.from(document.querySelectorAll(selector)) within the page and passes it as the first argument to pageFunction. 这将在页面内运行Array.from(document.querySelectorAll(selector))并将其作为第一个参数传递给pageFunction。

Example code: 示例代码:

await page.$$eval('a', a => a.href)

evaluate 评估

This is golden and you can execute any code that you can execute on your browsers console. 这是黄金,您可以执行可以在浏览器控制台上执行的任何代码。

const result = await page.evaluate(() =>
  [...document.querySelectorAll('input')] // or anything that you want to use normally
)

Useful APIs 有用的API

There are several useful APIs that can be used wisely: 有几种有用的API可以明智地使用:

  • .type : types a text on selector .type :在选择器上键入文本
  • .click : Clicks an elementhandle/selector etc .click :单击.click句柄/选择器等
  • .select : Selects something from a element .select :从元素中选择某项

Know what doesn't work 知道什么不起作用

elementHandle.attribute() has been removed on #638 . elementHandle.attribute()已在#638上删除。 Which means getting attribute should be handled by $eval from now. 这意味着从现在起应该由$eval处理获取属性。

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

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