2

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.

  • Have you tried running JavaScript executor for Selenium to interact with the button?. I had a similar problem trying to interact with a form inside an iframe, but switching between frames worked fine for me. – Alejandro Haro Nov 16 '18 at 18:40
  • Are you sure that driver.page_source changes when you switch browsing contexts? – Metareven Nov 16 '18 at 18:41
  • @Metareven It doesn't. That's the problem: if browsing context switched correctly, I'd expect to get the iframe's page source, not the whole page's. – Veli Akiner Nov 16 '18 at 20:14
  • @AlejandroHaro Interesting idea - do you have any sample code for selecting a button within an iframe and clicking it? – Veli Akiner Nov 16 '18 at 20:48
  • @RobertHartley, I will add my C# code. Hope it helps. – Alejandro Haro Nov 20 '18 at 14:51
  • There's part of the steps that the remote end performs that sheds light on the problem: https://w3c.github.io/webdriver/#switch-to-frame The fifth remote end step will result in switching to the top-level context(which is what we see here), if the `id` parameter is null. So it's possible that the cause here is that the parameter is null. Why that would be is another question. – Veli Akiner Nov 29 '18 at 17:16
  • A Chromedriver bug has been raised for this: https://bugs.chromium.org/p/chromedriver/issues/detail?id=2781 – Veli Akiner Feb 19 '19 at 11:06

1 Answers1

1
wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("formstack")));

        driver.SwitchTo().Frame("formstack");
        var FirstName = driver.FindElement(By.CssSelector("#field58374948"));
        FirstName.Click();
        FirstName.SendKeys("Alejandro");
        driver.SwitchTo().defaultContent();