javascript 在 Iframe 弹出窗口中获取跨度的 XPath 表达式需要帮助
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7923718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Need Help in getting XPath expression for span inside an Iframe popup
提问by Maalamaal
Need to get an xpath expression for the span inside the iframe. Need Help in getting XPath expression for span inside an Iframe popup.
需要获取 iframe 内跨度的 xpath 表达式。在 Iframe 弹出窗口中获取跨度的 XPath 表达式需要帮助。
i can get the iframe but getting /html/body inside iframe is not happening any help would be appreciated
我可以得到 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;"> </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;"> </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;"> </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>
`
`
采纳答案by Dimitre Novatchev
Use:
使用:
//iframe//span
This selects every span
element that is a descendent of any iframe
element in the XML document.
这将选择作为 XML 文档span
中任何iframe
元素的后代的每个元素。
回答by meebee
Elements in iframe are in fact in another page. so you should first find address of that page which is value of src value of iframe and load it and then access the element in that page.
iframe 中的元素实际上在另一个页面中。所以你应该首先找到那个页面的地址,它是 iframe 的 src 值的值并加载它,然后访问该页面中的元素。
回答by GJ.
You need to set the scope of the xpath to the iframe's contentDocument property:
您需要将 xpath 的范围设置为 iframe 的 contentDocument 属性:
var iframe = document.getElementsByTagName("iframe")[0];
var theFirstSpan = document.evaluate('//span', iframe.contentDocument,
null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue
回答by FailedDev
//span [ancestor::iframe]
This will select all span elements which at some point have an ancestor iframe element.
这将选择在某些时候具有祖先 iframe 元素的所有 span 元素。
回答by Irfan Ahmed
We can't access elements within an iframe
directly. We need to swith to iframe first.
我们不能iframe
直接访问 an 中的元素。我们需要先切换到 iframe。
I am using PHP Facebook webdriver and this is perfectly working for me.
我正在使用 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);
Now, we can access that span
as,
现在,我们可以访问它span
,
$spanButton = $driver->findElement(
WebDriverBy::xpath('//span')
);
I hope this may help somebody !
我希望这可以帮助某人!