【问题标题】:Unable to handle iframe in selenium with tinyMCE Editor无法使用 tinyMCE 编辑器处理 selenium 中的 iframe
【发布时间】:2015-06-26 08:51:27
【问题描述】:

我正在尝试访问 iFrame

    <iframe id="frame1" style="width: 100%; height: 100%; display: block;" tabindex="100">
    <html>
    <head xmlns="http://www.w3.org/1999/xhtml">
    <body id="tiny1" contenteditable="true" onload="window.parent.get('frame1').onLoad.dispatch();">
    <p/>
    </body>
    </html>
   </iframe>

我想用 id=tinymce 将 sendKeys() 发送到正文。但是当我尝试 switchTo().frame 时,它​​不起作用。

我的 Java 代码:

public void enterArea(String object, String content){
        String driverWindows = driver.toString();
        driver.switchTo().frame(selenium.driver.findElement(By.xpath("//*[@id='frame1']")));         
        String driverIFrame = driver.toString();
        WebElement contentTextArea = (new WebDriverWait(driver, 3))
                .until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='tiny1']/p")));
        contentTextArea.sendKeys(content);
        driver.switchTo().defaultContent();
    }

driverWindowsdriverIFrame 2 个总是返回相同的字符串,这意味着web driver 没有切换,对吧?

您能帮我访问元素吗? 如果需要任何进一步的细节,请告诉我。 谢谢。

【问题讨论】:

  • 这里 body 和 frame 是分开的,如果你想向 body 发送文本,为什么要切换到 frame 呢?
  • @HelpingHands:对不起,我的错字。我已经编辑了。
  • 您是否尝试通过 ID 来识别 iframe?
  • @HelpingHands:是的,我试过了,driver.switchTo().frame("frame1");,但还是一样。
  • 它应该可以工作...你调试代码了吗?

标签: javascript iframe selenium-webdriver automation


【解决方案1】:
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@id='frame1']")));

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

driver.findElement(By.tagName("body")).sendKeys("testing");

driver.switchTo().defaultContent();

另一个使用 javascript 的选项

如果您使用的是 javascript,请不要切换到 iframe

((JavascriptExecutor)driver).executeScript("tinyMCE.activeEditor.setContent('testing');");

我用 timymce 编辑器测试了上面的代码,它工作正常

编辑

        driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@id='frame1']")));

 //Do not use path body/p you need to send text to body tag

 (new WebDriverWait(driver, 10))
                .until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//body[@id='tiny1']")));

 driver.findElement(By.xpath("//body[@id='tiny1']")).sendKeys("testing");

 driver.switchTo().defaultContent();

希望这对您有所帮助..如果您有任何疑问,请回复

【讨论】:

  • 对我非常非常有帮助。效果很好。非常感谢。但是你能告诉我为什么我的代码不起作用吗?为什么我们必须使用implicitlyWait 而不是ExpectedConditions。 ?为什么我们应该使用 tagName body ?只是想了解更多以拥有知识。再次感谢。
  • 乐于助人:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-18
  • 2011-05-20
  • 2016-06-18
  • 1970-01-01
  • 2012-08-19
相关资源
最近更新 更多