【问题标题】:Puppeteer Won't Click on ElementPuppeteer 不会点击元素
【发布时间】:2018-01-15 12:10:29
【问题描述】:

试图让 Puppeteer 浏览我的登录页面,这会将自动化测试带到我网站的主页。这一切都有效,直到它达到'waitFor('#idSIButton9');'。 Puppeteer 成功输入密码和用户名,但似乎无法选择提交按钮(#idSIButton9)。知道这里可能出了什么问题吗?如果你们需要更多信息,请告诉我。 谢谢:)

const puppeteer = require('puppeteer');
    const { defineSupportCode } = require('cucumber')

    defineSupportCode(({ Before, Given, When, Then }) => {
        Before({ timeout: 60 * 1000 }, async function testCase() {

            this.browser = await puppeteer.launch({ headless: false });

            this.page = await this.browser.newPage();

            await this.page.setViewport({width: 1024, height: 768});

            await this.page.goto('http://localhost:3000', {waitUntil: 'networkidle2'});

            await this.page.screenshot({ path: 'test_artifacts/img/load.png', fullPage: true });

            await this.page.waitFor('button');

            await this.page.click('button');

            await this.page.waitFor('#i0116');

            await this.page.type('#i0116', 'username');

            await this.page.waitFor('#i0118');

            await this.page.type('#i0118', 'password');

            await this.page.waitFor('#idSIButton9');

            await this.page.click('#idSIButton9');

        })

【问题讨论】:

  • 也许是那个页面的错字?也许添加该页面的屏幕截图可能会让我们为您提供帮助?

标签: javascript cucumber cucumberjs puppeteer


【解决方案1】:

我好像找到了,如上面的答案

page.waitForNavigation('load') 

在点击触发导航的情况下效果很好。

【讨论】:

    【解决方案2】:

    看起来相当明显,但在点击之前等待使用数值:

    await this.page.waitFor(2000);
    
    await this.page.click('#idSIButton9');
    
    await this.page.waitForNavigation();
    

    【讨论】:

    • 上面的代码 sn-p 对我有用。但现在waitFor 方法已被弃用,我尝试使用显式的waitForSelectorwaitForTimeout,它们不起作用。即使我尝试使用硬等待但没有运气。有什么帮助吗?
    • WaitFor() 已弃用,但您可以改用 WaitForTimout()。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-01
    • 1970-01-01
    • 2016-12-19
    • 2016-06-08
    • 2017-04-16
    • 2018-03-16
    • 1970-01-01
    相关资源
    最近更新 更多