Selenium can't select context of an iframe (intermittently)












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:5nelement.find/</<@chrome://marionette/content/element.js:339:16n"},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.










share|improve this question

























  • 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.

    – Robert Hartley
    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?

    – Robert Hartley
    Nov 16 '18 at 20:48











  • @RobertHartley, I will add my C# code. Hope it helps.

    – Alejandro Haro
    Nov 20 '18 at 14:51
















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:5nelement.find/</<@chrome://marionette/content/element.js:339:16n"},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.










share|improve this question

























  • 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.

    – Robert Hartley
    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?

    – Robert Hartley
    Nov 16 '18 at 20:48











  • @RobertHartley, I will add my C# code. Hope it helps.

    – Alejandro Haro
    Nov 20 '18 at 14:51














2












2








2


1






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:5nelement.find/</<@chrome://marionette/content/element.js:339:16n"},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.










share|improve this question
















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:5nelement.find/</<@chrome://marionette/content/element.js:339:16n"},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.







python selenium testing selenium-webdriver






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 20:16







Robert Hartley

















asked Nov 16 '18 at 18:23









Robert HartleyRobert Hartley

113




113













  • 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.

    – Robert Hartley
    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?

    – Robert Hartley
    Nov 16 '18 at 20:48











  • @RobertHartley, I will add my C# code. Hope it helps.

    – Alejandro Haro
    Nov 20 '18 at 14:51



















  • 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.

    – Robert Hartley
    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?

    – Robert Hartley
    Nov 16 '18 at 20:48











  • @RobertHartley, I will add my C# code. Hope it helps.

    – Alejandro Haro
    Nov 20 '18 at 14:51

















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







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





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.

– Robert Hartley
Nov 16 '18 at 20:14







@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.

– Robert Hartley
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?

– Robert Hartley
Nov 16 '18 at 20:48





@AlejandroHaro Interesting idea - do you have any sample code for selecting a button within an iframe and clicking it?

– Robert Hartley
Nov 16 '18 at 20:48













@RobertHartley, I will add my C# code. Hope it helps.

– Alejandro Haro
Nov 20 '18 at 14:51





@RobertHartley, I will add my C# code. Hope it helps.

– Alejandro Haro
Nov 20 '18 at 14:51












1 Answer
1






active

oldest

votes


















0














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();





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343417%2fselenium-cant-select-context-of-an-iframe-intermittently%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    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();





    share|improve this answer




























      0














      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();





      share|improve this answer


























        0












        0








        0







        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();





        share|improve this answer













        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();






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 14:54









        Alejandro HaroAlejandro Haro

        686




        686






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343417%2fselenium-cant-select-context-of-an-iframe-intermittently%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            鏡平學校

            ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

            Why https connections are so slow when debugging (stepping over) in Java?