|
1 | 1 | import base64 |
2 | 2 | import functools |
| 3 | +import glob |
3 | 4 | import io |
4 | 5 | import json |
5 | 6 | import logging |
@@ -973,16 +974,27 @@ def print_pdf(self, path=None, print_options=None): |
973 | 974 | """ |
974 | 975 | title = self.page_title() or "document" |
975 | 976 | timeout = 60000 |
976 | | - if not self.page_title(): |
977 | | - timeout = 1000 |
978 | 977 | default_path = os.path.expanduser(os.path.join(self.download_folder_path, f"{title}.pdf")) |
979 | 978 |
|
980 | 979 | if self.browser in [Browser.CHROME, Browser.EDGE] and not self.headless: |
| 980 | + pdf_current_count = self.get_file_count(file_extension=".pdf") |
981 | 981 | # Chrome still does not support headless webdriver print |
982 | 982 | # but Firefox does. |
983 | 983 | self.execute_javascript("window.print();") |
| 984 | + |
984 | 985 | # We need to wait for the file to be available in this case. |
985 | | - self.wait_for_file(default_path, timeout=timeout) |
| 986 | + if self.page_title(): |
| 987 | + self.wait_for_file(default_path, timeout=timeout) |
| 988 | + else: |
| 989 | + # Waiting when the file don't have the page title in path |
| 990 | + self.wait_for_new_file(file_extension=".pdf", current_count=pdf_current_count) |
| 991 | + |
| 992 | + # Move the downloaded pdf file if the path is not None |
| 993 | + if path: |
| 994 | + last_downloaded_pdf = self.get_last_created_file(self.download_folder_path, ".pdf") |
| 995 | + os.rename(last_downloaded_pdf, path) |
| 996 | + return path |
| 997 | + self.wait(2000) |
986 | 998 | return default_path |
987 | 999 |
|
988 | 1000 | if print_options is None: |
@@ -1858,3 +1870,63 @@ def wait_for_file(self, path, timeout=60000): |
1858 | 1870 | continue |
1859 | 1871 | return True |
1860 | 1872 | self.sleep(config.DEFAULT_SLEEP_AFTER_ACTION) |
| 1873 | + |
| 1874 | + def get_last_created_file(self, path=None, file_extension=""): |
| 1875 | + """Returns the last created file in a specific folder path. |
| 1876 | +
|
| 1877 | + Args: |
| 1878 | + path (str, optional): The path of the folder where the file is expected. Defaults to None. |
| 1879 | + file_extension (str, optional): The extension of the file to be searched for (e.g., .pdf, .txt). |
| 1880 | +
|
| 1881 | + Returns: |
| 1882 | + str: the path of the last created file |
| 1883 | + """ |
| 1884 | + if not path: |
| 1885 | + path = self.download_folder_path |
| 1886 | + |
| 1887 | + files_path = glob.glob(os.path.expanduser(os.path.join(path, f"*{file_extension}"))) |
| 1888 | + last_created_file = max(files_path, key=os.path.getctime) |
| 1889 | + return last_created_file |
| 1890 | + |
| 1891 | + def get_file_count(self, path=None, file_extension=""): |
| 1892 | + """Get the total number of files of the same type. |
| 1893 | +
|
| 1894 | + Args: |
| 1895 | + path (str, optional): The path of the folder where the files are saved. |
| 1896 | + file_extension (str, optional): The extension of the files to be searched for (e.g., .pdf, .txt). |
| 1897 | +
|
| 1898 | + Returns: |
| 1899 | + int: the number of files of the given type |
| 1900 | + """ |
| 1901 | + if not path: |
| 1902 | + path = self.download_folder_path |
| 1903 | + |
| 1904 | + files_path = glob.glob(os.path.expanduser(os.path.join(path, f"*{file_extension}"))) |
| 1905 | + return len(files_path) |
| 1906 | + |
| 1907 | + def wait_for_new_file(self, path=None, file_extension="", current_count=0, timeout=60000): |
| 1908 | + """ |
| 1909 | + Wait for a new file to be available on disk without the file path. |
| 1910 | +
|
| 1911 | + Args: |
| 1912 | + path (str, optional): The path of the folder where the file is expected. Defaults to None. |
| 1913 | + file_extension (str, optional): The extension of the file to be searched for (e.g., .pdf, .txt). |
| 1914 | + current_count (int): The current number of files in the folder of the given type. Defaults to 0 files |
| 1915 | + timeout (int, optional): Maximum wait time (ms) to search for a hit. |
| 1916 | + Defaults to 60000ms (60s). |
| 1917 | +
|
| 1918 | + Returns: |
| 1919 | + str: the path of the last created file of the given type |
| 1920 | + """ |
| 1921 | + if not path: |
| 1922 | + path = self.download_folder_path |
| 1923 | + |
| 1924 | + start_time = time.time() |
| 1925 | + while True: |
| 1926 | + elapsed_time = (time.time() - start_time) * 1000 |
| 1927 | + if elapsed_time > timeout: |
| 1928 | + return None |
| 1929 | + pdf_count = self.get_file_count(path, f"*{file_extension}") |
| 1930 | + if pdf_count == current_count + 1: |
| 1931 | + return self.get_last_created_file(path, f"*{file_extension}") |
| 1932 | + self.sleep(config.DEFAULT_SLEEP_AFTER_ACTION) |
0 commit comments