Puppeteer iframe下拉菜单选择

时间:2019-04-15 13:20:46

标签: javascript puppeteer end-to-end

我正在使用puppeteer创建测试套件,但是在选择iframe下拉菜单时遇到了麻烦。

我阅读了文档,运行了几种情况,但似乎无法弄清楚。

try {
    (async () => {
         const browser = await puppeteer.launch({headless: false, 
slowMo: 100});
        const page = await browser.newPage();
        await page.setViewport({ width: 1280, height: 800 });
        await page.goto('https://giving.kcmhq.org/');
        await page.click('.col-sm-12 > .form-group > .radio > 
 .radio-inline > #invoice_items_0_gift_array_0');
        await page.type('.form-group > .form-inline > .col-sm-12 > 
.input-group > #invoice_items_0_other_amount', '9999');
        await page.click('.form-group > .col-sm-8 > div > .btn-other >      #invoice_items_0_is_recurring');
        await page.select('#invoice_items_0 > .fund-inputs-container >     .form-group > .col-xs-3 > 
#invoice_items_0_recurring_withdrawal_day', '15');


 try {
    const frame = await page.frames().find(f => f.name() === 
 'payment_iframe');
    const cardNumber = await frame.$('#card_number');
     await cardNumber.type("444433322221111");

}catch (e) {

}

try {
     const frame = await page.frames().find(f => f.name() === 
'payment_iframe');

    const [response] = await Promise.all([

            frame.select('#card_expiration_month', 'March'),


     ]);

}catch (e) {

}  

适用于信用卡的iframe示例有效,但用于“ car_expiration_month”下拉菜单并选择一个月份的iframe无效。

1 个答案:

答案 0 :(得分:0)

查看页面的每个框架可能会对您有所帮助:

for (const frame of page.mainFrame().childFrames()){
    // Here you can use few identifying methods like url(),name(),title()
    if (frame.url().includes('twitter')){
        console.log('we found the Twitter iframe')
        twitterFrame = frame 
        // we assign this frame to myFrame to use it later
    }
}

信用:https://kb.apify.com/articles/2116540-scraping-iframes-with-puppeteer