HTML代码:
<div class="listRte__editorFrame">
<iframe src="about:blank" style="height: 150px;">
#document
<html>
<head>
<body>
<button class="__clipped">
<div data-ghosttext="Add a detailed description here." contenteditable="true">
::after
</div>
</button>
</body>
</head>
</html>
</iframe>
</div>
我想在 div 中输入文本。 手动添加文本后的 HTML:
<div contenteditable="true">abcd</div>
我尝试使用 javascript 输入文本:
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("document.evaluate('//div[@data-ghosttext=\"Add a detailed description here.\"]', document, null, 9, null).singleNodeValue.innerHTML="+ "\"abc\"");
但它给我错误:
org.openqa.selenium.JavascriptException: TypeError: document.evaluate(...).singleNodeValue is null
我也试过切换到 iframe,但切换到 iframe 时无法识别 div。此外,当我尝试执行以下命令时,我得到 size=0
。但是,当我检查元素时,页面上有 3 个 iframe。
int size = driver.findElements(By.tagName("iframe")).size();
请问我可以在 Java/js 中获取准确的命令吗?我可以使用它在 div 元素中键入内容。 我使用 testng 框架处理 selenium
最佳答案
您必须先切换到适当的frame
,然后尝试在div
d> 中传递任何文本,如下所示:
driver.switchTo().frame(driver.findElement(By.xpath("//div[@class='listRte__editorFrame']//following::iframe[1]")));
关于javascript - Selenium : How to enter text in div inside an iframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47150907/