Puppeteer Does Not Visualise Complete SVG Chart

前端 未结 1 1542
一向
一向 2021-01-27 08:52

I am using this code in Try Puppeteer:

const browser = await puppeteer.launch();

const page = await browser.newPage();
await page.goto(\'https://www.barchart.co         


        
相关标签:
1条回答
  • 2021-01-27 09:01

    Problem

    You are not waiting for the request to resolve when the input is changed. As a change will trigger a request, you should use page.waitForResponse to wait until the data is loaded.

    In addition, this is an Angular application, which does not seem to like it if you simply change the value of the field via el.value = '1'. Instead you need to try to behave more like a human (and hit backspace and type the input value).

    Solution

    First, you get the element handle (input[name="fieldInput") from the document. Then, you focus the element, remove the value inside by pressing backspace. After that you type the desired input value.

    The input field now has the correct value, now we need to trigger the blur event by calling blur() on the element. In parallel, we wait for the request to the server to finish. After the request finishes, we should give the page a few milliseconds to render the data.

    All together, the resulting code looks like this:

    const browser = await puppeteer.launch();
    
    const page = await browser.newPage();
    await page.goto('https://www.barchart.com/futures/quotes/ESM19/interactive-chart/fullscreen');
    
    // wait until the element appears
    const linkHandler = await page.waitForXPath("//li[contains(text(), '1D')]");
    await linkHandler.click();
    
    // get the input field, focus it, remove what's inside, then type the value
    const elementHandle = await page.$('input[name="fieldInput"]');
    await elementHandle.focus();
    await elementHandle.press('Backspace');
    await elementHandle.type('1');
    
    // trigger the blur event and wait for the response from the server
    await Promise.all([
        page.waitForResponse(response => response.url().includes('https://www.barchart.com/proxies/timeseries/queryminutes.ashx')),
        page.evaluate(el => el.blur(), elementHandle)
    ]);
    
    // give the page a few milliseconds to render the diagram
    await page.waitFor(100);
    
    await page.screenshot({path: 'screenshot.png'});
    await browser.close();
    

    Code improvement

    I also removed the page.$x function and replaced it with the page.waitForXPath function. This makes sure that your scripts waits until the page is loaded and the element you want to click is available before the script continues.

    0 讨论(0)
提交回复
热议问题