在Iframe弹出窗口中获取span的XPath表达式时需要帮助

时间:2011-10-27 23:43:41

标签: javascript iframe xpath

需要获取iframe内的span的xpath表达式。 在Iframe弹出窗口中获取span的XPath表达式时需要帮助。

我可以获得iframe但是在iframe中获取/ html / body没有发生任何帮助将不胜感激

<div class="gwt-PopupPanelGlass" style="position: absolute; left: 0px; top: 0px; visibility: visible; display: block; width: 1680px; height: 222px;"></div>
<div class="gwt-PopupPanel" style="left: 439px; top: 0px; visibility: visible; position: absolute; overflow: visible;">
<div class="popupContent">
<div class="ph-PopupDialog" style="position: relative; width: 800px; height: 220px;">
<div style="position: absolute; z-index: -32767; top: -20ex; width: 10em; height: 10ex;">&nbsp;</div>
<div style="position: absolute; overflow: hidden; left: 0px; top: 0px; right: 0px; bottom: 0px;">
<div style="position: absolute; left: 0px; top: 0px; right: 0px; bottom: 0px;">
<div style="position: absolute; z-index: -32767; top: -20ex; width: 10em; height: 10ex;">&nbsp;</div>
<div style="position: absolute; overflow: hidden; left: 0px; top: 0px; right: 0px; height: 32px;">
<div style="position: absolute; overflow: hidden; left: 0px; top: 32px; right: 0px; bottom: 0px;">
<div class="ph-PopupDialog-content" style="position: absolute; left: 0px; top: 0px; right: 0px; bottom: 0px;">
<div style="position: absolute; z-index: -32767; top: -20ex; width: 10em; height: 10ex;">&nbsp;</div>
<div style="position: absolute; overflow: hidden; left: 5px; top: 5px; right: 5px; bottom: 5px;">
<div style="position: absolute; left: 0px; top: 0px; right: 0px; bottom: 0px;">
<div>
<iframe class="gwt-Frame ph-PaymentFrame" name="paymentFrame" src="javascript:''">
<html>
<head>
<body onload="pageLoaded();">
<div class="ph-View">
<div class="container_24 form-grid">
<div class="container_24 form-grid">
<div class="container_24 form-grid">
<div class="container_24 form-grid">
<div class="container_24 form-grid">
<div class="grid_12">
<div class="ph-ButtonPanel ph-ButtonPanel-undecorated">
<a class="ph-Button ph-Button-button" onclick="submit();return false;" style="float: right; margin-left: 5px;" href="#">
<a class="ph-Button ph-Button-button" onclick="cancel();return false;" style="float: right; margin-left: 5px;" href="#">
<span>Cancel</span>
</a>
</div>
</div>
</div>
</div>
</body>

`

5 个答案:

答案 0 :(得分:8)

iframe中的元素实际上是在另一个页面中。所以你应首先找到该页面的地址,该地址是iframe的src值的值,然后加载它,然后访问该页面中的元素。

答案 1 :(得分:5)

您需要将xpath的范围设置为iframe的contentDocument属性:

var iframe = document.getElementsByTagName("iframe")[0];
var theFirstSpan = document.evaluate('//span', iframe.contentDocument,
         null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue

答案 2 :(得分:1)

使用

//iframe//span

这将选择XML文档中任何span元素后代的每个iframe元素。

答案 3 :(得分:0)

//span [ancestor::iframe]

这将选择在某些时候具有祖先iframe元素的所有span元素。

答案 4 :(得分:-1)

我们无法直接访问iframe内的元素。我们首先需要转向iframe。

我正在使用PHP Facebook webdriver,这对我来说非常有用。

$driver->get("http://www.example.com"); // containing an iframe
$iFrame = $driver->findElement(
            WebDriverBy::xpath('//iframe[@name="iFrameName"]') //name or id or whatever by which we can access.
        );
$driver->switchTo()->frame($iFrame);

现在,我们可以访问span as,

$spanButton = $driver->findElement(
            WebDriverBy::xpath('//span') 
        );

我希望这可以帮助别人!