Forum Discussion

wt0vremr's avatar
wt0vremr
Occasional Contributor
9 years ago
Solved

How do I iterate over links in TC Python script?

def GetPageLinks(page):
  linkList = []
  i = 0
  while i < page.contentDocument.links.length:
    if Browsers.CurrentBrowser.Family == Browsers.btIExplorer:
      linkList.append(str(page.contentDocument.links.item[i])) #IE
    if Browsers.CurrentBrowser.Family == Browsers.btChrome:
      linkList.append(str(page.contentDocument.links.item[i].href)) #chrome
    i = i + 1
  linkList = list(set(linkList))
  return linkList

Hello.

How do I ask for link from FF? None of what I tried works. The code above seems to work fine for Chrome & IE.

  • Hi wt0vremr,

     

    The following code works for me in Firefox:

     

        if Browsers.CurrentBrowser.Family == Browsers.btFirefox:
          linkList.append(str(page.contentDocument.links.item(i).href)) #Firefox

     

    Does it work for you?

4 Replies

  • TanyaYatskovska's avatar
    TanyaYatskovska
    SmartBear Alumni (Retired)

    Hi wt0vremr,

     

    The following code works for me in Firefox:

     

        if Browsers.CurrentBrowser.Family == Browsers.btFirefox:
          linkList.append(str(page.contentDocument.links.item(i).href)) #Firefox

     

    Does it work for you?

    • TanyaYatskovska's avatar
      TanyaYatskovska
      SmartBear Alumni (Retired)

      BTW, for those of you who are interested in learning Python. Very soon, we are setting up a webinar to talk about using this language to writes tests. You can find more information here.

    • wt0vremr's avatar
      wt0vremr
      Occasional Contributor

      Tanya,
      Thank you very much for your reply. The code you provided works like a charm :catvery-happy:
      As for me, TestComplete documentation is missing some essential python examples. There are a lot of articles related to TestComplete, which only have code samples in other scripting languages. I know python far better then any other language, however some python + TestComplete usage specifics may still be unclear.

      • TanyaYatskovska's avatar
        TanyaYatskovska
        SmartBear Alumni (Retired)

        Hi wt0vremr,

         

        I’m glad to hear that my reply has helped :smileyhappy:

         

        We have implemented the Python support only in the latest TestComplete version. Perhaps, the help articles were not fully updated yet. I think our Documentation Team will add more Python examples soon!