Ways to deal with #document under iframe

前端 未结 2 1663
不知归路
不知归路 2020-11-21 04:44

For the portal I am testing now, I came with the problem that I could not create any xpath locators, after some time I figured out that it was because of an \'#document\', t

2条回答
  •  滥情空心
    2020-11-21 05:28

    inline frames

    As per the documentation in Using inline frames, an inline frame is a construct which embeds a document into an HTML document so that embedded data is displayed inside a subwindow of the browser's window. This does not mean full inclusion and the two documents are independent, and both them are treated as complete documents, instead of treating one as part of the other.


    iframe structure and details

    • Generally, an iframe element is in the form of:

      
      
    • Browsers which support iframe display the document referred to by the URL in a subwindow, typically with vertical and/or horizontal scroll bars. Such browsers ignore the content of the iframe element (i.e. everything between the start tag and the end tag ). Browsers which do not support iframe (or have such support disabled) does the opposite, i.e. process the content as if the and tags were not there. Thus, the content matters, despite being ignored by some browsers.

    • So to summarize, inline frames do not mean an include feature, although it might sometimes serve similar purposes.

    • Note that, when inline frames are used, the browser (if it supports them) sends a request to the server referred to by the URL in the iframe element, and after getting the requested document displays it inside an inline frame. In this sense inline frames are a joint browser-server issue, but only the browser needs to be specifically iframe-aware; from the server's point of view, there's just a normal HTTP request for a document, and it sends the document without having (or needing) any idea on what the browser is going to do with it.


    Something Smarter

    As per the best practices while switching to an iframe you need to induce WebDriverWait as follows:

    • Switch through Frame Name (Java Sample Code):

      new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.name("frame_name")));
      
    • Switch through iframe XPath (Python Sample Code):

      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='ptifrmtgtframe' and @name='TargetContent']")))
      
    • Switch through iframe CssSelector (C# Sample Code):

      new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt(By.CssSelector("iframe#twitter-widget-0")));
      

    Reference

    You can find a couple of relevant discussions in:

    • Python: How can I select a html element no matter what frame it is in in selenium?
    • Java: Is it possible to switch to an element in a frame without using driver.switchTo().frame(“frameName”) in Selenium Webdriver Java?
    • C#: How to wait for a frame to load before locating an element?

    tl; dr

    Inline frames vs. normal frames

提交回复
热议问题