简体   繁体   English

如何使用 java 在 Selenium WebDriver 中处理 iframe

[英]How to handle iframe in Selenium WebDriver using java

<div>    
  <iframe id="cq-cf-frame ">    
    <iframe id="gen367">   
      <body spellcheck="false" id="CQrte" style="height: 255px; font-size: 12px; font-family:tahoma,arial,helvetica,sans-serif; background-image: url(&quot;/libs/cq/ui/widgets/themes/default/ext/form/text-bg.gif&quot;); background-repeat: repeat-x; background-attachment: fixed;">
        <p>4t43t4<br></p>
      </body >
    </iframe>
  </iframe>    
</div> 

In this scenario there is an iframe under iframe .在这种情况下有一个iframeiframe And I have to select the outer iframe to go to inner iframe and write in the body which is in the inner iframe .我不得不选择外iframe去内iframe ,并写在身体这是在内部iframe

Next, I have to come out from the inner iframe to outer iframe and click on OK button, (which is in the outer iframe ).接下来,我必须从内部iframe到外部iframe并单击 OK 按钮(在外部iframe )。

Following is my code以下是我的代码

/*Line 1 */ driver.switchTo().frame("cq-cf-frame");
/*     2 */ driver.findElement(By.css("#extdd-9 > div.tblRow >  input.edititem").click();
/*     3 */ driver.switchTo().Frame("cq-gen379");
/*     4 */ driver.findElement(By.id("CQrte").sendKeys("Tnx");  
/*     5 */ selenium.selectFrame("relative=up");       
/*     6 */ driver.findElement(By.xpath("//button[text()='OK']")).click(); 

Following is my problem:以下是我的问题:

My test code is working fine up to line number 4 ie writing into the body, but I want to come out from inner to outer iframe it says that the element //button[text()='OK'] not found.我的测试代码在第 4 行之前工作正常,即写入正文,但我想从内部到外部iframe未找到元素//button[text()='OK']

I tried with using index, parent, relative, but had no luck.我尝试使用索引、父母、亲戚,但没有运气。

NOTE: If I don't select the inner frame ( cq-gen379 ).注意:如果我不选择内部框架 ( cq-gen379 )。 I'm able to click on the OK button.我可以单击“确定”按钮。

In Webdriver, you should use driver.switchTo().defaultContent();在 Webdriver 中,你应该使用driver.switchTo().defaultContent(); to get out of a frame.走出框架。 You need to get out of all the frames first, then switch into outer frame again.您需要先退出所有框架,然后再次切换到外框架。

// between step 4 and step 5
// remove selenium.selectFrame("relative=up");
driver.switchTo().defaultContent(); // you are now outside both frames
driver.switchTo().frame("cq-cf-frame");
// now continue step 6
driver.findElement(By.xpath("//button[text()='OK']")).click(); 

You have to get back out of the Iframe with the following code:您必须使用以下代码退出 Iframe:

driver.switchTo().frame(driver.findElement(By.id("frameId")));
//do your stuff
driver.switchTo().defaultContent();

You need to first find iframe .您需要先找到iframe You can do so using following statement.您可以使用以下语句执行此操作。

WebElement iFrame= driver.findElement(By.tagName("iframe"));

Then, you can swith to it using switchTo method on you WebDriver object.然后,您可以使用WebDriver对象上的switchTo方法切换到它。

driver.switchTo().frame(iFrame);

And to move back to the parent frame, you can either use switchTo().parentFrame() or if you want to get back to the main (or most parent) frame, you can use switchTo().defaultContent();要移回父框架,您可以使用switchTo().parentFrame()或者如果您想返回主(或大多数父)框架,您可以使用switchTo().defaultContent(); . .

driver.switchTo().parentFrame();    // to move back to parent frame
driver.switchTo().defaultContent(); // to move back to most parent or main frame

Hope it helps.希望能帮助到你。

To get back to the parent frame, use:要返回父框架,请使用:

driver.switchTo().parentFrame();

To get back to the first/main frame, use:要返回第一个/主框架,请使用:

driver.switchTo().defaultContent();
WebDriver driver=new FirefoxDriver();
driver.get("http://www.java-examples.com/java-string-examples");
Thread.sleep(3000);
//Switch to nested frame
driver.switchTo().frame("aswift_2").switchTo().frame("google_ads_frame3");

Below approach of frame handling : When no id or name is given incase of nested frame下面的框架处理方法:当嵌套框架没有给出 id 或 name 时

WebElement element =driver.findElement(By.xpath(".//*[@id='block-block19']//iframe"));
driver.switchTo().frame(element);
driver.findElement(By.xpath(".//[@id='carousel']/li/div/div[3]/a")).click();

Selenium Web Driver Handling Frames Selenium Web 驱动程序处理框架
It is impossible to click iframe directly through XPath since it is an iframe.这是不可能的点击iframe中直接通过XPath的,因为它是一个iframe。 First we have to switch to the frame and then we can click using xpath.首先,我们必须切换到框架,然后我们可以使用 xpath 单击。

driver.switchTo().frame() has multiple overloads. driver.switchTo().frame()有多个重载。

  1. driver.switchTo().frame(name_or_id)
    Here your iframe doesn't have id or name, so not for you.这里你的iframe没有 id 或 name,所以不适合你。

  2. driver.switchTo().frame(index)
    This is the last option to choose, because using index is not stable enough as you could imagine.这是最后一个选择,因为使用索引并没有你想象的那么稳定。 If this is your only iframe in the page, try driver.switchTo().frame(0)如果这是页面中唯一的 iframe,请尝试driver.switchTo().frame(0)

  3. driver.switchTo().frame(iframe_element)
    The most common one.最常见的一种。 You locate your iframe like other elements, then pass it into the method.您可以像其他元素一样定位 iframe,然后将其传递给方法。

driver.switchTo(). defaultContent (); [parentFrame, defaultContent, frame] 默认内容(); [parentFrame, defaultContent, frame] (); [parentFrame, defaultContent, frame]

// Based on index position:
int frameIndex = 0;
List<WebElement> listFrames = driver.findElements(By.tagName("iframe"));
System.out.println("list frames   "+listFrames.size());
driver.switchTo().frame(listFrames.get( frameIndex ));

// XPath|CssPath Element:
WebElement frameCSSPath = driver.findElement(By.cssSelector("iframe[title='Fill Quote']"));
WebElement frameXPath = driver.findElement(By.xpath(".//iframe[1]"));
WebElement frameTag = driver.findElement(By.tagName("iframe"));

driver.switchTo().frame( frameCSSPath ); // frameXPath, frameTag


driver.switchTo().frame("relative=up"); // focus to parent frame.
driver.switchTo().defaultContent(); // move to the most parent or main frame

// For alert's
Alert alert = driver.switchTo().alert(); // Switch to alert pop-up
alert.accept();
alert.dismiss();

XML Test: XML 测试:

<html>
    <IFame id='1'>...       parentFrame() « context remains unchanged. <IFame1>
    |
     -> <IFrame id='2'>...  parentFrame() « Change focus to the parent context. <IFame1>
</html>

</html>
<frameset cols="50%,50%">
    <Fame id='11'>...     defaultContent() « driver focus to top window/first frame. <html>
    |
     -> <Frame id='22'>... defaultContent() « driver focus to top window/first frame. <Fame11> 
                           frame("relative=up") « focus to parent frame. <Fame11>
</frameset>
</html>

Conversion of RC to Web-Driver Java commands.将 RC 转换为 Web-Driver Java 命令。 link . 链接


<frame> is an HTML element which defines a particular area in which another HTML document can be displayed. <frame>是一个 HTML 元素,它定义了可以显示另一个 HTML 文档的特定区域。 A frame should be used within a <frameset> .应该在<frameset>使用<frameset> « Deprecated . « 已弃用 Not for use in new websites.不适用于新网站。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Selenium WebDriver中使用Java处理灯箱 - How to handle lightbox using java in selenium webdriver 如何使用 selenium webdriver 处理动态更改 iframe id - how to handle dynamically changing iframe id using selenium webdriver 如何使用带有Java的Selenium WebDriver在iframe中拖动对象 - How do I drag an object in an iframe using Selenium WebDriver with Java 如何处理linkedIn授权使用Java在Selenium Webdriver中弹出? - How to Handle linkedIn authorization Pop up in selenium webdriver using Java? 如何使用带有Java的Selenium WebDriver处理日历弹出窗口? - How to handle calendar popup using Selenium WebDriver with Java? 如何使用Java处理Selenium WebDriver中的弹出窗口 - How to handle Pop-up in Selenium WebDriver using Java 如何使用Java处理Selenium WebDriver中的新窗口? - How to handle the new window in Selenium WebDriver using Java? 如何使用 Java 在 Selenium Webdriver 中处理 HTTP Basic Auth 标头? - How to Handle HTTP Basic Auth headers in Selenium Webdriver using Java ? 如何使用 Java 处理 Selenium WebDriver 的身份验证弹出窗口 - How to handle authentication popup with Selenium WebDriver using Java 如何使用Java在Selenium Webdriver中处理促销广告或cookie - How to handle promotional ads or cookies in Selenium webdriver using Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM