[英]I want to check whether frame exists or not on web page by using Selenium, how can do it
After login to my application, sometimes a frame opens, which requires a click on the 'OK' button.登录到我的应用程序后,有时会打开一个框架,这需要单击“确定”按钮。 So, I have written the code below which switches to the frame, clicks on the OK button and again switches to default.
因此,我编写了下面的代码,用于切换到框架,单击“确定”按钮并再次切换到默认值。
driver.switchTo().frame(driver.findElement(By.id("InlineDialog_Iframe")));
driver.findElement(By.id(prop.getProperty("pending_email_close_btn_id"))).click();
driver.switchTo().defaultContent();
But, if frame doesn't appear then code gives error saying that the frame doesn't exist.但是,如果框架没有出现,那么代码会给出错误,说框架不存在。
Please let me know how can I check whether the frame exists or not by using 'if' loop or by any other method?请让我知道如何使用“if”循环或任何其他方法检查框架是否存在?
Thanks.谢谢。
hi there i had to do some stuff in the past regards to iframes as well and it was very confusing for me as well,嗨,过去我也不得不做一些关于 iframe 的事情,这也让我感到非常困惑,
so the very first within you need to understand is that the iframe is actually a web page "inside other web-page" due so you will need to switchTo().frame(...stuff..)
to be able to do something因此,您首先需要了解的是 iframe 实际上是“其他网页内部”的网页,因此您需要
switchTo().frame(...stuff..)
才能执行某些操作
than when you finish you need to get the initial frame driver.switchTo().defaultContent();
比完成时需要获取初始帧
driver.switchTo().defaultContent();
so you king of move from one page to the other所以你是从一页移动到另一页的王者
regards to find the element which you are after in the iframe i suggest you find any element which will always be present and wrap it with a try关于在 iframe 中找到您所追求的元素,我建议您找到任何始终存在的元素并尝试将其包装起来
i also suggest you to create a class which will hold your code and wait for JS, Angular, Jquery ...我还建议您创建一个类来保存您的代码并等待 JS、Angular、Jquery ...
example from my code:我的代码中的示例:
try {
driver.switchTo().frame(driver.findElement(By.cssSelector("iframe[id^='xdm_default']")));//change focus to iframe
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.tagName("html"))));//wait for html in the iframe
WebElement divCardsGrid = wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.cssSelector("div[class^='CardsGrid']"))));
if (!divCardsGrid.findElements(By.tagName("a")).isEmpty()) {//check for external links
} catch (WebDriverException e) {
log.error(e.getMessage());
}
//change the focus to the initial frame
driver.switchTo().defaultContent();
I hope that helps我希望有帮助
List<WebElement> iframes = driver.findElements(By.tagName("iframe"));<br/>
if(iframes.size() > 0)<br/>
{<br/>
// Frames is Present <br/>
driver.switchTo().frame("a077aa5e"); //switching the frame by ID<br/>
driver.findElement(By.id(prop.getProperty("pending_email_close_btn_id"))).click();<br/>
driver.switchTo().defaultContent();<br/>
}else{<br/>
// No Frames <br/>
driver.findElement(By.xpath("html/body/a/img")).sendKeys(“----------”);<br/>
}<br/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.