familyGuy
familyGuy

Reputation: 435

How to switch to sequential iframes on the page

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

Upvotes: 1

Views: 603

Answers (1)

alecxe
alecxe

Reputation: 473833

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

Upvotes: 2

Related Questions