1
System.out.println("iframe source::"+driver.findElements(By.tagName("iframe")).get(1).getText());

I have tried the above code but it is returning empty.

driver.getPageSource() will return HTML code of all frames but I need the html content of current frame.

  • You can use executeScript: document.querySelectorAll('IFRAME')[0].contentDocument – Barney Jan 23 '18 at 15:23
3

You need to switch to the iFrame first and then do a driver.getPageSource().

WebElement iFrame = driver.findElements(By.tagName("iframe").get(1);
driver.switchTo().frame(iframe);
driver.getPageSource();
driver.switchTo().defaultContent();
1

Based on excellent response by Alok, the driver.getPageSource(); gave me enough detail to navigate within the i-Frame using:

    WebElement iFrame = driver.findElements(By.tagName("iframe")).get(6); 
          driver.switchTo().frame(iFrame);
//        System.out.println(driver.getPageSource());
          System.out.println(driver.findElement(By.xpath("//div[@class='description']")).getText());

//To click button 'Ill Do this later' in the popup
          driver.findElement(By.xpath("//button[@id='deny']")).click();
          driver.switchTo().defaultContent();

//This is my (partial) getpageSource() output to create xpath: 
<div class=" webpush-prompt"><div class="wrapper"><div class="table content-table"><div class="tablerow"><div id="webpush-prompt" class="container tablecell"><div class="description-container"><div class="description">makemytrip.com wants to start sending you push notifications. Click <b>Allow</b> to subscribe.</div></div></div></div></div>
<div class="button-group-custom clearfix"><button id="deny" class="button close">I'll do this later</button><button id="allow" class="button">Allow</button></div><div class="we-beak"></div></div></div></body></html>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.