-
-
Notifications
You must be signed in to change notification settings - Fork 77
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
--wait-for / wait_for mechanism for waiting until a JavaScript expression returns true #72
Comments
Interesting idea. It might be possible to support this as:
Since it looks like There's a default timeout of 30s. Not sure if it's worth having an extra |
I like |
Built a simple test page: <html>
<body><h1>Here it comes...</h1>
<script>
setTimeout(() => {
var div = document.createElement('div');
div.innerHTML = 'DIV after 2 seconds';
document.body.appendChild(div);
}, 2000);
</script>
</body>
</html> Used this for a prototype:
Here's the prototype: diff --git a/shot_scraper/cli.py b/shot_scraper/cli.py
index f2c96c6..3d238ae 100644
--- a/shot_scraper/cli.py
+++ b/shot_scraper/cli.py
@@ -117,6 +117,7 @@ def cli():
@click.option(
"--wait", type=int, help="Wait this many milliseconds before taking the screenshot"
)
+@click.option("--wait-for", help="Wait until this JS expression returns true")
@click.option(
"--timeout",
type=int,
@@ -151,6 +152,7 @@ def shot(
retina,
quality,
wait,
+ wait_for,
timeout,
interactive,
devtools,
@@ -198,6 +200,7 @@ def shot(
"height": height,
"quality": quality,
"wait": wait,
+ "wait_for": wait_for,
"timeout": timeout,
"padding": padding,
"retina": retina,
@@ -637,6 +640,7 @@ def take_shot(
output = filename_for_url(url, ext="png", file_exists=os.path.exists)
quality = shot.get("quality")
wait = shot.get("wait")
+ wait_for = shot.get("wait_for")
padding = shot.get("padding") or 0
selectors = shot.get("selectors") or []
@@ -673,10 +677,14 @@ def take_shot(
if wait:
time.sleep(wait / 1000)
+
javascript = shot.get("javascript")
if javascript:
_evaluate_js(page, javascript)
+ if wait_for:
+ page.wait_for_function(wait_for)
+
screenshot_args = {}
if quality:
screenshot_args.update({"quality": quality, "type": "jpeg"})
|
I decided to evaluate any JavaScript from |
Documentation is now live here: https://shot-scraper.datasette.io/en/latest/screenshots.html#waiting-until-a-specific-condition |
The Playwright
.wait_for_function()
allows you to wait for a certain condition to met before progressing to the next step.It would be useful to support this sort of conditional delay when there is uncertainty about what an appropriate explicit
--wait
delay might be.The text was updated successfully, but these errors were encountered: