I have Selenium tests (written in Python) that find an iframe using its ID, and then looks for a button inside the iframe. The iframe is injected by a browser extension. This is the Python code:
id_ = "my-special-iframe"
driver.switch_to.frame(driver.find_element_by_id(id_))
polling.poll(
target=lambda:driver.find_elements_by_id("button")),
timeout=10,
step=1)
Where an element with the id "button" exists within the iframe but not outside it.
What I observe is the call to switch_to
succeeds, but half of the time the context remains the default context at the top of the DOM. I know this because the result of driver.page_source
prints the whole DOM (as if the default context was selected), and not that of the iframe. Moreover, I can call switch_to
as many times as I want (because, being within the main DOM rather than within the iframe, I can select the iframe). So it follows that Selenium can't find the button element.
Here's the logs from Geckodriver in an instance where Selenium fails to get into the iframe's context:
1542386220903 Marionette TRACE 0 -> [0,25,"WebDriver:FindElement",{"using":"xpath","value":"//iframe[@id='my-special-frame']"}]
1542386220906 Marionette TRACE 0 <- [1,25,null,{"value":{"element-6066-11e4-a52e-4f735466cecf":"47898438-5185-4e1e-ac19-c5220a6f8fcf"}}]
1542386220908 Marionette TRACE 0 -> [0,26,"WebDriver:SwitchToFrame",{"element":"47898438-5185-4e1e-ac19-c5220a6f8fcf"}]
1542386220910 Marionette TRACE 0 <- [1,26,null,{"value":null}]
1542386220912 Marionette TRACE 0 -> [0,27,"WebDriver:FindElement",{"using":"css selector","value":"#button"}]
1542386220914 Marionette TRACE 0 <- [1,27,{"error":"no such element","message":"Unable to locate element: #button","stacktrace":"WebDri ... entError@chrome://marionette/content/error.js:388:5\nelement.find/</<@chrome://marionette/content/element.js:339:16\n"},null]
With the exception of the failure to find the element at the end, the logs look the same in the case where Selenium can select the iframe context.
I see this using both Chromedriver and Geckodriver. Why can't Selenium switch to the correct context sometimes? When I open the Chrome (or Firefox) console, I can see the contents of the iframe there so Selenium should be able to find the element. Does Selenium have a problem with switching to injected iframes?
Previous answers deal with finding elements within the iframe without switching to the iframe first. In this case, I am switching to the iframe but even then I don't get the correct context. No idea why this was marked as duplicate.