Home > Blockchain >  Can't access in any way dynamically generated element with Selenium
Can't access in any way dynamically generated element with Selenium

Time:10-20

I've been trying to modify the router SSIDs through a script made in Selenium, the problem is that I can't get any JS element that's generated by the router page. I've been trying with the Expected Conditions and whatever you could think of but without success.

Example of commands that I used:

element: WebElement = WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.XPATH, xpath)))

I investigated further by trying to do some queries with JavaScript in the Chrome's "Console" tab. What I found is that if I try to select any element using any query selector before inspecting any element on the page it won't work.

Example query:

document.querySelector("#WIFIIconInfo")

How can I fix this weird behaviour?

EDIT: Don't know if these are useful informations, but they are still information. The website is built off these 2 things:

Web frameworks
Microsoft ASP.NET

JavaScript libraries
jQuery - 1.11.1

(Wappalyzer Output)

EDIT 2: I looked up into the HTML as suggested by @JeffC and found out that the div I wanted to click was inside the following iframe.

<iframe id="menuIframe" frameborder="0" width="100%" height="100%" marginheight="0" marginwidth="0"  scrolling="no" overflow="hidden" src="CustomApp/mainpage.asp"></iframe>

His solution was indeed right.

CodePudding user response:

What I found is that if I try to select any element using any query selector before inspecting any element on the page it won't work.

That tells me that your desired element is inside of an IFRAME. When you inspect an element on the page, the dev tools automatically switch the context to the containing IFRAME.

Using Selenium, you will need to switch into the IFRAME, interact with whichever elements are inside the IFRAME, and then switch context back out to the main page.

We don't have any relevant HTML so here's a generic example.

# wait for the IFRAME and switch to it
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe")))
# do stuff to elements in the IFRAME
...
# switch back to the main content of the page
driver.switch_to.default_content()
  • Related