0

After reading the switch-to-iframe documentation, I am still not sure what will driver.getPageSource() return after driver.switchTo().frame(frame element) is called.

By Page source, I refer to the definition in getPageSource(). But I can't find anything that confirm src's attribute is the same as page source. I think during the switching, webdriver will send a request to the src url and use the response to create a page source for the iframe, is it correct?

Is an iframe src's attribute the address for storing its page source? In here, it says

The src attribute specifies the address of the document to embed in an iframe.

(1) Anyway, let's say if we have an iframe like below

<iframe src="/video.mp4">
   #document    ----> may or may not exist, not sure if #document create by src or not 
   <html>  
   </html> 
</iframe>

When the driver switch to this iframe,

  • Is the "/video.mp4"'s content use to create the iframe's page source (I think page source is the #document)? But this is a video file, how is it possible to use a video as page source (html)?

  • Or the page source is "/video.mp4"'s conetent + #document?

(2) What if the iframe has a html as src, such as

<iframe src="/file.html">
   #document    ----> may or may not exist, not sure if #document create by src or not 
   <html>  
   </html> 
</iframe>

Does the page source become whatever is in file.html PLUS the #document in iframe? Or the #document will only be created using the content from file.html after driver is switch?

(3) If the iframe has no src, such as

<iframe></iframe>

Can the iframe still has page source? What will getPageSource() return in this case after driver switch to this iframe?

eda
  • 1

1 Answers1

0

getPageSource() gets the source of the last loaded page which is actually the source of the current page. The page source returned is a representation of the underlying HTML DOM.

Where as iframe 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.

The src attribute specifies the address of the document to embed in an iframe.

Syntax:

<iframe src="URL">

Attribute Values:

Value        Description
URL          Specifies the URL of the document to embed in the iframe.

Possible values:

  • An absolute URL - points to another web site (like src="http://www.example.com/default.htm")
  • A relative URL - points to a file within a web site (like src="default.htm")

This usecase

Considering the HTML:

<iframe src="/video.mp4">
   #document    ----> may or may not exist, not sure if #document create by src or not 
   <html>  
   </html> 
</iframe>

Here video.mp4 is the file within the web site which will be embedded within the frame and you can access once you switch to the frame element.


Update

Once you switch to the desired iframe you can again use getPageSource() to extract the DOM Tree of the iframe element.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thank you for the explanation, but you may misunderstood the question. I understand the purpose of iframe and its relationship to its parent page. But I would to know what is a iframe's page source after we switch to it. For example, if a iframe's src attribute contains a mp4, how is getPageSource() (call after switch to the iframe) construct a html dom from a mp4 file? – eda Apr 01 '22 at 12:46
  • Checkout the updated answer and let me know the status. – undetected Selenium Apr 01 '22 at 12:58