[Selenium] Print without header and footer (forced) [Python / Chrome Driver]

Do you operate the browser?

Do you operate the same browser every day?

Let's leave such browser operation to Selenium!

By default, Google Chrome printing is set to display the header and footer. You don't need a date or URL to print ... But there are no items that can be set, maybe ...

Use PyAutoGui to operate the keyboard and change it by force!

It will be such a series of flow.

(Don't touch the mouse or keyboard while running) (Cannot be used in headless mode) (I have not tried it in Mac environment)

Print without header and footer (forced)

sample.py



from selenium import webdriver

import time
import pyautogui as pgui

driver = webdriver.Chrome("chromedriver.exe") #chromedriver.exe load

#
#
#Processing up to the corresponding page
#
#

pgui.hotkey('ctrl','p')
time.sleep(3)

for loop in range(9):
    pgui.press('tab')
    time.sleep(.5)

time.sleep(.5)
pgui.press('enter')
time.sleep(1)

for loop in range(5):
    pgui.press('tab')
    time.sleep(.5)

time.sleep(.5)
pgui.press('enter')
time.sleep(1)

for loop in range(3):
    pgui.press('tab')
    time.sleep(.5)

time.sleep(.5)
pgui.press('enter')
time.sleep(1)

Let's adjust by inserting wait and sleep as appropriate. It will be printed by the printer set as the default printer.

Commentary

sample.py


from selenium import webdriver

Load the Selenium library.

sample.py


import time
import pyautogui as pgui

With the time module to put a delay Load the module for keyboard operation.

sample.py


driver = webdriver.Chrome("chromedriver.exe") #chromedriver.exe load

#
#
#Processing up to the corresponding page
#
#

Let's start the chrome driver and operate it.

sample.py



pgui.hotkey('ctrl','p')
time.sleep(3)

for loop in range(9):
    pgui.press('tab')
    time.sleep(.5)

time.sleep(.5)
pgui.press('enter')
time.sleep(1)

for loop in range(5):
    pgui.press('tab')
    time.sleep(.5)

time.sleep(.5)
pgui.press('enter')
time.sleep(1)

for loop in range(3):
    pgui.press('tab')
    time.sleep(.5)

time.sleep(.5)
pgui.press('enter')
time.sleep(1)

CTRL + P is a print shortcut.

Then use the Tab key to move and press enter at the header and footer to uncheck it.

You can also use the Tab key to move to the print button and press the print button to start printing.

It's an analog and simple method.

bonus

PyAutoGUI is excellent and can operate the PC automatically.

You can also operate the mouse cursor. Pretending to be working? e? What?

sample.py


import pyautogui as pgui

#Move the mouse cursor 100px to the right from the current position to the 100px position over 1 second.
pgui.move(100, -100, 1)

#If move is set to moveTo, it can be specified by coordinates.
#Seconds can be omitted. teleportation.
#You can also use None.
pgui.moveTo(300, None)

#You can click. You can mass-produce cookies by turning them in a loop.
pgui.click() 

If you want to know more, please read the document. https://pyautogui.readthedocs.io/

Thank you very much!

Recommended Posts

[Selenium] Print without header and footer (forced) [Python / Chrome Driver]
Try running Google Chrome with Python and Selenium
Automate Chrome with Python and Selenium on your Chromebook
[Selenium] Open the link in a new tab and move it [Python / Chrome Driver]
Challenge Python3 and Selenium Webdriver
How to automatically install Chrome Driver for Chrome version with Python + Selenium + Chrome
Scraping with Python, Selenium and Chromedriver
Print with python3 without line breaks
Selenium and python to open google
Screenshots of Megalodon in selenium and Chrome.
Practice web scraping with Python and Selenium
Overwrite download file for python selenium Chrome
I tried to make a periodical process with CentOS7, Selenium, Python and Chrome
Environment construction procedure to operate chrome without installing python on Windows (using selenium)