OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Playwright: Download via Print to PDF?

  • Thread starter Thread starter VikR
  • Start date Start date
V

VikR

Guest
I'm seeking to scrape a web page using Playwright.

I load the page, and click the download button with Playwright successfully. This brings up a print dialog box with a printer selected.

print dialog box

I would like to select "Save as PDF" and then click the "Save" button.

Here's my current code:

Code:
with sync_playwright() as p:
    browser = p.chromium.launch(headless=True)
    playwright_page = browser.new_page()
    got_error = False

    try:
        playwright_page.goto(url_to_start_from)
        print(playwright_page.title())
        html = playwright_page.content()
    except Exception as e:
        print(f"Playwright exception: {e}")
        got_error = True

    if not got_error:
        soup = BeautifulSoup(html, 'html.parser')

        #download pdf
        with playwright_page.expect_download() as download_info:
            playwright_page.locator("text=download").click()

        download = download_info.value
        path = download.path()
        download.save_as(DOWNLOADED_PDF_FOLDER)

    browser.close()

Is there a way to do this using Playwright?
<p>I'm seeking to scrape a web page using Playwright.</p>
<p>I load the page, and click the download button with Playwright successfully. This brings up a print dialog box with a printer selected.</p>
<p><a href="https://i.sstatic.net/30Mvim.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/30Mvim.jpg" alt="print dialog box" /></a></p>
<p>I would like to select "Save as PDF" and then click the "Save" button.</p>
<p>Here's my current code:</p>
<pre><code>with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
playwright_page = browser.new_page()
got_error = False

try:
playwright_page.goto(url_to_start_from)
print(playwright_page.title())
html = playwright_page.content()
except Exception as e:
print(f"Playwright exception: {e}")
got_error = True

if not got_error:
soup = BeautifulSoup(html, 'html.parser')

#download pdf
with playwright_page.expect_download() as download_info:
playwright_page.locator("text=download").click()

download = download_info.value
path = download.path()
download.save_as(DOWNLOADED_PDF_FOLDER)

browser.close()
</code></pre>
<p>Is there a way to do this using Playwright?</p>
 

Latest posts

G
Replies
0
Views
1
Guilherme G Silva
G
Replies
0
Views
1
潘德拉贡阿尔托莉雅
Y
Replies
0
Views
1
yacine bendjebbar
Y
Top