无法从HTML代码中检测webelement

时间:2014-11-12 11:22:33

标签: iframe selenium

我遇到了一个我无法从源代码中检测到Web元素的情况。 在我的代码中,您可以看到一个元素

<a class="CSPortalGuiListItemDrag" ondragstart="CSPortalGuiList.onDragStart(event, '', '', '');" onclick="return false;" href="#"> By Suite </a>

我试图检测。

在那之前我试过的,简直告诉你 对于定位元素,我使用了Xpath,className和CSS。我尝试了绝对路径和相对路径,但没有一个工作。

尝试A. 1.切换到默认内容。 2.切换到i-frame。 3.切换到i-frame 3.找到元素。

尝试B 1.切换到默认内容。 2.切换到i-frame 3.find元素。

尝试C 1.切换到i-frame。 2.找到元素。

所有尝试均未成功,无法检测到网络元素。

请帮助我提出宝贵的建议。 以下是源代码

<html>
<head>
<body>
<iframe scrolling="auto" frameborder="no" src="../admin?redirect=true&" marginwidth="0" marginheight="0">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="de-DE" dir="ltr" xml:lang="de-DE" xmlns="http://www.w3.org/1999/xhtml">
<head>
<body class="CSPortal Win ff17 ff SingleWidget SingleTab editmode" ffdragdropid="dd_1">
<div id="CSPortalPortalTitle" onclick="CS.reloadTopFrame();">Rupesh</div>
<div id="CSPortalPortalLogo" onclick="CS.reloadTopFrame();"></div>
<div id="header">
<div id="main" class="tabs1 t1 st0">
<div id="footer">
<div id="CSPortalWindow" class="CSPortalWindow" name="CSPortalWindow" style="width: 1000px; height: 568px; left: 20px; top: 20px; z-index: 10003;">
<div class="CSPortalWindowToolbar">
<div style="background-color: white; position: relative; overflow: auto; width: 1000px; font-size: 0px; height: 526px;">
<iframe frameborder="0" style="background-color: white; border: medium none; width: 100%; height: 100%; margin: 0px; padding: 0px;" src="../admin/portal.php?forward=core/extensions/portal/gui/framework/CSPortalGuiWidgetSelector.php&mode=add&PortalTabID=131&col=1">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html style="height:100%">
<head id="CSGuiWindowHead">
<body id="CSGuiWindowBody" class="Win ff17 ff hasHeight" oncontextmenu=";" style="background-color: #FFFFFF; height:100%;width: 100%;">
<div id="CSPortalLayoutManager_4367034" class="CSPortalGuiLayout">
<div class="CSPortalGuiLayoutInnerDiv">
<table class="CSPortalGuiLayout">
<tbody>
<tr>
<tr id="CSPortalLayoutManager_4367034_Middle">
<td id="CSPortalLayoutManager_4367034_Center" class="CSPortalGuiLayoutCenter" colspan="3">
<div class="CSPortalGuiLayoutCenterOverflow">
<div id="CSPortalLayoutManager_4367034_CenterContent" class="CSPortalGuiLayoutCenterOverflow2">
<table class="CSPortalGuiLayoutCenterTable" style="border-collapse: collapse; width: 100%; height: 100%;">
<tbody>
<tr>
<td id="Col1" class="CSPortalGuiLayoutCenterCol First Odd" width="200" height="0">
<div id="CSPortalLayoutManager_3849853" class="CSPortalGuiLayout">
<div class="CSPortalGuiLayoutInnerDiv">
<table class="CSPortalGuiLayout">
<tbody>
<tr>
<tr id="CSPortalLayoutManager_3849853_Middle">
<td id="CSPortalLayoutManager_3849853_Center" class="CSPortalGuiLayoutCenter" colspan="3">
<div class="CSPortalGuiLayoutCenterOverflow">
<div id="CSPortalLayoutManager_3849853_CenterContent" class="CSPortalGuiLayoutCenterOverflow2">
<div id="CSPortalGuiList_4885314" class="CSPortalGuiList unmarkable maxHeight list">
<style type="text/css">
<table class="CSPortalGuiListContainer">
<tbody>
<tr>
<td id="CSPortalGuiList_4885314_TdContent" class="CSPortalGuiListTdContent">
<div class="CSPortalGuiListOverflow">
<div id="CSPortalGuiListContentCSPortalGuiList" class="CSPortalGuiListContent">
<table class="CSPortalGuiListTable" name="CSPortalGuiList">
<tbody class="CSPortalGuiListBody">
<tr id="CSPortalGuiListItem_4885314_0" class="CSPortalGuiListItem first hasAction " clickhandler="1" onclick="var event=arguments[0]||window.event;var param=arguments[1];var tr=this;if (CSPortalGuiList && !CSPortalGuiList.clickItem(tr, event)) return false;tr.callback = function(event){ listCONTENTSERVModules(); };tr.callback(event);" style="" title="">
<td class="CSPortalGuiListItemContent">
<div class="CSPortalGuiListItemCaption">
<a class="CSPortalGuiListItemDrag" ondragstart="CSPortalGuiList.onDragStart(event, '', '', '');" onclick="return false;" href="#"> By Suite </a>
</div>
</td>
<td id="CSPortalGuiListActions_4885314_0" class="CSPortalGuiListItemAction">
</tr>
<tr id="CSPortalGuiListItem_4885314_1" class="CSPortalGuiListItem hasAction " clickhandler="1" onclick="var event=arguments[0]||window.event;var param=arguments[1];var tr=this;if (CSPortalGuiList && !CSPortalGuiList.clickItem(tr, event)) return false;tr.callback = function(event){ listCONTENTSERVVendors(); };tr.callback(event);" style="" title="">
<tr id="CSPortalGuiListItem_4885314_2" class="CSPortalGuiListItem hasAction " clickhandler="1" onclick="var event=arguments[0]||window.event;var param=arguments[1];var tr=this;if (CSPortalGuiList && !CSPortalGuiList.clickItem(tr, event)) return false;tr.callback = function(event){ listCONTENTSERVMostPopular(); };tr.callback(event);" style="" title="">
<tr id="CSPortalGuiListItem_4885314_3" class="CSPortalGuiListItem last hasAction " clickhandler="1" onclick="var event=arguments[0]||window.event;var param=arguments[1];var tr=this;if (CSPortalGuiList && !CSPortalGuiList.clickItem(tr, event)) return false;tr.callback = function(event){ listCONTENTSERVPreconfigured(); };tr.callback(event);" style="" title="">
</tbody>
</table>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</td>

1 个答案:

答案 0 :(得分:0)

试试这段代码: -

 //switching to the frame using the frameelement that has your concerned webelement
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[contains(@src,'CSPortalGuiWidgetSelector')]")));    

 //Locating the element inside the iframe after switching to it. 
 WebElement ele = driver.findElement(By.xpath("//a[class='CSPortalGuiListItemDrag']"));

 ele.click(); // in case you want to click the element

 driver.switchTo().defaultContent(); //to revert to the default window.