The following warnings occurred:
Warning [2] count(): Parameter must be an array or an object that implements Countable - Line: 895 - File: showthread.php PHP 7.2.34 (Linux)
File Line Function
/showthread.php 895 errorHandler->error




Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
RPA Challlenge - first exercise
#1
This code is ugly but works, and is not slow. I average from 169ms to 195ms on VERY old hardware. (You can Google this to see how it compares with others' results.)
I want to tweak this so it will run headless, but I believe it will require a different approach.
Note that I used PuppeteerSharp, PuppeteerExtraSharp and (maybe) PuppeteerExtraSharp.Plugins.ExtraStealth from Nuget. Maybe you won't need them all.
If you copy-and-paste this code you'll likely need to modify it using Code->Find window and Code->Find UI element. I don't imagine it will work for you as-is.


Code:
Copy      Help
// script "RPAchallenge7.cs"
/*/ nuget PExtraSharp\PuppeteerExtraSharp; nuget XL\ExcelDataReader; nuget XL\ExcelDataReader.DataSet; /*/
using PuppeteerExtraSharp;
using PuppeteerExtraSharp.Plugins.ExtraStealth;
using PuppeteerSharp;
using ExcelDataReader;

/*
This is a quick-and-dirty stab at rpachallenge.com's
first exercise. I do not have this working headless yet;
I expect it will be faster, but I will need to use
Javascript to find and click on elements, I think.
My times on very old hardware range from 169ms to 195ms
with this script.
*/

var extra = new PuppeteerExtraSharp.PuppeteerExtra();
var jsCode = $@""; // Lots of JS will be executed in one fell swoop.
string path1 = folders.Downloads + "challenge.xlsx";
if (filesystem.exists(path1)) {
    filesystem.delete(path1);
}


Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);


// Use stealth plugin
extra.Use(new StealthPlugin());

using var browserFetcher = new BrowserFetcher();
await browserFetcher.DownloadAsync(BrowserFetcher.DefaultChromiumRevision);


// Launch the puppeteer browser with plugins
var browser = await extra.LaunchAsync(new PuppeteerSharp.LaunchOptions {
    Args = new[]
    {

        /*
         * You can't access an <iframe> with different origin using JavaScript,
         * it would be a huge security flaw if you could do it.
         * For the same-origin policy browsers block scripts trying to access a frame with a different origin.
         */

    "--start-maximized",
    "--disable-web-security",
    "--disable-features=IsolateOrigins,site-per-process",
    },

    Headless = false,
    UserDataDir = @"E:\LibreAutomate\UserData", // Pick your own data dir!
    IgnoredDefaultArgs = new string[] {
    "--enable-automation",
    }
});


// Create a new page
var page = await browser.NewPageAsync();

await page.GoToAsync("https://rpachallenge.com");
//await page.DisposeAsync();
//await browser.DisposeAsync();
/*var w = wnd.find(1, "Rpa Challenge - Chromium", "Chrome_WidgetWin_1");*/


