From cd12419b47682ba36d117b9d2fc84648af7a21ff Mon Sep 17 00:00:00 2001 From: EmaMaker Date: Thu, 17 Feb 2022 15:32:20 +0100 Subject: [PATCH] page viewer: fix 'empty range for randrange() (0, 0, 0)' when no clickable elements are found --- page_viewer.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/page_viewer.py b/page_viewer.py index 0b6c4e9..3828190 100644 --- a/page_viewer.py +++ b/page_viewer.py @@ -47,17 +47,17 @@ def visit_page(driver, url): if try_to_click_elements(driver, get_clickable_ads(driver)): time.sleep(4) break - + driver.quit() def try_to_click_elements(driver, clickable_elements): while True: - index = random.randint(0, len(clickable_elements) - 1) - if len(clickable_elements) == 0: print("All the elements weren't clickable!") return False + index = random.randint(0, len(clickable_elements) - 1) + try: print("trying to click element", clickable_elements[index]) clickable_elements[index].click() @@ -70,7 +70,7 @@ def try_to_click_elements(driver, clickable_elements): def get_clickable_ads(driver): searchable_ads = [frame for frame in driver.find_elements_by_xpath('.//iframe') ] id_ok_ads = [frame for frame in searchable_ads if ('ad' in frame.get_attribute('id') or 'google_ad' in frame.get_attribute('id')) and frame.is_displayed() ] - print(id_ok_ads) + print(f"Ads that can be clicked {id_ok_ads}") return id_ok_ads @@ -122,8 +122,8 @@ if __name__ == "__main__": while True: if len(threads) < NO_PROCESSES: - print(f"[BOT] Starting thread n.{len(threads)}/{NO_PROCESSES}") - t1 = threading.Thread(target=launch_and_visit, args=(False, 'https://giangillorossi.altervista.org', False)) + print(f"[BOT] Starting thread n.{len(threads)+1}/{NO_PROCESSES}") + t1 = threading.Thread(target=launch_and_visit, args=(True, 'https://giangillorossi.altervista.org', True)) t1.start() threads.append(t1) @@ -132,7 +132,7 @@ if __name__ == "__main__": while i < len(threads): # print(i) if not (threads[i].is_alive()): - print(f"[BOT] Thread n.{i} has stopped, removing from list") + print(f"[BOT] Thread n.{i+1} has stopped, removing from list") threads.pop(i) else: