Selenium VBA Excel - problem clicking a link within an iframe

Viewed 647

I am new to the forum and have some VBA experience, but not a lot.

I am using Selenium to facilitate interaction with a Chrome site with the aim of automating a series of requests to the site (Starleaf Web meetings) with the data in an Excel spreadsheet.

I can log in fine and can navigate from page to page using FindElementByID and FindElementByTag("a").Click statements.

I have a problem with navigating using this approach on one page. I have tried ByID(the first 2 IDs in the code below), ByLinkText, ByClass (portalContent, button, single), ByCssSelector; but nothing works. The macro stalls on code line of this form:

Set elem = bot.FindElementBy*****("Parameter")
    elem.FindElementByTag("a").Click

The code on the page in question is:

<div class="portalContent">
    <div id="viewConferencesAddDiv" class="buttons single" style="">
        <a id="viewConferences_add" href="https://portal.starleaf.com/#page=addUserConference" target="_parent">
            <span id="viewConferences_add_text">Schedule meeting</span>
        </a>
    </div>    

I wondered if the id="viewConferences_add" between the 'a' tag and the href might be the problem but I am not sure how to get round this.

An alternative approach that I have tried to use is to go directly to the page I need after login however this does not seem to work. I may have a syntax problem???

Here's what I tried:

 bot.Get "/#page=addUserConference"
 bot.Get "https://portal.starleaf.com/#page=addUserConference"
 bot.Get "//portal.starleaf.com/#page=addUserConference"

With this code afterwards to allow time for the page to load:

 Do Until InStr(1, bot.Url, "addUserConference") > 0
 Application.Wait (DateAdd("s", 1, Now))
 DoEvents
 Loop

Update

I was looking around and found someone talking about iframe switching. Looking at the page in F12 it does actually look as thought the 'Schedule Meeting' button might be in an iframe. The suggestion was to switch to if however I have no idea how to do that. There appear to be several iframes on the page - so I guess I need the ID. I think it might be id=mainPanel_iframe.

HTML of the iframe tag:

<iframe id="mainPanel_iframe" name="mainPanel_iframe" class="mochaIframe" src="https://portal.starleaf.com/pbx/node-1.live.pr.starleaf.com/portal/#page=users&amp;lang=en_int" marginwidth="0" marginheight="0" frameborder="0" scrolling="auto" style="height: 579px; width: 1543px;"></iframe>
1 Answers

To click on the link with text as Schedule meeting you need to induce some wait and you can use either of the following Locator Strategies:

  • Using FindElementByCss:

    bot.wait 5000
    bot.FindElementByCss("a#viewConferences_add[href*='addUserConference'] > span#viewConferences_add_text").Click
    
  • Using FindElementByXPath:

    bot.wait 5000
    bot.FindElementByXPath("//a[@id='viewConferences_add' and contains(@href, 'addUserConference')]/span[@id='viewConferences_add_text']").Click
    

Update

If the desired element is within an <iframe> first you need to SwitchToFrame as follows:

  • Using FindElementByCss:

    bot.wait 5000
    bot.SwitchToFrame "mainPanel_iframe"
    bot.wait 5000
    bot.FindElementByCss("a#viewConferences_add[href*='addUserConference'] > span#viewConferences_add_text").Click
    
  • Using FindElementByXPath:

    bot.wait 5000
    bot.SwitchToFrame "mainPanel_iframe"
    bot.wait 5000
    bot.FindElementByXPath("//a[@id='viewConferences_add' and contains(@href, 'addUserConference')]/span[@id='viewConferences_add_text']").Click
    

You you can find a find a relevant detailed discussion in How to send text with in the username field within an iframe using Selenium VBA


Reference

Ways to deal with #document under iframe

Related