selenium webdriver如何在iframe wysiwyg文本区域输入文本

时间:2015-03-04 02:22:09

标签: java selenium iframe selenium-webdriver wysiwyg

如何在iframe wysiwyg文本区输入文本

// Enter text for Message field
ContactUs_Page.txt_keyInMessage().sendKeys(ColMessage);

ContactUs_Page.java

public static WebElement txt_keyInMessage() throws Exception{
    try{     
        WebElement iframeMsg= driver.findElement(By.xpath("//*[contains(@class, 'wysiwyg_frame')]"));        
        driver.switchTo().frame(iframeMsg);     
        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);         
        element = driver.findElement(By.cssSelector("body"));    
    }catch (Exception e){
        throw(e);
    }
    return element;
}

iframe html代码

<iframe class="wysiwyg_frame" frameborder="0" src="" style="width: 100%; height: 100%;" title="Rich Text Editor, contact_remarks" aria-describedby="cke_30" tabindex="0" allowtransparency="true">
    <!DOCTYPE html>
    <html lang="en" dir="ltr">
        <head>
        <body class="cke_editable cke_editable_themed cke_contents_ltr cke_show_borders" contenteditable="true" spellcheck="false">
            <p>
                hello, pls contact me once you received this message
                <br>
            </p>
        </body>
        </html>
</iframe>

例如。这里输入的文字是

“你好,请在收到此消息后与我联系”

如果在普通文本框中,则可以使用getAttribute(“value”)

但在iframe所见即所得文本区域中,它没有value类型

请建议,谢谢。

1 个答案:

答案 0 :(得分:3)

看起来它只是textbody元素的iframe

WebElement iframeMsg = driver.findElement(By.xpath("//*[contains(@class, 'wysiwyg_frame')]"));        
driver.switchTo().frame(iframeMsg);

WebElement body = driver.findElement(By.cssSelector("body"));
System.out.println(body.getText());