JSFiddle - React, Tailwind, and code Playground

by JeffC

JavaScript

// write a function that finds a product name on any of the search result pages

function FindProduct(browserName, productName)
{
    var page = Sys.Browser(browserName).Page("/search/searchResults.jsp");
    page.Wait();

    var products = page.contentDocument.getElementsByClassName("grid_4 row");
    var noResults = (document.getElementsByClassName("no-search-results").length === 1);
    if (products.length = 0 && noResults)
    {
        return -1;
    }

    while (true)
    {
        for (i = 0; i < products.length; i++)
        {
            if (products[i].getElementsByTagName("h5")[0].innerText == productName)
            {
                return products[i].getElementsByTagName("img")[0];
            }
        }
        var nextPageLink = page.contentDocument.getElementsByClassName("next-page");
        if (nextPageLink.length === 0)
        {
            return -1;
        }
        nextPageLink.click();
        // TODO: wait for page to load
        products = page.contentDocument.getElementsByClassName("grid_4 row");
    }
}