无法使用 tinyMCE 编辑器处理 selenium 中的 iframe

Unable to handle iframe in selenium with tinyMCE Editor

我正在尝试访问 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>

我想将 sendKeys() 发送到 id=tinymce 的正文。但是当我尝试 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();
    }

2个driverWindowsdriverIFrame总是return一样的字符串,那就说明web driver没有切换吧?

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

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();

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