fix the Timeout during the screenshots

pull/1960/head
Ali Abusaleh 2 years ago
parent 7bc1d2fa8f
commit d4bed8d0bf

@ -75,7 +75,7 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int):
print_substep("Launching Headless Browser...")
browser = p.chromium.launch(
headless=True
headless=False
) # headless=False will show the browser for debugging purposes
# Device scale factor (or dsf for short) allows us to increase the resolution of the screenshots
# When the dsf is 1, the width of the screenshot is 600 pixels
@ -99,7 +99,6 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int):
page.goto("https://www.reddit.com/login", timeout=0)
page.set_viewport_size(ViewportSize(width=1920, height=1080))
page.wait_for_load_state()
page.locator('[name="username"]').fill(settings.config["reddit"]["creds"]["username"])
page.locator('[name="password"]').fill(settings.config["reddit"]["creds"]["password"])
page.locator("button[class$='m-full-width']").click()
@ -177,12 +176,12 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int):
# zoom the body of the page
page.evaluate("document.body.style.zoom=" + str(zoom))
# as zooming the body doesn't change the properties of the divs, we need to adjust for the zoom
location = page.locator('[data-test-id="post-content"]').bounding_box()
location = page.locator(f'#t3_{reddit_id}').bounding_box()
for i in location:
location[i] = float("{:.2f}".format(location[i] * zoom))
page.screenshot(clip=location, path=postcontentpath)
else:
page.locator('[data-test-id="post-content"]').screenshot(path=postcontentpath)
page.locator(f'#t3_{reddit_id}').screenshot(path=postcontentpath)
except Exception as e:
print_substep("Something went wrong!", style="red")
resp = input(
@ -213,6 +212,8 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int):
"Downloading screenshots...",
)
):
print_substep(f"Downloading screenshot {idx + 1} of {screenshot_num}...")
print_substep(f"Comment: {comment['comment_body']}")
# Stop if we have reached the screenshot_num
if idx >= screenshot_num:
break
@ -241,9 +242,9 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int):
# zoom the body of the page
page.evaluate("document.body.style.zoom=" + str(zoom))
# scroll comment into view
page.locator(f"#t1_{comment['comment_id']}").scroll_into_view_if_needed()
page.locator(f"#t1_{comment['comment_id']}-comment-rtjson-content").scroll_into_view_if_needed()
# as zooming the body doesn't change the properties of the divs, we need to adjust for the zoom
location = page.locator(f"#t1_{comment['comment_id']}").bounding_box()
location = page.locator(f"#t1_{comment['comment_id']}-comment-rtjson-content").bounding_box()
for i in location:
location[i] = float("{:.2f}".format(location[i] * zoom))
page.screenshot(
@ -251,7 +252,7 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int):
path=f"assets/temp/{reddit_id}/png/comment_{idx}.png",
)
else:
page.locator(f"#t1_{comment['comment_id']}").screenshot(
page.locator(f"#t1_{comment['comment_id']}-comment-rtjson-content").screenshot(
path=f"assets/temp/{reddit_id}/png/comment_{idx}.png"
)
except TimeoutError:

Loading…
Cancel
Save