'Selenium python throws error while exporting the pop up chrome window's content as pdf
I am trying to export new pop up window content's as pdf. Sometimes the code worked but most of the time it gives an exception. Clicking on a link on main_window(span icon link) opens a new popup window and I can see the new window contents(by setting headless parameter) are loading fine. Same is code working fine when i tries to export the main_window as pdf. **Note:**appLoadingDiv is the present in both the pages.
from selenium.webdriver import DesiredCapabilities, ChromeOptions
from pathlib import Path
import os
from base64 import b64decode
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import pandas as pd
from retry import retry
def wait(driver):
time.sleep(10)
element = WebDriverWait(driver, 10).until(EC.invisibility_of_element_located((By.ID, 'appLoadingDiv')))
def export_pdf():
driver.get(f"https://my company url")
wait(driver)
driver.find_element(By.XPATH,"//span[@title='span title']").click()
wait(driver)
driver.switch_to.window(driver.window_handles[1])
try:
pdf = b64decode(driver.print_page())#this line is throwing the exception
file = DATA_FOLDER / f"{str(id)}_{str(name)}.pdf"
with file.open( 'wb') as f:
f.write(pdf)
except Exception as e:
print(e)
Code for initializing the driver
HEADLESS = True
CHROME_DRIVER_MANAGER = False
DELAY = 10
def initiate_driver():
BROWSER_HEIGHT = '1080'
BROWSER_WIDTH = '1920'
CHROME_OPTIONS = [f"--window-size={BROWSER_WIDTH}x{BROWSER_HEIGHT}",
"start-maximized",
"headless",
"--no-sandbox",
'--disable-dev-shm-usage',
'--enable-print-browser',
]
options = ChromeOptions()
prefs = {
"profile.default_content_setting_values.notifications": 2,
"download.default_directory": str(Path.cwd()),
"download.directory_upgrade": True,
'prompt_for_download': False,
}
options.add_experimental_option("prefs", prefs)
if HEADLESS:
for option in CHROME_OPTIONS:
options.add_argument(option)
caps = DesiredCapabilities.CHROME
if CHROME_DRIVER_MANAGER:
driver = webdriver.Chrome(ChromeDriverManager().install(), desired_capabilities=caps, chrome_options=options)
else:
driver = webdriver.Chrome(desired_capabilities=caps, chrome_options=options)
driver.implicitly_wait(0)
driver.maximize_window()
return driver
After switching to new window, print_page function is not working and throws an error. Below is the full exception details.
Message: invalid argument
from unknown error: unhandled inspector error: {"code":-32000,"message":"Printing failed"}
(Session info: headless chrome=99.0.4844.51)
Stacktrace:
Backtrace:
Ordinal0 [0x011069A3+2582947]
Ordinal0 [0x0109A6D1+2139857]
Ordinal0 [0x00F93A98+1063576]
Ordinal0 [0x00F85B78+1006456]
Ordinal0 [0x00F8490A+1001738]
Ordinal0 [0x00F84AF7+1002231]
Ordinal0 [0x00F99B9C+1088412]
Ordinal0 [0x00FF4824+1460260]
Ordinal0 [0x00FD854C+1344844]
Ordinal0 [0x00FE834A+1409866]
Ordinal0 [0x00FD8366+1344358]
Ordinal0 [0x00FB5176+1200502]
Ordinal0 [0x00FB6066+1204326]
GetHandleVerifier [0x012ABE02+1675858]
GetHandleVerifier [0x0136036C+2414524]
GetHandleVerifier [0x0119BB01+560977]
GetHandleVerifier [0x0119A8D3+556323]
Ordinal0 [0x010A020E+2163214]
Ordinal0 [0x010A5078+2183288]
Ordinal0 [0x010A51C0+2183616]
Ordinal0 [0x010AEE1C+2223644]
BaseThreadInitThunk [0x776AFA29+25]
RtlGetAppContainerNamedObjectPath [0x77AD7A9E+286]
RtlGetAppContainerNamedObjectPath [0x77AD7A6E+238]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
