如何从iframe中查找所有链接?我在Selenium和C#中尝试过的以下代码以及其他一些变体,但是错误消息指出未找到此类元素:
var detailFrame = Driver.driver.FindElement(By.Id("twitter - widget - 0"));
Driver.driver.SwitchTo().Frame(detailFrame);
var linksTweetContents = Driver.driver.FindElements(By.XPath("/ol[@class='timeline-TweetList']//a"));
Driver.driver.SwitchTo().DefaultContent();
这是我使用过的SO链接:Selenium: Unable to access iframe and data inside it
我已经给出了html结构的尽可能多的细节,以显示所涉及的复杂性。希望得到您的反馈。
<html>
<head>
</head>
<body>
<script type = "test/javascript"> src = "somejavascriptlink"</script>
<div type = "test/javascript" id="divid1"> script links </div>
<span></span>
<form method="post" action="events" onsubmit="javascript:return WebForm_OnSubmit(); id="aspnetForm" novalidate="novalidate">
<div class="aspNetHidden">.....</div>
<script>....</script>
<script>...</script>
<script>...</script>
<script>...</script>
<script>...</script>
<div class="aspNetHidden">.....</div>
<script>...</script>
<script>...</script>
<div class="s4-notdlg noindex">....</div>
<div id="s4-workspace" class="ms-core-overlay">
<div id="s4-bodyContainer">
<a id="top"></a>
<header>....</header>
<div id="contentRow">
<div id="contentbox">
<div class="hcf-TopZone">...</div>
<div id="DeltaPlaceHolderMain">
<div class="hcf-maincontent hcf-960w-wrapper">
<div id="sideNavBox">....</div>
<div class="hcf-page-container">
<div class="col-md-4 hcf-page-content2">
<div class="row">
<div class="col-md-12">
<div class="row hcf-page-additionalZone">....</div>
<div class="row hcf-page-secWrapper">....</div>
<div class="row hcf-page-additionalZone">
<div class="col-md-12">
<div class="ms-webpart-zone ms-fullWidth">
<div id="MSOZoneCell_WebPartctl00_ctl62">
<div class="ms-webpart-chrome ms-webpart">
<div webpartid="5a1803e3-4e87">
<div id="ctl00_ctl62_g">
<div class="ms-rte-embedcode ms-rte-embedwp">
<iframe id="twitter-widget-0">
#document
<html class="SandboxRoot env-bp-min">
<head>....</head>
<body>
<div class="timeline-Widget">
<div class="timeline-Header timeline-InformationCircle-widgetParent">
<div>
<div class="timeline-InformationCircle">
<a href="linkurl">link description"</a>
</div>
</div>
<div class="timeline-Body customisable-border">
<div class="timeline-Body-notification timeline-NewTweetsNotification new-tweets-bar">
<button class="timeline-ShowMoreButton" data-scribe="element:show_new_tweets">Load new Tweets</button>
</div>
<div class="timeline-Viewport">
<ol class="timeline-TweetList">
<li>
<p><a>url link</a>
<ul class="timeline-Tweet-actions">
<li><a>url link</a></li>
<li><a>url link</a></li>
</ul>
</li>
<li><a>url link</a></li>
<li><a>url link</a></li>
<li><a>url link</a></li>
</ol>
</div>
</div>
</div>
</div>
</body>
</html>
</iframe>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</form>
</body>
</html>
答案 0 :(得分:1)
根据您共享的 HTML 来查找所需的 WebElements ,您必须:
诱导 WebDriverWait 以使所需的元素可见,并且您可以使用以下任一解决方案:
使用FrameToBeAvailableAndSwitchToIt(By.CssSelector())
:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt(By.CssSelector("iframe#twitter-widget-0")));
var linksTweetContents = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.CssSelector("ol.timeline-TweetList a")));
使用FrameToBeAvailableAndSwitchToIt(By.XPath())
:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt(By.XPath("//iframe[@id='twitter-widget-0']")));
var linksTweetContents = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.XPath("//ol[@class='timeline-TweetList']//a")));
在这里您可以找到有关Ways to deal with #document under iframe的相关讨论