Search code examples
javascriptscreen-scrapingpuppeteertarget

How to prevent opening a new tab during scraping with puppeteer, when clicking a button with ng-click directive?


When scraping a website containing a button that navigates to a pdf file when clicked, I would normally remove target="_blank" attribute to prevent from opening in a new tab. However, this time around, clicking the button triggers some function like this: ng-click="$ctrl.openPriceList()", pdf gets open in a new tab and removing target attribute on button element does nothing. How to prevent opening in a new tab in such case?


Solution

  • You can override window.open:

    ((window, open) => {
        window.open = (url) => {
            open.call(window, url, '_self');
        };
    })(window, window.open);