var w2 = wnd.find(1, "Rpa Challenge - Chromium", "Chrome_WidgetWin_1");
w2.ShowMaximized();
// You will probably need to use Code->Find Window here
var c = w2.Child(1, "", "Intermediate D3D Window");
string image = @"image:iVBORw0KGgoAAAANSUhEUgAAAEsAAAAiCAYAAAAXtSR4AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAN0SURBVGhD5Zq9S1tRGMaDoouiqIguTurgIC46SCuU1o/BoYWWdhIUB0dRhBYTjFEoxNFBHBwU/AMcXBwURJzERecmuTafxSbRWPKpT/Me70mv5jY2aXKuhzzwkHve897A++Oc9x5yY3IoXjxnf1M86md6fOHLmtfa67/EVeQXorE4Ure3KLaePawsu3RiaYeuImpJpZN8sMiPgMXiCbWc0kpOWGnz7RkKX6ullF7SwiJ70j1KpKSGdZ1u5iIlNSx66omU1LCSqZRahhhJDUu0yh7WrXp4VRQFVqsVHo8Hd3d3mbhWZQmLYHDH43EGZn19HSaTCRsbGyyeSCQyOVxlBUtbPH0mk8nM+PDwEJOTk5kcDovEY2UHi6TdYtFoFC6XC+Pj45ibm4Pb7UYsFlNn/+QaAuvdh0/4YrFmxSlGc4/juVyIePE3NzeYmZlBQ0MDGhsbUVdXh/r6enbd1NQEi8XCVh6JQxYOy/bVjpev3jwARtcUozlt7lPOV6n0UYNgURPv7OxEW1sb1tbW4PP5EA6HEQqF2Nzq6ipaWlrQ09ODYDDI7iMbsg0/m+/hECQOimJ6ubmcr6hgWiV9fX3o7e1lfYlWTyAQUDMAr/f+eyORCLq6ujA0NMTGhsEi3wN7XTAociHa29tjTz2n08nG09PTGBkZYRDJBJF6F+n8/Jzlnp6esrFhsPiKeqGuML2cp5yvCAaBaG9vz2zJiYkJdHd3Z2C1trZiamqKzdGxorm5GYuLi2zOEFjarafdknq5uVyI6HjQ39/PrgnIzs4Otra22JYkUw/b3d1lcEgdHR3sQUAyrMFrtx4HVuoGTwDGxsZQXV2N4+NjNfp3HRwcoKKiArOzs8asrLfvP+r2KIrR3ON4LhciKpz6EAEbHR2F2WzGwsICVlZWYLfb2fX8/DyGh4dRVVXFcilGMqxnFcP5ilbHyckJKisrGYR/MUE9OzszrmcVy/mKmjppf38fAwMDqKmp0QVErq2txeDgII6Ojtg9hh4diuFCxE/wJIfDgc3NTSwtLcFmszEvLy9je3ub/QrBxe8pu21I4ivsKVE+zy27bfi/khaW4varJYiTtLD8P36qJYiTtLCCAl+uckkJy3nhQzIp9s0OSUpYIv4EoifpYIl+Za+VVLCM6FNaPWtYync/vIFLXAavEBP8qj5bwG/54HaVRl3QKgAAAABJRU5ErkJggg==";
var im = uiimage.find(1, c, image);
im.MouseClick();
wait.s(1);
keys.send("Esc");
mouse.move(100, 100);
// You will probably need to use Code->Find Image here
var w = wnd.find(1, "Rpa Challenge - Chromium", "Chrome_WidgetWin_1");
var c2 = w.Child(1, "", "Intermediate D3D Window");
string image2 = @"image:iVBORw0KGgoAAAANSUhEUgAAAHcAAAAZCAYAAAALx7GgAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAADZSURBVGhD7dNRCsIwEITh3MPe/wTig4cRfOk1IoMshDKuEpBup/Pw0ZJNIPSn7fFcu2lyXGGOK8xxhTmuMMcV5rjCpOO21lLszJ4uy9KvtzudAWbYw2aMfFy2DhXjRjwWOJt9Ihl3/DuZ2LM9VwGLOBMWZOOydageF8aYs2HBcYuKqLNhIY2LD/ALdnZP2Z1iVvHeo7/HPaqjx42weI7vbG/mlHEDm++NxZwNfNo/t6Is4kxg2bgZdqaCb/EiMJsxknHtzXGFOa4wxxXmuMIcV5jjCnNcWWt/AYRNgUmMS4PbAAAAAElFTkSuQmCC";
var im2 = uiimage.find(1, c2, image2);
im2.MouseClick();

