'Python - Selenium, How to automate print in Chrome?

Hello and happy Holidays!

I wrote a script where I open a webpage in chrome, login, go to a certain page and then trying to print that page. When the Chrome print preview comes up, I can not target the print button neither try to just hit enter via automation. Nothing works.

Any ideas please?

Ps: This is a the code I wrote but the login information will be incorrect

Thank you in advance!

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from shutil import which
import time
from pynput.keyboard import Key, Controller

keyboard = Controller()


chrome_path = which("chromedriver")

driver = webdriver.Chrome(executable_path="chromedriver")

name = '****'
password = '****'

driver.get(
    "https://www.winbank.gr/sites/corporate/el/Pages/default.aspx")

driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))


username = driver.find_element_by_name('Username')
username.send_keys(name)

passwords = driver.find_element_by_name('Password')
passwords.send_keys(password)

passwords.send_keys(Keys.ENTER)

time.sleep(10)
driver.find_element_by_xpath(
    '//*[@id="ng-app"]/body/div[3]/div/div/div/div/div/div/div/div[2]/div[2]/div[1]/div[2]/div/div[2]/div/div/div[6]').click()

window_before = driver.window_handles[0]

time.sleep(6)


driver.find_element_by_xpath(
    '//*[@id="ng-app"]/body/div[3]/div/div/div/div/div/div/div/div[3]/div[2]/wb-pfm-transactions/div/div[2]/div/div[22]/div[2]').click()

time.sleep(3)


driver.switch_to.window(driver.window_handles[-1])

driver.find_element_by_id('print').click()

driver.switch_to.window(driver.window_handles[-1])


print('-------------test----------')

# driver.print = "function(){};"


keyboard.press(Key.enter)


driver.find_element_by_xpath(
    '/html/body/print-preview-app//print-preview-sidebar//print-preview-button-strip//cr-button[2]').click()


Solution 1:[1]

Usually when dealing with untargetable objects I use the tab key to navigate and enter to "click" the element currently focused, what you should do is:
1. Try to print it manually, but with using tabs to navigate and Enter to click
2. Keep track of all key presses needed
3. Write your code accordingly
Edit: Also, if you need help with automatically pressing keys please refer to this link.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ely Shaffir