如何切换到页面上的顺序iframe

时间:2015-03-05 21:16:52

标签: selenium iframe selenium-webdriver

我有一个场景,我需要在动态更改的iframe中点击图片。我只能点击页面上的第一张图片。之后脚本无法识别iframe,并且给了我TimeoutException。这是我的脚本:

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

然后我切换到页面上的下一个iframe

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

1 个答案:

答案 0 :(得分:2)

在切换到下一帧之前,您需要切换到默认内容

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