简体   繁体   English

我想使用Selenium检查网页上是否存在框架,该怎么做

[英]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.

相关问题 如何使用Mongodb和JAVA检查文档是否存在? - How do i check whether the document exists using Mongodb and JAVA? Selenium Web驱动程序-如何检查页面中是否所有产品都已加载 - Selenium Web Driver - How to check whether all the products in a page loads 如何检查Selenium中是否存在webelement? - How can I check if webelement exists in Selenium? 如何使用Java检查SQLite数据库文件是否存在? - How do I check whether a SQLite database file exists using Java? 如何查看MongoDB中是否存在某个字段? - How can I check whether a field exists or not in MongoDB? 如何检查文件是否存在以及是否存在,然后我想在Java中的finally块中将其关闭? - How to check whether a file exists or not and if it exist then i want to close it in finally block in java? 如何切换到框架并使用Web驱动程序(Selenium-Java)单击弹出窗口? - How do I switch to a Frame and click on the pop up using web driver (Selenium - Java)? 当我在“搜索”按钮中输入数据时,如何使用硒编写逻辑来检查数据是否显示 - when I enter data into “search” button, using selenium how can I write a logic to check whether data is displaying or not 如何检查 jsp 页面中是否存在变量以在索引中发布? - How do I check if variable exists in a jsp page for post in index? 如何检查Vector是否存在并且已经在Java中定义? - How do I check whether a Vector exists and has already been defined in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM