带有iframe的driver.findElement()和没有ID的元素

时间:2012-11-30 02:42:34

标签: java xpath selenium webdriver

Java代码:

driver.switchTo().frame(0);
                WebElement elemText = driver.findElement(By.xpath("/html/body[contains(@class='forum')]"));
                //WebElement elemText = driver.findElement(By.xpath("//td[@id='cke_contents_vB_Editor_001_editor']/textarea"));
                elemText.sendKeys(message);
                elemText.submit();
                forumLink =  driver.getCurrentUrl();

HTML代码:

<td id="cke_contents_vB_Editor_001_editor" class="cke_contents" style="height:1726px" role="presentation">
<iframe style="width:100%;height:100%" frameborder="0" title="Rich text editor, vB_Editor_001_editor, press ALT 0 for help." src="" tabindex="-1" allowtransparency="true">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html dir="ltr" lang="en" contenteditable="true">
<head>
<title data-cke-title="Rich text editor, vB_Editor_001_editor, press ALT 0 for help.">Rich text editor, vB_Editor_001_editor, press ALT 0 for help.</title>
<base href="http://fairplay.garena.com/" data-cke-temp="1">
<link type="text/css" rel="stylesheet" href="http://fairplay.garena.com/clientscript/vbulletin_css/style00008l/editor_contents.css">

<style type="text/css" data-cke-temp="1">
form{border: 1px dotted #FF0000;padding: 2px;}

img.cke_hidden{background-image: url(http://fairplay.garena.com/clientscript/ckeditor/plugins/forms/images/hiddenfield.gif?t=B37D54V);background-position: center center;background-repeat: no-repeat;border: 1px solid #a9a9a9;width: 16px !important;height: 16px !important;}
img.cke_iframe{background-image: url(http://fairplay.garena.com/clientscript/ckeditor/plugins/iframe/images/placeholder.png?t=B37D54V);background-position: center center;background-repeat: no-repeat;border: 1px solid #a9a9a9;width: 80px;height: 80px;}
img.cke_anchor{background-image: url(http://fairplay.garena.com/clientscript/ckeplugins/vblink/images/anchor.gif?t=B37D54V);background-position: center center;background-repeat: no-repeat;border: 1px solid #a9a9a9;width: 18px !important;height: 18px !important;}
a.cke_anchor{background-image: url(http://fairplay.garena.com/clientscript/ckeplugins/vblink/images/anchor.gif?t=B37D54V);background-position: left center;background-repeat: no-repeat;border: 1px solid #a9a9a9;padding-left: 18px;}
</style>
</head>
<body class="forum" spellcheck="true">
</body>
</html>
</iframe>
</td>

图片:http://s9.postimage.org/nwyvq3san/Screen_Shot038.jpg

我找不到在iframe中获取元素而没有id的方法。

你能帮助我吗?

1 个答案:

答案 0 :(得分:1)

driver.switchTo().frame(driver.findElementByXpath("//iframe"));
WebElement elemText = driver.findElement(By.xpath("//body[@class='forum')]"));

应该有用。