#document iFrame access using Playwright - iframe

How to access the virutal document #document contents using playwright ? I tried using iFrame and Pagelocator. However, I am unable to reach document location.
Is there an option in Playwright to approach this?
This is the page URL - https://sites.google.com/view/pinnednote/home

Yes, you have frame_locator for python or frameLocator for javascript.
Here an example in your page (Using python)
# Import needed libs
from playwright.sync_api import sync_playwright
# We initiate the playwright page
p = sync_playwright().start()
browser = p.chromium.launch(headless=False)
context = browser.new_context()
page = context.new_page()
# Navigate
page.goto("https://sites.google.com/view/pinnednote/home")
# We get the first iframe
iframe1 = page.frame_locator("//iframe[#jsname='WMhH6e']")
# We get the iframe inside the first iframe
iframe2 = iframe1.frame_locator("#innerFrame")
# We get the iframe inside the second iframe
iframe3 = iframe2.frame_locator("#userHtmlFrame")
# We print the title of this third iframe
print(iframe3.locator("//title").inner_text())
The page has a lot of iframes being honest.
About managing frames with playwright: Playwirght frameLocator

Related

Stop fancy box 3 opening when iframe URL fails to load

I'd like to be able to redirect to a given URL if it fails to load with Fancybox 3. The users supply the URL's so it could be a youtube video, image, or some other arbitrary link. In the case of something like a Google Doc, google prevents you from loading those inside iframes, so I'd like to catch that error and stop the fancy box viewer from loading at all and instead redirect to that URL directly in the browser. I can kind of get it working but I can't seem to stop the fancy box dialog from showing before the redirect happens:
$.fancybox.defaults.afterLoad = (instance, current) ->
if current.hasError
window.location = current.src
instance.close()
I've tried returning false.
This is the best I have come up with so far:
$.fancybox.defaults.defaultType = 'iframe'
$.fancybox.defaults.beforeLoad = (instance, current) ->
$.ajax
url: current.src
method: 'HEAD'
.catch ->
window.location = current.src
instance.close()
The default for a URL is ajax if it fails all the other tests (like image and video), so we need to switch this to iframe first. Then I do an ajax HEAD call to see if the result is successful, if not, we can just redirect to the src. instance.close() is the best way I could find to stop the fancybox from loading (it could already be loaded if this is a slideshow/gallery anyway). There is a brief flash before the page then redirects to the URL.
As #misorude mentions, there isn't a way to detect if the iframe failed to load for cross site requests. In the end I decided to do away with previewing off-site links completely and do a redirect like so:
$.fancybox.defaults.afterLoad = (instance, slide) ->
if !slide.redirecting && slide.contentType == 'html'
slide.redirecting = true
message = """<div class="fancybox-error"><p>Redirecting...</p></div>"""
instance.setContent slide, message
window.location = slide.src
This displays a nice redirecting message and then sends the user on to that link via the browser. contentType is only html when it's not image, video, map etc... from the other media type plugins. This means fancybox can still show youtube links without trouble even though these are iframe and html based.

How can I get the current PDF page number from PDF.js iframe?

I have the viewer hosted on my local webserver and the iframe points and loads up the pdf.
I then want to press a button that 'logs' the page number to a text file, I read this question and answer which seems to suggest you can use pdf.getPage to get the page number but how can i access the PDFJS when it is being ran from inside of the iframe?
Thank you.
var iFrame = document.getElementById('iframe_id');
if ( iFrame.contentDocument ) {
currentPageNum= iFrame.contentDocument.getElementById('pageNumber').value;
}
alert(currentPageNum);
This will access the iframe and then search the viewer.html for the element that contains the page number. All that needs doing now is to pass currentPageNum to the form.

Selenium iframe selection issue

I'm testing our website login sequence with selenium. When I click login it loads an iframe, and the iframe id value changes each time it is loaded. So the test case fails in selenium.
What is the the command to read only the dynamic part of the iframe and locate it during the test run?
try this for capturing the popup,
String parentWindowHandle = browser.getWindowHandle(); // save the current window handle.
WebDriver popup = null;
Iterator<String> windowIterator = browser.getWindowHandles();
while(windowIterator.hasNext()) {
String windowHandle = windowIterator.next();
popup = browser.switchTo().window(windowHandle);
}
Hope it will helpful to you
Use FrameIndex to switch iframes.
driver.switchTo().frame(1);
or use xpath
driver.switchTo().frame(webElement)
Or share Html Code so we can give a better solution.

stoping flash file from running more than one time

I am working with a flash-template to create a website. I have a master page with a flash header and flash menu. when my home pages runs the flash header runs. the flash file should just run once but it is running each time I click on the menu items. I tried to use updatepanel, but i didn't know how to set the updatemode since my menu is working with flash. any idea of what i should use???
Store some data in the client that says they've viewed the flash before. When you execute your flash file, check for the existence of this data, and react accordingly. You may want to peak at http://www.flash-db.com/Tutorials/saving/ on how to save data.
You could use local storage, as shown here :
Write it as :
import flash.net.SharedObject;
var so:SharedObject = SharedObject.getLocal("userData");
so.data.isUsed= "true";
so.flush(); // writes changes to disk
Read it as :
import flash.net.SharedObject;
var so:SharedObject = SharedObject.getLocal("userData");
var isUsed = so.data.isUsed;
If it's a website page you may also use ExternalInterface.addCallback to add a function which can be called from javascript.

Apply "onclick" to all elements in an iFrame

How do I use the JavaScript DOM to apply onclick events to links inside of an iframe?
Here's what I'm trying that isn't working:
document.getElementById('myIframe').contentDocument.getElementsByTagName('a').onclick = function();
No errors seem to be thrown, and I have complete control of the stuff in the iframe.
Here is some code to test and see if I can at least count how many div's are in my iframe.
// access body
var docBody = document.getElementsByTagName("body")[0];
// create and load iframe element
var embed_results = document.createElement('iframe');
embed_results.id = "myIframe";
embed_results.setAttribute("src", "http://www.mysite.com/syndication/php/embed.php");
// append to body
docBody.appendChild(embed_results);
// count the divs in iframe and alert
alert(document.getElementById("myIframe").contentDocument.getElementsByTagName('div').length);
It is possible for an iFrame to source content from another website on a different domain.
Being able to access content on other domains would represent a security vulnerability to the user and so it is not possible to do this via Javascript.
For this reason, you can not attach events in your page to content within an iFrame.
getElementsByTagName returns a NodeCollection, so you have to iterate throgh this collection and add onclick handler to every node in that collection. The code below should work.
var links = document.getElementById('myIframe').contentDocument.getElementsByTagName('a');
for(var i=0;i<links.length;++i)links[i].onclick=function(){}
also make sure, you run this code after the frames' content is loaded
embed_results.onload=function(){
// your code
}

Resources