2012-11-30 47 views
1

Java代碼:driver.findElement()iframe和元素,而ID

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
driver.switchTo().frame(driver.findElementByXpath("//iframe")); 
WebElement elemText = driver.findElement(By.xpath("//body[@class='forum')]")); 

應該工作。

+0

org.openqa.selenium.NoSuchFrameException:無法找到frame:// iframe 命令持續時間或超時:15毫秒 構建信息:版本:'2.24.1',修訂版本:'17205',時間:'2012- 06-19 15:28:49' 系統信息:os.name:'Windows 2003',os.arch:'x86',os.version:'5.2',java.ve rsion:'1.7.0_09' 驅動程序信息:driver.version:RemoteWebDriver – user1864657

+0

請參閱已編輯的答案.. –