到目前为止相当令人印象深刻,只是从编剧开始,真的很好,到目前为止真的印象深刻!
我对我们从QuerySelector*()方法返回的元素的持久性/生命周期感到好奇。考虑到大多数web库/框架,如Angular和React,可能会移除和移植新的UI元素……我们是否应该以这样一种方式编写代码,假设元素是更瞬时的,并且应该始终在使用之前执行QuerySelectorAsync(),因为我的元素之前是被获取的?到目前为止,值似乎是实时获取的,即获取控件、检查值、更改值,现在获取值将获得当前/更新的值。
更感兴趣的是,在我们各自的UI库/框架更新了可能的DOM之后,预期的行为是什么?
提前感谢:)
发布于 2021-07-02 17:26:14
如果使用ElementHandle
,这些变量将链接到创建变量时找到的元素。这是一个你可以尝试的例子
const { chromium } = require('playwright');
(async () => {
const browser = await chromium.launch();
const page = await browser.newPage();
await page.evaluate(() => {
const span = document.createElement('span');
span.textContent = 'hello';
document.querySelector('body').appendChild(span);
});
const el = await page.$('span');
console.log(await el.innerText());
await page.evaluate(() => document.querySelector('span').remove());
await page.evaluate(() => {
const span = document.createElement('span');
span.textContent = 'world';
document.querySelector('body').appendChild(span);
});
console.log(await el.innerText());
console.log(await page.innerText('span'));
await browser.close();
})();
输出将是
hello
hello
world
https://stackoverflow.com/questions/68228625
复制