0
votes

One of my automation case has been blocked for more than 1 week, there is a lazy loaded iframe (clicking a link will insert this iframe into DOM), my code is like below:

driver.get("my url");
driver.findElement(By.linkText('REGISTER')).click();
driver.wait(function() {
    console.log("WAITING FOR EXTERNAL IFRAME");
    return driver.findElement(By.css("div#main-container > iframe")).isDisplayed();
}, 8000, "WAIT TIMEOUT!!!!");
driver.switchTo().frame(1); // The iframe has no id and I have no control
// Manipulating in the iframe

Whenever I manually do the operation above, I am 100% sure after clicking the "REGISTER" button, there are totally two iframes on the page, i.e. if I do document.getElementsByTagName("iframe"), I can get two iframes! But during the automation running, it just always fails, I really don't know why, and more important, I did see debug tips for protractor and webdriverjs, however, I didn't figure out a good way to do the DOM query in the REPL, all because of webdriverjs' promise control flow, any experienced debugging tips here are highly appreciated!!

3

3 Answers

0
votes

You can handle iFrame using three methods:

  1. driver.switchTo().frame(1);
  2. driver.switchTo().default; - it will switch to the default iframe if its id isn't provided
  3. driver.switchTo().index(0); - it will switch to default first index
0
votes

Updated:There are 2 spaces in HTML with iframe : in of iframe, out of iframe.In of iframe there is a child document, child head, child body. When we have a iframe or many iframe, we want to do smt in iframe, we can: Using Element (widely used) or Using Index

  1. Option 1

driver = browser.driver;

var iframeElement = element(by.css('path_to_iframe');

browser.switchTo().frame(iframeElement.getWebElement());

driver.findElement(by.tagName('body')).click();  

driver.findElement(by.tagName('body')).sendKeys(string);
  1. Option 2 With first iframe: index is 0, second iframe index is 1, and go on...

browser.switchTo().frame(0);

browser.switchTo().frame(1);

Every done with a iframe, you need to switch out of the iframe. you need :

browser.switchTo().defaultContent();
-1
votes

I work in Selenium-java. The following code worked perfectly for me.

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("https://developer.autodesk.com/");

driver.findElement(By.id("btn_oxygen_registration")).click();

WebElement iframe = driver.findElement(By.xpath("//iframe[contains(@src,'https://accounts.autodesk.com/register')]"));
driver.switchTo().frame(iframe);
driver.findElement(By.id("firstname_str")).sendKeys("EnterUsername");

Let me know if this helps you.