Selenium WebDriver:无法使用TinyMCE编辑器在iframe中定位元素 [英] Selenium WebDriver: unable to locate element inside iframe using TinyMCE editor

查看:133
本文介绍了Selenium WebDriver:无法使用TinyMCE编辑器在iframe中定位元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许我的问题类似于此 Selenium WebDriver无法在iframe中定位元素,并抛出NoSuchElementException ,但是我仍然找不到所需的元素.
元素本身看起来是:

Maybe my question is similar to this Selenium WebDriver cannot locate element within an iframe, and throws NoSuchElementException however I still can't find required element.
The element itself looks:

body id="tinymce" class="mceContentBody" contenteditable="true" onload="window.parent.tinyMCE.get('compose_295_composeEditor').onLoad.dispatch();" dir="ltr" style="overflow: auto;">

包含该元素的iframe是:

body id="tinymce" class="mceContentBody" contenteditable="true" onload="window.parent.tinyMCE.get('compose_295_composeEditor').onLoad.dispatch();" dir="ltr" style="overflow: auto;">  

我尝试了

driver.switchTo().frame(10);
driver.switchTo().frame(driver.findElement(By.id("tinymce")));
driver.findElement(By.id("tinymce")).clear();
driver.findElement(By.id("tinymce")).sendKeys("Privet!"); // clear mail text body
driver.switchTo().defaultContent();  

但收到错误:

Unable to locate frame: 10 

也尝试过

driver.switchTo().frame(driver.findElement(By.id("tinymce")));  

如此处其他答案所述,但收到NoSuchElement错误.

as described in other answers there but received NoSuchElement error.

元素周围的HTML是:

<div class="b-compose__editor ru_RU">
<div id="compose_295_toolbar_external" class="compose__editor_toolbar defaultSkin">
<div class="js-removeDraftContainer infobar infobar_notice infobar_draft" style="display: none;">
<div id="compose_295_composeFrame" class="b-compose__editor__frame">
<div class="compose__editor__frame_shadow"></div>
<table class="w100" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="vti w100">
<div id="compose_295_composeEditorFrame" class="composeEditorFrame" style="height: 200px;">
<table id="compose_295_shell__text" class="mlruTmpId0" cellspacing="0" cellpadding="0" bgcolor="" background="" style="width: 100%">
<tbody>
<tr class="nojsdn textModeHide">
<tr>
<td class="cell w100">
<table id="compose_295_middleTable" class="w100" cellspacing="0" cellpadding="0">
<tbody>
<tr id="compose_295_middleRow_sht">
<td id="compose_295_shell__text_cell_holder" class="cell shell__text_cell_holder w100" valign="top">
<table class="w100" cellspacing="0" cellpadding="0">
<tbody>
<tr class="nojsdn textModeHide">
<tr>
<td id="compose_295_shell__text_cell" class="cell shell__text_cell w100" valign="top">
<textarea id="compose_295_composeEditor" class="bsbb composeEditor" wrap="physical" tabindex="10" name="Body" style="width: 100%; height: 570px; display: none;" cols="80" rows="15" spellcheck="true" aria-hidden="true"> </textarea>
 <span id="compose_295_composeEditor_parent" class="mceEditor defaultSkin">
 <table id="compose_295_composeEditor_tbl" class="mceLayout" cellspacing="0" cellpadding="0" style="width: 100%; height: 570px;">
<tbody>
<tr class="mceFirst mceLast">
<td class="mceIframeContainer mceFirst mceLast">
<iframe id="compose_295_composeEditor_ifr" frameborder="0" src="javascript:""" allowtransparency="true" title="{#aria.rich_text_area}" style="width: 100%; height: 200px; display: block;" hidefocus="true" tabindex="10" scrolling="auto">
<html>
<head xmlns="http://www.w3.org/1999/xhtml">
<body id="tinymce" class="mceContentBody" contenteditable="true" onload="window.parent.tinyMCE.get('compose_295_composeEditor').onLoad.dispatch();" dir="ltr" style="overflow: auto;">
</html>
</iframe>
</td>
</tr>
</tbody>
</table>
</span>
</td>
</tr>
<tr class="nojsdn textModeHide">
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>

UPD Selenium和wordpress:新的发布测试,olyv的回答对我有帮助,但:
1)将这个问题与我的问题联系起来非常困难,因为它的标题中没有提到TinyMCE编辑器,而且问题实际上与wordpress无关.
2)我编辑了问题名称以指示问题源-TinyMCE编辑器.
3)赛富尔(Saifur)的回答在这里完美回答了我的问题.
所以我认为我的问题不应该被解决

UPD Selenium and wordpress: New post test , olyv's answer there helped me but:
1) It was very hard to associate that question to mine since it doesn't mention TinyMCE editor in the title and the problem isn't really with wordpress.
2) I edited my question name to indicate the problem source - TinyMCE editor.
3) Saifur's answer here answers my question perfectly.
So I believe my question shouldn't be closed

推荐答案

根据html,用于选择iframe的选择器不正确.我正在使用cssSelector,它将使您可以识别部分ID匹配的iframe.你为什么不试试这个?

According to the html the selector to identify the iframe is incorrect. I am using a cssSelector that will allow you to identify the iframe with partial id match. Why do not you try this?

driver.switchTo().frame(driver.findElement(By.cssSelector("iframe[id$='_composeEditor_ifr']")));
d̶r̶i̶v̶e̶r̶.̶s̶w̶i̶t̶c̶h̶T̶o̶(̶)̶.̶f̶r̶a̶m̶e̶(̶d̶r̶i̶v̶e̶r̶.̶f̶i̶n̶d̶E̶l̶e̶m̶e̶n̶t̶(̶B̶y̶.̶i̶d̶(̶"̶t̶i̶n̶y̶m̶c̶e̶"̶)̶)̶)̶;̶
driver.findElement(By.id("tinymce")).clear();
driver.findElement(By.id("tinymce")).sendKeys("Privet!"); // clear mail text body
driver.switchTo().defaultContent();  

这篇关于Selenium WebDriver:无法使用TinyMCE编辑器在iframe中定位元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