mouse.move(100, 100);
// You will probably need to use Code->Find Image here
var w3 = wnd.find(1, "Rpa Challenge - Chromium", "Chrome_WidgetWin_1");
var c3 = w3.Child(1, "", "Intermediate D3D Window");
string image3 = @"image:iVBORw0KGgoAAAANSUhEUgAAAGsAAAAXCAYAAAAMX7G2AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAADBSURBVGhD7dVBCsIwEIXh3MPe/wTiwsMIbnqN6CwCQR7NuHH641v8tGQaSPkobY/n3h0jY4EyFihjgTIWKCxWay2V2lvZZdv69XaXsyhm8YyaobHU+twZsQaGAjuaRcYqSKGsoCJjFTXjZKAiYxU2kDJQ0ft99I/5M7W5ssyZznjuua+x1CIhOtaAiut8r54dGasghZMBM9aPO0JZgaGxMqm9la2+ngGmZlisf8xYoIwFyligjAXKWKCMBcpYmPb+AlpcSWWh2sNtAAAAAElFTkSuQmCC";
var im3 = uiimage.find(1, c3, image3);
im3.MouseClick();

mouse.move(100, 100);


var w4 = wnd.find(1, "Rpa Challenge - Chromium", "Chrome_WidgetWin_1");
var e = w4.Elm["web:LINK", "DOWNLOAD EXCEL cloud_download"].Find(1).MouseClick();

wait.s(2);
//keys.send("Esc");

// NOW TO PROCESS THE DOWNLOAD
// Open the downloaded file

string file = folders.Downloads + "challenge.xlsx";
using (var stream = File.OpenRead(file)) {
    using var r = ExcelReaderFactory.CreateReader(stream);
    var results = r.AsDataSet();
    var colCount = results.Tables[0].Columns.Count;
    var rowCount = results.Tables[0].Rows.Count;
    
    //print.it(rowCount);
    for (int i = 1; i < rowCount; i++) {
        var rs = results.Tables[0].Rows[i];
        var firstName = rs[0];
        var lastName = rs[1];
        var company = rs[2].ToString();
        var role = rs[3];
        var address = rs[4];
        var email = rs[5];
        var phone = rs[6].ToString();
        ///print.it(rs[0], rs[1], rs[2], rs[3], rs[4], rs[5], rs[6]);
        jsCode += $"""document.querySelector('input[ng-reflect-name="labelFirstName"]').value = '{firstName}'""" + Environment.NewLine;
        jsCode += $"""document.querySelector('input[ng-reflect-name="labelLastName"]').value = '{lastName}'""" + Environment.NewLine;
        jsCode += $"""document.querySelector('input[ng-reflect-name="labelCompanyName"]').value = '{company}'""" + Environment.NewLine;
        jsCode += $"""document.querySelector('input[ng-reflect-name="labelRole"]').value = '{role}'""" + Environment.NewLine;
        jsCode += $"""document.querySelector('input[ng-reflect-name="labelAddress"]').value = '{address}'""" + Environment.NewLine;
        jsCode += $"""document.querySelector('input[ng-reflect-name="labelEmail"]').value = '{email}'""" + Environment.NewLine;
        jsCode += $"""document.querySelector('input[ng-reflect-name="labelPhone"]').value = '{phone}'""" + Environment.NewLine;
        jsCode += $"""document.querySelector('input[value="Submit"]').click()""" + Environment.NewLine;
    }

    // for debugging
    print.it(jsCode);
}

wait.ms(100);
await page.EvaluateExpressionAsync("document.querySelector('button.waves-effect').click()");
await page.EvaluateExpressionAsync(jsCode);
// Wait 2 seconds
await page.WaitForTimeoutAsync(2000);
// Take a screenshot
await page.ScreenshotAsync(@"E:\LibreAutomate\Downloads\rpa_challenge.png");
await page.DisposeAsync();
await browser.DisposeAsync();

/*

*/
   
Best regards,
burque505


Attached Files
.zip   RPAchallenge7.zip (Size: 3.91 KB / Downloads: 119)


Messages In This Thread
RPA Challlenge - first exercise - by burque505 - 09-01-2023, 07:45 PM

Forum Jump:


Users browsing this thread: 2 Guest(s)