0

I'm trying to automate my course registration. Everything is running smoothly until I arrive at this Term Select page: element not found with doccument.getElementsByTagName('input')

The function document.getElementsByTagName('input') returns 10 elements, none of which are the desired radio button. This perplexes me as the element is clearly loaded. document.getElementByID() also doesn't work/ returns null.

Then, when I use chrome's "Select an element in the page to inspect it" tool, running the same function yields different results. element now found with doccument.getElementsByTagName('input')

Now the function returns 31 elements, including the desired radio input. document.getElementByID() also works/ returns the desired element.

Upon further investigation, I see that many elements are not shown as descendants of document before the devTool inspection. document.child0.child1.child2 shows as having 1 descendent before inspection, then 12 after

Can anyone explain what's going on? How can I programmatically select this element after the page is loaded? I'm quite perplexed. Thanks!

EDIT: NVM. Today I learned iframes exist!

  • 1
    The **only** reason `getElementById` (note: lowercase `d` at the end) returns `null` is if the element is not in the DOM when you make the call. Some code is apparently adding the elements *later*, after you make the call but before you inspect in devtools. You can watch for DOM modifications via a [`MutationObserver`](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver). – T.J. Crowder Apr 21 '21 at 13:13
  • Thanks for the Speedy response T.J.! Sorry if these are stupid questions - how can an element be visible on the page but not in the DOM? Is there any way to refresh the DOM to make it aware of these elements' existence? Thanks again – Omri Shavit Apr 21 '21 at 13:21
  • It can't. You're just looking for it too soon. – T.J. Crowder Apr 21 '21 at 13:25
  • Sorry to keep bothering you. I tried placing a MutationObserver before inspecting in devTools, but it isn't detecting any mutations of the DOM during the inspection. Moreover, I can even manually interact with the radio button before searching the DOM to no avail. Is it really impossible that I'm looking for it after it's loaded / it's somehow hidden? Also, going on airplane mode crudely proves that the server isn't doing any shenanigans. Is there something else I'm not understanding? Thanks again, T.J. – Omri Shavit Apr 21 '21 at 14:45
  • 1
    There's nothing more I can tell you other than: `getElementById` is not broken (to misquote the Pragmatic Programmer). If it's not finding the element, the element **is not there** as of when you called it (or doesn't have that ID on it). It's being added later, or something is setting its ID. There are things in programming that are nuanced or subtle, but this doesn't happen to be one of those things. :-) I hope you find out what is adding it! – T.J. Crowder Apr 21 '21 at 14:47

0 Answers0