Java 如何在Selenium中使用CSS选择器找到TinyMCE编辑器的主体?
我正在尝试使用Selenium在页面上查找元素。以下是一些示例内容:Java 如何在Selenium中使用CSS选择器找到TinyMCE编辑器的主体?,java,selenium,tinymce,webdriver,selenium-webdriver,Java,Selenium,Tinymce,Webdriver,Selenium Webdriver,我正在尝试使用Selenium在页面上查找元素。以下是一些示例内容: <body id="tinymce" class="mceContentBody " contenteditable="true" dir="ltr" style="overflow: auto;"> 但是,我没有得到返回的元素。您可以将HTML更改为: <body> <input id="tinymce" type="text"/> </body>
<body id="tinymce" class="mceContentBody " contenteditable="true" dir="ltr" style="overflow: auto;">
但是,我没有得到返回的元素。您可以将HTML更改为:
<body>
<input id="tinymce" type="text"/>
</body>
您可以将选择器从
body#tinymce
更改为#tinymce
。使用id时不需要指定标记名,因为id无论如何都应该是唯一的。看起来您正在测试它
这些问题是:
- 它位于iframe中,您需要首先切换到iframe
- 您需要将密钥发送到iframe中的
元素(而不是
)
// 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");
进一步阅读:
元素发送密钥?您不应该在文本字段中键入内容吗?是的,您是对的,我应该在文本字段中键入内容不应该body#tinymce
是指id为tinymce
的
元素吗?注意,两者之间没有空格。
// 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");