question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Selecting elements for screenshotting based on tag content

See original GitHub issue

Selecting elements as the target for screenshots based on CSS selectors does not (currently) allow for the selection of elements based on tag content, or relative to DOM elements selected based on tag content.

However, elements can be selected based on tag content using Javascript. It would be useful to allow for the selection of elements via Javascript as well as CSS.

Alternatively, support a method that can be called from a javascript scraper call that will apply a screen shot to a Javascript selected element.

At the moment, selector based screenshots seem to be focused in _selector_javascript(selectors) by:

https://github.com/simonw/shot-scraper/blob/e40070d9bc5334ad8b2ff9e4e3565263aad545f4/shot_scraper/cli.py#L520

As well as passing --selector(s), s, one approach might be to pass element(s) el returned from a --js-selector script?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:21 (16 by maintainers)

github_iconTop GitHub Comments

1reaction
simonwcommented, Apr 9, 2022

OK, I built a protoype. @psychemedia you can try it out by running:

pip install https://github.com/simonw/shot-scraper/archive/8a61c7e1ba90f1210868cf7f84ece78e5f8300db.zip

Here’s a demo:

shot-scraper https://simonwillison.net/2022/Apr/8/weeknotes/ \
  --js-selector 'el.tagName == "P" && el.innerText.includes("shot-scraper")' \
  --padding 15 --retina

Which produced this image (of the first paragraph on the page to contain the term shot-scraper):

image

Note that I had to include el.tagName == "P" in the expression - because it scans through every tag on the page, so without that it returns the html tag since that includes the shot-scraper text somewhere in its innerText!

The demo also currently outputs debug info showing the JavaScript it generated - I’d remove that before shipping the feature:

% shot-scraper https://simonwillison.net/2022/Apr/8/weeknotes/ \
      --js-selector 'el.tagName == "P" && el.innerText.includes("shot-scraper")' \
      --padding 15 --retina
() => {
Array.from(
  document.getElementsByTagName('*')
).find(el => el.tagName == "P" && el.innerText.includes("shot-scraper")).classList.add("js-selector-a1f5ba0fc4a4317e58a3bd11a0f16b96");
} ['.js-selector-a1f5ba0fc4a4317e58a3bd11a0f16b96']
Screenshot of '.js-selector-a1f5ba0fc4a4317e58a3bd11a0f16b96' on 'https://simonwillison.net/2022/Apr/8/weeknotes/' written to 'simonwillison-net-2022-Apr-8-weeknotes.1.png'
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to capture the screenshot of a specific element rather ...
This program captures the screenshot of the whole page and crop the element based on its location. The element image will be available...
Read more >
I want the screenshot of only a specific element and ... - Edureka
I want the screenshot of only a specific element and not the entire page Is it possible with Selenium Webdriver.
Read more >
How To Select HTML Elements Using ID, Class, and Attribute ...
You can do this broadly with an element selector to select high-level tags, but to gain ... To begin, open index.html in your...
Read more >
Select Page Elements | Basic Guides | Guides | Docs - TestCafe
Select Page Elements. You should identify the target page element to perform an action with it (click, drag, etc.) or check its state...
Read more >
Harvesting the web with rvest - Tidyverse
You can right-click the element you want to inspect and select Inspect or Inspect Element, depending on your browser. This will open Developer...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found