Open sno2 opened 4 years ago
Sounds like what you're asking for is for the return type to be NodeListOf<HTMLAnchorElement>
, like it is in dom.lib.d.ts
. Current behavior seems like a bug.
Yeah, sorry, I am trying to understand all of this ;)
This should be fairly easy to fix. I'll try to make the time for fixing this later today 👍
Thank you very much! I just started working on a scraper with Deno and that really threw me off ;)
I guess I could give an update on this. This wasn't as easy as I initially thought. I've been working on it. The parser doesn't disambiguate between different element types, and so I must fix that first. Doing that is a much larger task, so you'll be waiting for my PR for a while longer.
I think I have encountered this issue as well, while getting a list of Halloween emoji:
halloween-emoji.ts
:Specifically:
// line 14
const elements = [...document.querySelectorAll('body > div.container > div.content > ul > li')] as Element[];
// line 17
const anchor = element.querySelector(':scope > a'); // requires assertion on elements array
If I don't include the assertion, I am presented with this error:
Property 'querySelector' does not exist on type 'Node'. ts(2339)
Also, it seems that the Element type doesn't provide JS property access to attributes, for example HTMLAnchorElement.href
;
const url = anchor?.href;
Property 'href' does not exist on type 'Element'. ts(2339)
Edit: Here's a workaround: https://github.com/b-fuze/deno-dom/issues/72#issuecomment-1250615385
Hi, I am having the same issue trying to get all img tags and later reading its properties
I started to use the dom types that are available by default with TypeScript instead of the types provided by deno-dom and didn't have any issue yet: https://github.com/lumeland/lume/blob/main/core/utils/dom.ts#L28
Would it not be more ideal to have the
querySelectorAll
method return anElementList
instead of aNodeList
? I think this because thequerySelector
method returns a singleElement
. You are able to do this in the web, so I think it should definitely be integrated into this module. Also, this would make it easier by being able to do the following:It would bring this module into the view of not only parsing and using the html, but adding ease to use it with web scraping. Great job on the module!