1

I have a scenario, where I need to click an image within dynamically changing iframes. I am able to click ONLY on the first image on the page. The script does not recognize the iframe afterwards, and gives me TimeoutException. This is my script below:

//get the First iframe 
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//iframe[contains(@id, 'adnxs_tag_')]")));
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[contains(@id, 'adnxs_tag_')]")));

Then I switch to the next iframe on the page

//get the second iframe 
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//iframe[contains(@id, '336')]")));
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[contains(@id, '336')]")));
familyGuy
  • 425
  • 2
  • 5
  • 22

1 Answers1

2

You need to switch to the default content before switching to the next frame:

driver.switchTo().frame(driver.findElement(By.xpath("//iframe[contains(@id, 'adnxs_tag_')]")));

driver.switchTo().defaultContent();

driver.switchTo().frame(driver.findElement(By.xpath("//iframe[contains(@id, '336')]")));
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • @familyGuy just to make sure: are you waiting for the second iframe after switching to the default content? – alecxe Mar 05 '15 at 23:50
  • I added this, but still getting timeout. Wait is 20. driver.switchTo().defaultContent(); wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//iframe[contains(@id, 'adxns')]"))); driver.switchTo().frame(driver.findElement(By.xpath("//iframe[contains(@id, 'adnxs')]"))); – familyGuy Mar 06 '15 at 00:32
  • @familyGuy first, please recheck your xpath. Also, is the second frame located on the same level as the first one, or are they nested? Thanks. – alecxe Mar 06 '15 at 00:35
  • 1
    I asked the developer to add some static id to the div. It was just a waste of time to capture dynamic iframes on this page. Thanks for all of your help!, and your answer is definitely acceptable. – familyGuy Mar 06 '15 at 00:41