botcity-dev / botcity-framework-web-python

BotCity Framework Web - Python
https://documentation.botcity.dev/frameworks/web/
Apache License 2.0
40 stars 19 forks source link

print_pdf() error -> PermissionError: [WinError 32] O arquivo já está sendo usado por outro processo #39

Closed lf2a closed 2 years ago

lf2a commented 2 years ago

Describe the bug

Raises error when i use print_pdf() only in EDGE headful mode:

def print_pdf(self, path=None, print_options=None):
    """Print the current page as a PDF file.

    Args:
        path (str, optional): The path for the file to be saved. Defaults to None.
        print_options (dict, optional): Print options as defined at. Defaults to None.

    Returns:
        str: the saved file path
    """
    title = self.page_title() or "document"
    timeout = 60000
    default_path = os.path.expanduser(os.path.join(self.download_folder_path, f"{title}.pdf"))

    if self.browser in [Browser.CHROME, Browser.EDGE] and not self.headless:
        pdf_current_count = self.get_file_count(file_extension=".pdf")
        # Chrome still does not support headless webdriver print
        # but Firefox does.
        self.execute_javascript("window.print();")

        # We need to wait for the file to be available in this case.
        if self.page_title():
            self.wait_for_file(default_path, timeout=timeout)
        else:
            # Waiting when the file don't have the page title in path
            self.wait_for_new_file(file_extension=".pdf", current_count=pdf_current_count)

        # Move the downloaded pdf file if the path is not None
        if path:
            last_downloaded_pdf = self.get_last_created_file(self.download_folder_path, ".pdf")
>           os.rename(last_downloaded_pdf, path)
E           PermissionError: [WinError 32] O arquivo já está sendo usado por outro processo: 'D:\\BotCity\\projetos\\dev\\web\\Botcity - web test.pdf' -> 'D:\\BotCity\\projetos\\dev\\web\\page.pdf'

venv\lib\site-packages\botcity\web\bot.py:992: PermissionError

Expected behavior Save pdf and rename.

Steps to Reproduce

web = WebBot()
web.headless = False
web.browser = Browser.EDGE
web.driver_path = os.path.join(project_path, 'web-drivers', 'msedgedriver.exe')

web.browse(os.path.join(project_path, 'web', 'index.html'))
pdf = web.print_pdf(path=os.path.join(project_path, 'page.pdf'))
print(pdf)

My Platform