Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing Webpage in a Specific Location in Selenium

I have this code to go to Google and convert the page to a PDF.

# Program to go to Google and convert the page to a PDF
# But it saves in my downloads folder (I want it in a particular folder in the directory I ran this code)
from selenium import webdriver
import json

# Set up web driver
options = webdriver.ChromeOptions()
options.headless = False

options.add_argument("--kiosk-printing")

settings = {
    "recentDestinations": [{
        "id": "Save as PDF",
        "origin": "local",
        "account": "",
        'default_directory': 'folder' # This is the folder where i want to place my PDF (in the same directory as this
        # file) 
    }],
    "selectedDestinationId": "Save as PDF",
    "version": 2,

}

prefs = {'printing.print_preview_sticky_settings.appState': json.dumps(settings)}
options.add_experimental_option('prefs', prefs)

driver = webdriver.Chrome(executable_path="chromedriver.exe", options=options)

driver.get("https://google.com")

# This gets saved in my downloads folder
driver.execute_script("window.print();")

But I want it to be saved in the folder called "folder", which is in the same directory as this Python file. 'default_directory': 'folder' did not work.

like image 467
Tkinter Lover Avatar asked Mar 14 '21 15:03

Tkinter Lover


People also ask

How do I print all links on a website?

On a webpage that contains multiple hyperlinks in Internet Explorer 11, you want to print a document and all its linked documents as separate pages. To do this, you click Print on the File menu, select the Options tab, select the Print all linked documents check box, and then click Print.

How do you extract text from a Web page using selenium and save it as a text file?

We can extract text from a webpage using Selenium webdriver and save it as a text file using the getText method. It can extract the text for an element which is displayed (and not hidden by CSS).

How to get the contents of the entire page using selenium?

In this article, we will discuss ways to get the contents of the entire page using Selenium. There can broadly be two methods for the same. Let’s discuss them in detail. For extracting the visible text from the entire page, we can use the find_element_by_* methods which help us find or locate the elements on the page.

How can I Capture network traffic of a specific page using selenium?

How can I capture network traffic of a specific page using Selenium? We can capture network traffic on a specific page using Selenium webdriver in Python. To achieve this, we take the help of the JavaScript Executor. Selenium can execute JavaScript commands with the help of the execute_script method.

How to take a screenshot of a web element in selenium?

Sometimes, we need a screenshot of a particular element on a page. There are two ways to capture the screenshot of a web element in Selenium- Take the fullscreen image and then crop the image as per the dimensions of the web element. Using the getScreenshotAs () method on the web element. ( This is available only in selenium version 4.X)

How to get text from a list in Selenium WebDriver?

We can get the text from a list of all web elements with the same class name in Selenium webdriver. We can use any of the locators like the class name with method By.className, xpath with method By.xpath, or css with method By.cssSelector.


1 Answers

options = webdriver.ChromeOptions()
options.headless = False

options.add_argument("--kiosk-printing")
options.add_argument("--kiosk")

options.add_argument("--kiosk-printing")

settings = {
    "recentDestinations": [{
        "id": "Save as PDF",
        "origin": "local",
        "account": "",
    }],
    "selectedDestinationId": "Save as PDF",
    "version": 2,

}

prefs = {
    'printing.print_preview_sticky_settings.appState': json.dumps(settings), 
    "savefile.default_directory": r"C:\Users\prave\Downloads\travelBA\folder",
}
options.add_experimental_option('prefs', prefs)

driver = webdriver.Chrome(executable_path="chromedriver.exe", options=options)

driver.get("https://google.com")

# This gets saved in my downloads folder
driver.execute_script("window.print();")

input()

use saveFile.default_directory

like image 142
PDHide Avatar answered Oct 31 '22 05:10

PDHide