如何使用Selenium WebDriver选择新的IFrame? [英] How to select new IFrame using Selenium WebDriver?

查看:188
本文介绍了如何使用Selenium WebDriver选择新的IFrame?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Body中选择 Iframe 并输入值。我正在尝试使用以下代码。

I wanted to select an Iframe and Enter values in the Body . I am trying with the below code.

HTML代码:

 <iFrame id="4564654_content_ifr">
     <html>
       <head>
         <body id="tiny">
             <div aria-lable="New Compose body">
                 <br>
             </div>
         </body>
       </head>
      </html>
  </iFrame>

Selenium代码:

    driver.switchTo().frame(driver.findElement(By.xpath("//iframe[contains(@id,'content_ifr')]")));
driver.findElement(By.xpath("//*[@id='tiny']/div[1]")).sendKeys("Happy New IFrame");

但我无法输入值。

任何人都可以帮我这个吗?

Can anybody help me with this?

推荐答案

而不是发送密钥,您可以直接设置 innerHTML

Instead of sending keys, you can set innerHTML directly.

driver.switchTo().frame(driver.findElement(By.xpath("//iframe[contains(@id,'content_ifr')]")));

WebElement body = driver.findElement(By.cssSelector("body"));
(JavascriptExecutor)driver.executeScript("arguments[0].innerHTML = 'Happy New IFrame'", body);

如果您正在测试某些WYSIWYG编辑器,如TinyMCE,请随时查看这篇文章:

If you are testing some kind of WYSIWYG editors like TinyMCE, feel free to have a look at this article:

使用Selenium WebDriver测试WYSIWYG编辑器

然后您可以直接通过编辑器API设置内容。众所周知,Firefox中存在 sendKeys 的问题,但Chrome或PhantomJS应该没问题。

Then you might be able to set content through editor's API directly. It's known to have issues with sendKeys in Firefox, but should be fine with Chrome or PhantomJS.

这篇关于如何使用Selenium WebDriver选择新的IFrame?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