Automate Chrome with Google Script?

I am purely an amateur coder so please forgive any stupid questions !

I know a little bit of VBA and can pull together code which: (a) creates a new Internet Explorer object (b) navigates to a given URL (c) accesses specific div tags and returns their innertext

However I would be really interested to know how to do a similar thing using Google Scripts and Google Chrome instead ?

Thanks in advance !

1 answer

  • answered 2018-08-16 10:05 ScottMcC

    I would suggest that you take a look at the UrlFetchApp class object in Google Apps Script and specifically the fetch(url) method.

    Here is an example to get you started

    // Make a GET request and log the returned content.
    var response = UrlFetchApp.fetch('http://www.google.com/');
    Logger.log(response.getContentText());
    

    To analyse the contents of the page I would suggest using the XMLService class in addition to the above method. See the following for an example:

    // Log the title and labels for the first page of blog posts on the G Suite Developer blog.
    function parseXml() {
      var url = 'https://gsuite-developers.googleblog.com/atom.xml';
      var xml = UrlFetchApp.fetch(url).getContentText();
      var document = XmlService.parse(xml);
      var root = document.getRootElement();
      var atom = XmlService.getNamespace('http://www.w3.org/2005/Atom');
    
      var entries = document.getRootElement().getChildren('entry', atom);
      for (var i = 0; i < entries.length; i++) {
        var title = entries[i].getChild('title', atom).getText();
        var categoryElements = entries[i].getChildren('category', atom);
        var labels = [];
        for (var j = 0; j < categoryElements.length; j++) {
          labels.push(categoryElements[j].getAttribute('term').getValue());
        }
        Logger.log('%s (%s)', title, labels.join(', '));
      }
    }