Act

apify/example-selenium

  • Builds
  • latest 0.0.2 / 2018-02-13
  • Created 2017-11-02
  • Last modified 2018-02-13
  • grade 4

Description

Example of loading a web page in headless Chrome using Selenium Webdriver.


API

To run the act, send a HTTP POST request to:

https://api.apify.com/v2/acts/apify~example-selenium/runs?token=<YOUR_API_TOKEN>

The POST payload will be passed as input for the act. For more information, read the docs.


Example input

Content type: application/json; charset=utf-8

{ "url": "http://www.wikipedia.com" }

Source code

Based on the apify/actor-node-chrome Docker image (see docs).

const Apify = require('apify');
const { Capabilities, Builder, By, until, logging } = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');

// Enable logging if needed
// logging.installConsoleHandler();
// logging.getLogger('webdriver.http').setLevel(logging.Level.ALL);

Apify.main(async () => {
    console.log('Fetching input...');
    const input = await Apify.getValue('INPUT');
    if (!input || !input.url) throw new Error('Invalid input, must be a JSON object with the "url" field!');
    
    console.log('Launching Selenium WebDriver...');
    const webDriver = await Apify.launchWebDriver();

    console.log(`Opening ${input.url}...`);
    await webDriver.get(input.url);
                   
    console.log('Getting the page title...');
    const pageTitle = await webDriver.executeScript(() => {
        return document.title;
    });
    console.log(`Page title: ${pageTitle}`);
    
    console.log('Saving output...');
    await Apify.setValue('OUTPUT', {
        url: input.url,
        pageTitle, 
    });
    
    console.log('Done.')
});