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

Need Help in getting XPath expression for span inside an Iframe popup

本文关键字:表达式 XPath 帮助 span Iframe 窗口 获取      更新时间:2023-09-26

需要在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>

`

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

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

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

使用

//iframe//span

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

//span [ancestor::iframe]

这将选择在某个点上具有祖先iframe元素的所有span元素。

我们不能直接访问iframe中的元素。我们需要先切换到iframe。

我正在使用PHP Facebook网络驱动程序,这对我来说非常有效:

$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作为:

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