我正在尝试使用 Selenium 在页面上查找元素。以下是一些示例内容:
<body id="tinymce" class="mceContentBody " contenteditable="true" dir="ltr" style="overflow: auto;">
这是我尝试选择它的方法:
driver.findElement(By.cssSelector("body#tinymce")).sendKeys("Hello, everyone!! Don't worry it is a test letter to check connection!!");
但是我没有得到返回的元素。
最佳答案
看起来您正在针对 TinyMCE editor 进行测试.
问题是:
- 它位于 iframe 中,您需要先切换到 iframe。
- 您需要将 key 发送至
<body>
该 iframe 内的元素(不是<input>
)
这是要做的事情:
// switch to iframe, use locator of your choice, "#editMe_ifr" here as an example
WebElement editorFrame = driver.findElement(By.cssSelector("#editMe_ifr"));
driver.switchTo().frame(editorFrame);
WebElement body = driver.findElement(By.TagName("body")); // then you find the body
body.sendKeys(Keys.CONTROL + "a"); // send 'ctrl+a' to select all
body.SendKeys("Some text");
进一步阅读:
关于java - 如何在 Selenium 中使用 CSS 选择器找到 TinyMCE 编辑器的主体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18432390/