简体   繁体   English

如何使用WebDriver在iframe中设置代码的innerHTML

[英]How to set innerHTML of tag inside iframe using webdriver

<iframe>
    <html>
        <head>
        </html>
        <body>
            <blockquote>
                <p>
                "set text here"
                </p>
            </blockquote>
        </body>
    </html>
</iframe

Hi I am trying to set innerHTML of a tag inside iframe and i can't seem to get it to work 嗨,我正在尝试在iframe中设置代码的innerHTML,但似乎无法正常工作

textareaID.setHTML("Some Text");

By using runScript it writes into the textarea but it doesn't seem to write it inside the blockquote tag 通过使用runScript,它可以写入到textarea中,但似乎没有将它写入到blockquote标记中

selectFrame(iframeLocator);

sendKeys("//html/body/blockquote/p", "Some Text");

selectFrame selects the frame and sendKeys sends the text I want but it also doesn't write the text inside the p tag. selectFrame选择框架,并且sendKeys发送我想要的文本,但它也没有在p标签内写入文本。 It works fine when I run this to //html/body but it doesn't seem to work for //html/body/blockquote/p 当我将其运行到// html / body时,它工作正常,但似乎不适用于// html / body / blockquote / p

Modify your <p> 修改您的<p>

<p id="text">
 "set text here"
</p>

Place in <head> 放在<head>

<script>
document.getElementById("text").innerHTML="some new text"
</script>

I would take a look at the FAQ here : 我将在这里查看常见问题解答:

From the selenium wiki: 从硒维基:

A: Assuming that the iframe is named "foo": 答:假设iframe命名为“ foo”:

driver.switchTo().frame("foo");
WebElement editable = driver.switchTo().activeElement();
editable.sendKeys("Your text here");

Sometimes this doesn't work, and this is because the iframe doesn't have any content. 有时这不起作用,这是因为iframe没有任何内容。 On Firefox you can execute the following before "sendKeys": 在Firefox上,您可以在“ sendKeys”之前执行以下命令:

((JavascriptExecutor) driver).executeScript("document.body.innerHTML = '<br>'");

This is needed because the iframe has no content by default: there's nothing to send keyboard input to. 这是必需的,因为默认情况下iframe不包含任何内容:没有东西可以发送键盘输入。 This method call inserts an empty tag, which sets everything up nicely. 此方法调用将插入一个空标签,可以很好地进行设置。 Remember to switch out of the frame once you're done (as all further interactions will be with this specific frame): 请记住,一旦完成,请跳出框架(因为所有进一步的交互都将与该特定框架有关):

driver.switchTo().defaultContent(); driver.switchTo()。defaultContent();

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM