1

How do I find all links from inside an iframe? The following code I've tried in Selenium and C# along with a few other variations but error message says no such element is found:

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();

Here is the SO link I have used: Selenium: Unable to access iframe and data inside it

I have given as much detail of the html structure to show the complexity involved. Hoping for your feedback.

<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>
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Sohel
  • 656
  • 2
  • 11
  • 31

1 Answers1

2

As per the HTML you have shared to locate the desired WebElements you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.
  • Induce WebDriverWait for the desired elements to be visible and you can use either of the following solutions:

    • Using 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")));
      
    • Using 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")));
      

Here you can find a relevant discussion on Ways to deal with #document under iframe

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    Thank you DebanjanB for pointing me to the right direction. I had to make one change, instead of using ExpectedConditions.VisibilityOfAllElementsLocatedBy used ExpectedConditions.PresenceOfAllElementsLocatedBy and the code picked up all links inside the iFrame. Thank you again for your help. – Sohel Nov 29 '18 at 16:34
  • @Sohel Glad to be able to help you. However I would suggest you to look at the [ExpectedConditions Class](https://seleniumhq.github.io/selenium/docs/api/dotnet/html/T_OpenQA_Selenium_Support_UI_ExpectedConditions.htm) in details. [PresenceOfAllElementsLocatedBy](https://seleniumhq.github.io/selenium/docs/api/dotnet/html/M_OpenQA_Selenium_Support_UI_ExpectedConditions_PresenceOfAllElementsLocatedBy.htm) will only ensure the _expectation for checking that all elements present on the web page that match the locator_ but not necessarily those elements will be **visible** / **interactable** – undetected Selenium Nov 29 '18 at 18:56
  • @Sohel As you are looking for all the links either you should be using [VisibilityOfAllElementsLocatedBy](https://seleniumhq.github.io/selenium/docs/api/dotnet/html/M_OpenQA_Selenium_Support_UI_ExpectedConditions_VisibilityOfAllElementsLocatedBy.htm) or [VisibilityOfAllElementsLocatedBy Method (ReadOnlyCollection)](https://seleniumhq.github.io/selenium/docs/api/dotnet/html/M_OpenQA_Selenium_Support_UI_ExpectedConditions_VisibilityOfAllElementsLocatedBy_1.htm) – undetected Selenium Nov 29 '18 at 18:59