Finding DOM elements within a specific rectangular area using headless browsers

I’m working on a project where users can draw rectangular selections on web pages. After they select an area, I need to identify which HTML elements fall within or intersect with that rectangle using headless browser automation tools like PhantomJS or CasperJS.

Basically, I want to capture all DOM elements that are either completely inside the selected rectangle or partially overlapping with it. The user provides the coordinates of the rectangle (x, y, width, height) and I need to programmatically determine which page elements are affected.

Is this kind of element detection possible with headless browsers? What would be the best approach to implement this functionality?

you could try document.querySelector() with css selectors, but that won’t work here. I had success mixing intersection observer api with manual coordinate checks - set up observers on your target elements, then cross-reference with rect coords. it’s hacky but handles dynamic content way better than static boundingrect calls. phantomjs is dead tho, I’d switch to puppeteer or playwright.

I built something like this for a web scraping project last year. Use getBoundingClientRect() through your headless browser’s JavaScript execution. Inject a script that loops through visible elements and checks if their bounding rectangles overlap with your selection area. The overlap logic is simple - two rectangles intersect if one’s left edge is less than the other’s right edge, and one’s top edge is less than the other’s bottom edge, for both axes. Puppeteer works better than PhantomJS here since it uses a newer rendering engine. Watch out for elements with transforms or absolute positioning. You’ll need to factor in scroll position and viewport offset when calculating coordinates. Also decide upfront if you want hidden elements or just visible ones.

Document.elementsFromPoint() is another solid option. Unlike getBoundingClientRect() where you’re iterating through everything, you can sample multiple points in your rectangle and grab elements at those coordinates directly. I set up a grid of test points across the selection area and called elementsFromPoint() for each one. This catches overlapping elements way better, especially with complex layouts or z-index stacking. The catch? Performance takes a hit - more API calls means slower execution. For better coverage, I’d combine both: use elementsFromPoint() for rectangle corners and center points, then verify with getBoundingClientRect() for edge cases. Don’t forget to account for browser zoom and device pixel ratios in your calculations.