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')]")));