Skip to content

Commit a215140

Browse files
authored
Merge pull request #10 from hhslepicka/wait_for_downloads
ENH: Add wait_for_downloads method.
2 parents bf4ff3b + 1e4266d commit a215140

File tree

5 files changed

+57
-4
lines changed

5 files changed

+57
-4
lines changed

botcity/web/bot.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from selenium.common.exceptions import InvalidSessionIdException
1818
from selenium.webdriver.common.action_chains import ActionChains
1919
from selenium.webdriver.common.keys import Keys
20+
from selenium.webdriver.support.ui import WebDriverWait
2021

2122
from . import config, cv2find
2223
from .browsers import Browser, BROWSER_CONFIGS
@@ -65,7 +66,7 @@ def __init__(self, headless=False):
6566
self._shift_hold = False
6667

6768
self._dimensions = (1600, 900)
68-
self._download_folder_path = None # Defaults to ~/Desktop
69+
self._download_folder_path = os.path.join(os.path.expanduser("~"), "Desktop")
6970

7071
@property
7172
def driver(self):
@@ -205,6 +206,7 @@ def stop_browser(self):
205206
"""
206207
if not self._driver:
207208
return
209+
self._driver.close()
208210
self._driver.quit()
209211
self._driver = None
210212

@@ -854,6 +856,17 @@ def print_pdf(self, path=None, print_options=None):
854856
f.write(bytes_file)
855857
return path
856858

859+
def wait_for_downloads(self, timeout: int = 120000):
860+
"""
861+
Wait for all downloads to be finished.
862+
863+
Args:
864+
timeout (int, optional): Timeout in millis. Defaults to 120000.
865+
"""
866+
wait_method = BROWSER_CONFIGS.get(self.browser).get("wait_for_downloads")
867+
# waits for all the files to be completed
868+
WebDriverWait(self._driver, timeout/1000, 1).until(wait_method)
869+
857870
#######
858871
# Mouse
859872
#######

botcity/web/browsers/__init__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,19 @@ class Browser(str, enum.Enum):
2323
Browser.CHROME: {
2424
"driver": "chromedriver",
2525
"class": chrome.Chrome,
26-
"options": chrome.default_options
26+
"options": chrome.default_options,
27+
"wait_for_downloads": chrome.wait_for_downloads
2728
},
2829
Browser.FIREFOX: {
2930
"driver": "geckodriver",
3031
"class": firefox.Firefox,
31-
"options": firefox.default_options
32+
"options": firefox.default_options,
33+
"wait_for_downloads": firefox.wait_for_downloads
3234
},
3335
Browser.EDGE: {
3436
"driver": "msedgedriver",
3537
"class": edge.Edge,
36-
"options": edge.default_options
38+
"options": edge.default_options,
39+
"wait_for_downloads": edge.wait_for_downloads
3740
},
3841
}

botcity/web/browsers/chrome.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,14 @@ def default_options(headless=False, download_folder_path=None, user_data_dir=Non
7878
chrome_options.add_argument("--kiosk-printing")
7979

8080
return chrome_options
81+
82+
83+
def wait_for_downloads(driver):
84+
if not driver.current_url.startswith("chrome://downloads"):
85+
driver.get("chrome://downloads/")
86+
return driver.execute_script("""
87+
var items = document.querySelector('downloads-manager')
88+
.shadowRoot.getElementById('downloadsList').items;
89+
if (items.every(e => e.state === "COMPLETE"))
90+
return items.map(e => e.fileUrl || e.file_url);
91+
""")

botcity/web/browsers/edge.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,14 @@ def default_options(headless=False, download_folder_path=None, user_data_dir=Non
8383
edge_options.add_argument("--kiosk-printing")
8484

8585
return edge_options
86+
87+
88+
def wait_for_downloads(driver):
89+
if not driver.current_url.startswith("chrome://downloads"):
90+
driver.get("chrome://downloads/")
91+
return driver.execute_script("""
92+
var items = document.querySelector('downloads-manager')
93+
.shadowRoot.getElementById('downloadsList').items;
94+
if (items.every(e => e.state === "COMPLETE"))
95+
return items.map(e => e.fileUrl || e.file_url);
96+
""")

botcity/web/browsers/firefox.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,9 +351,24 @@ def default_options(headless=False, download_folder_path=None, user_data_dir=Non
351351
firefox_profile.set_preference('general.warnOnAboutConfig', False)
352352

353353
mimetypes_to_download = ",".join(FIREFOX_MIMETYPES_TO_DOWNLOAD)
354+
firefox_profile.set_preference("pdfjs.disabled", True)
355+
firefox_profile.set_preference("plugin.disable_full_page_plugin_for_types", mimetypes_to_download)
354356
firefox_profile.set_preference('browser.helperApps.neverAsk.saveToDisk', mimetypes_to_download)
355357

356358
firefox_profile.update_preferences()
357359
firefox_options.profile = firefox_profile
358360

359361
return firefox_options
362+
363+
364+
def wait_for_downloads(driver):
365+
print('Start Wait for Downloads')
366+
if not driver.current_url.startswith("about:downloads"):
367+
print('Open the downloads tab')
368+
driver.get("about:downloads")
369+
370+
return driver.execute_script("""
371+
var items = document.querySelector('richlistbox').itemChildren;
372+
if (items.every(e => e.attributes["state"].value != 0))
373+
return true;
374+
""")

0 commit comments

Comments
 (0)