WebDriver等待iframe,通过部分名称进行识别



当我只知道一个iframe的部分名称时,我在等待它并切换到它时遇到了问题。每次加载页面时,名称都是唯一的。模式为"Framexxxxxxx",其中"xxxxx"是随机数。

有没有办法让webdriver ExpectedConditions.frameToBeAvailableAndSwitchToIt()找到那个iframe?

到目前为止,我已经尝试过:

By.xpath(".//*[starts-with(name(),'Frame')]")
By.xpath("iframe[starts-with(@name,'Frame')]")

还有其他方式——运气不好。

提前谢谢。

尝试使用以下定位器

By.xpath("//*[starts-with(@name,'Frame')]")
By.xpath("//iframe[starts-with(@name,'Frame')]")
By.cssSelector("iframe[name*='Frame']")

您可以使用相同的定位器来等待iframe出现/可用。

使用CsSelector来识别iframe。。如果只有一个iframe更好,我认为最好使用这个。。

driver.switchTo().frame(driver.findElement(By.cssSelector("iframe")));

我在等待iframe加载时遇到了一些问题,所以我尝试了这个:

WebDriverWait wait = new WebDriverWait(driver, 60);      
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[contains(@name,'Frame')]")));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.tagName("body")));
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
  • 它等待iframe加载和切换
  • 它等待他的身体加载
  • 此外,请等待3秒以上才能完全渲染

最新更新