切换到iframe
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@id='ContentFrame']")));
我尝试过这两种方法,但没有一种方法可以使用。
操作
WebElement webElement = driver.findElement(locator);
action().moveToElement(webElement);
action().click();
action().sendKeys("Hello world");
action().build().perform();
JS Executor
WebElement webElement = driver.findElement(locator);
String script = "arguments[0].setAttribute('value', 'Hello world');";
((JavascriptExecutor) driver).executeScript(script, webElement);
HTML:
<iframe id="ContentFrame" width="100%" height="500" frameborder="0" allowfullscreen="" src="/content/geometrixx/assdasasda.html" style="height: 603px;">
<!DOCTYPE html>
<html class=" aem-AuthorLayer-Edit" style="overflow: hidden;">
<head>
<body class="page homepage " spellcheck="false">
<div class="parbase clientcontext">
<div id="main_bg" class="bg">
<div class="header">
<div id="main">
<div class="container_16">
<div class="grid_16">
<div class="grid_12 body_container">
<div class="lead">
<div class="par parsys">
<div class="section text parbase is-edited gecko" contenteditable="true" style="outline-style: none;">
<p>Hello World</p>
答案 0 :(得分:0)
要在框架内写字,您需要点击framebody
然后您可以写任何您想要的内容。
<强>方法1 强>
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@id='ContentFrame']")));
WebElement framebody = driver.findElement(By.tagName("body"));
new Actions(driver).click(framebody).sendKeys("Hello World!").build().perform();
<强>方法2 强>
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@id='ContentFrame']")));
WebElement framebody = driver.findElement(By.tagName("body"));
framebody.sendKeys("HelloWorld!");
希望它能够奏效。