how to save opened page as pdf in Selenium (Python)

前端 未结 4 2016
忘了有多久
忘了有多久 2021-02-04 16:20

Have tried all the solutions I could find on the Internet to be able to print a page that is open in Selenium in Python. However, while the print pop-up shows up, after a second

4条回答
  •  面向向阳花
    2021-02-04 17:05

    The solution is not very good, but you can take a screenshot and convert to pdf by Pillow...

    from selenium import webdriver
    from io import BytesIO
    from PIL import Image
    
    driver = webdriver.Chrome(executable_path='path to your driver')
    driver.get('your url here')
    img = Image.open(BytesIO(driver.find_element_by_tag_name('body').screenshot_as_png))
    img.save('filename.pdf', "PDF", quality=100)
    

提交回复
热议问题