-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bug fixed when making screenshot #2151
Open
MohaMBS
wants to merge
3
commits into
elebumm:master
Choose a base branch
from
MohaMBS:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+61
−23
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,7 +73,7 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int): | |
with sync_playwright() as p: | ||
print_substep("Launching Headless Browser...") | ||
|
||
browser = p.chromium.launch( | ||
browser = p.firefox.launch( | ||
headless=True | ||
) # 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 | ||
|
@@ -99,6 +99,7 @@ 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.wait_for_timeout(timeout=500) | ||
|
||
page.locator(f'input[name="username"]').fill(settings.config["reddit"]["creds"]["username"]) | ||
page.locator(f'input[name="password"]').fill(settings.config["reddit"]["creds"]["password"]) | ||
|
@@ -160,10 +161,13 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int): | |
reddit_object["thread_title"], | ||
to_language=lang, | ||
translator="google", | ||
) | ||
) | ||
|
||
page.wait_for_timeout(2000) #small delay | ||
page.wait_for_selector('shreddit-post > h1', timeout=5000) # we wait for the title to appere | ||
|
||
page.evaluate( | ||
"tl_content => document.querySelector('[data-adclicklocation=\"title\"] > div > div > h1').textContent = tl_content", | ||
"tl_content => document.querySelector('shreddit-post > h1').textContent = tl_content", | ||
texts_in_tl, | ||
) | ||
else: | ||
|
@@ -177,12 +181,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('shreddit-post').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('shreddit-post').screenshot(path=postcontentpath) | ||
except Exception as e: | ||
print_substep("Something went wrong!", style="red") | ||
resp = input( | ||
|
@@ -220,7 +224,7 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int): | |
if page.locator('[data-testid="content-gate"]').is_visible(): | ||
page.locator('[data-testid="content-gate"] button').click() | ||
|
||
page.goto(f"https://new.reddit.com/{comment['comment_url']}") | ||
page.goto(f"https://new.reddit.com{comment['comment_url']}") | ||
|
||
# translate code | ||
|
||
|
@@ -230,30 +234,63 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int): | |
translator="google", | ||
to_language=settings.config["reddit"]["thread"]["post_lang"], | ||
) | ||
|
||
page.wait_for_timeout(timeout=1500) | ||
page.evaluate( | ||
'([tl_content, tl_id]) => document.querySelector(`#t1_${tl_id} > div:nth-child(2) > div > div[data-testid="comment"] > div`).textContent = tl_content', | ||
'([tl_content, tl_id]) => document.querySelector(`#t1_${tl_id}-comment-rtjson-content > div:nth-child(1) > p`).textContent = tl_content', | ||
[comment_tl, comment["comment_id"]], | ||
) | ||
try: | ||
if settings.config["settings"]["zoom"] != 1: | ||
# store zoom settings | ||
if settings.config["settings"]["zoom"] != 1: | ||
# Store zoom settings | ||
zoom = settings.config["settings"]["zoom"] | ||
# 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() | ||
# 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() | ||
for i in location: | ||
location[i] = float("{:.2f}".format(location[i] * zoom)) | ||
page.screenshot( | ||
clip=location, | ||
path=f"assets/temp/{reddit_id}/png/comment_{idx}.png", | ||
) | ||
|
||
# Zoom the body of the page | ||
page.evaluate(f"document.body.style.zoom = {zoom}") | ||
|
||
# Scroll the parent of the comment into view | ||
parent_locator = page.locator(f"#t1_{comment['comment_id']}-comment-rtjson-content").locator('xpath=..') | ||
parent_locator.scroll_into_view_if_needed() | ||
|
||
# Get the bounding box of the parent element | ||
location = parent_locator.bounding_box() | ||
|
||
# Ensure bounding box is valid | ||
if location: | ||
# Adjust for zoom (since zooming doesn't change the div's actual properties) | ||
location['x'] = float("{:.2f}".format(location['x'] * zoom)) | ||
location['y'] = float("{:.2f}".format(location['y'] * zoom)) | ||
location['width'] = float("{:.2f}".format(location['width'] * zoom)) | ||
location['height'] = float("{:.2f}".format(location['height'] * zoom)) | ||
|
||
# Adjust the height to be exactly 200px | ||
location['height'] = 200 | ||
|
||
# Take a screenshot of the parent element with the adjusted height | ||
page.screenshot( | ||
clip=location, | ||
path=f"assets/temp/{reddit_id}/png/comment_{idx}.png", | ||
) | ||
else: | ||
print("Could not get the bounding box of the parent element.") | ||
else: | ||
page.locator(f"#t1_{comment['comment_id']}").screenshot( | ||
path=f"assets/temp/{reddit_id}/png/comment_{idx}.png" | ||
) | ||
# getting the bounding_box | ||
parent_locator = page.locator(f"#t1_{comment['comment_id']}-comment-rtjson-content").locator('xpath=..') | ||
location = parent_locator.bounding_box() | ||
|
||
# bounding_box is valid ? | ||
if location: | ||
# ajust 200px | ||
location['height'] = 200 | ||
|
||
# make screenshot | ||
page.screenshot( | ||
path=f"assets/temp/{reddit_id}/png/comment_{idx}.png", | ||
clip=location # resize to 200px | ||
) | ||
else: | ||
print("No se pudo obtener el cuadro delimitador del elemento.") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please keep all comments in English |
||
except TimeoutError: | ||
del reddit_object["comments"] | ||
screenshot_num += 1 | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://playwright.dev/python/docs/api/class-page#page-wait-for-timeout