【发布时间】:2019-05-23 20:34:01
【问题描述】:
我不知道为什么,但似乎有些字段不适合我。
这对我来说在一个站点上运行良好:
await page.click(USERNAME_SELECTOR);
await page.keyboard.type(CREDS.username);
await page.click(PASSWORD_SELECTOR);
await page.keyboard.type(CREDS.password);
await page.click(LOGIN_BUTTON_SELECTOR);
await page.waitForNavigation();
但是,在 https://app.member.virginpulse.com/ 上,它似乎对我不起作用。它应该很简单,因为输入字段都有 ID(#username 和 $password)......但是,它似乎没有正确填写。
const puppeteer = require('puppeteer');
const email = "foo@bar.com";
const password = "foopass";
const emailInputSel = "#username";
const passwordInputSel = "#password";
const signInButtonSel = ".login-submit input";
const homeUrl = "https://app.member.virginpulse.com";
async function run() {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(homeUrl, {waitUntil: 'domcontentloaded'});
//----from here---
await page.waitFor(emailInputSel);
await page.$eval(emailInputSel, (e,v) => e.value = v, email);
await page.$eval(passwordInputSel, (e,v) => e.value = v, password);
//----to here----- i've tried several things
await page.screenshot({path: 'screenshot.png'});
...
我尝试过的其他事情:
await page.evaluate((sel, val) => {
const ele = document.querySelector(sel);
if (!ele) {
return;
}
ele.value = val;
}, sel, val);
await page.evaluate((sel) => {
const ele = document.querySelector(sel);
if (!ele) {
return;
}
ele.click(); ele.focus();
}, sel);
await page.keyboard.type(val);
似乎没有正确填写数据。有谁知道发生了什么以及如何让它发挥作用?
【问题讨论】:
-
嗨,您能否在
it doesn't seem to work上解释更多,任何表明它不起作用的标志或指标`或其似乎不起作用的原因? -
实际填写部分的代码对我来说看起来是正确的,你可以尝试更改
{waitUntil: 'domcontentloaded'}部分吗,因为有时会触发domcontentloaded事件,但是页面需要一些时间来加载js文件并运行脚本 -
我注意到的另一件事是,当我导航到https://app.member.virginpulse.com/ 时,它会重定向到https://iam.virginpulse.com/auth/realms/virginpulse/protocol/openid-connect/auth?... 之类的东西以输入实际密码,您是否尝试过 waitForNavigation 或其他逻辑来处理这种行为?
-
其他可能的行为是您登录一次后浏览器已经设置了cookie,当您再次导航到https://app.member.virginpulse.com/时,cookie显示您已经登录,因为浏览器重定向了其他页面,因此有不是电子邮件或密码输入
-
@plat123456789 - 由于不工作,屏幕截图通常显示登录字段完全为空......我遇到过一些奇怪的情况,其中部分输入存在而部分不存在。如果您自己插入运行,您可以看到屏幕截图可能只是空的。除了
domcontentloaded,我还尝试过networkidle0。我还使用waitFor等待选择器可用。我还输入了代码来获取页面和控制台的标题在尝试输入之前记录它......正如我所期望的那样,它是“登录到 Virgin Pulse”。