Hey guys,
first I would like to send a thank to Gintaras, for this amazing software
In addition to meeting my needs in terms of automation, LibreAutomate allowed me to discover and start with C#.
I would like to contribute, so I'm sharing this useful alternative to Selenium, which allow an other approach regarding browser automation.
With PuppeteerSharp the browser Chromium is embedded and is by default updated every time you run the code (can be a fix version).
You can chose to launch it with Headless mode enable or disable.
The browser can be easily set to keep the sessions and the coockies, which I find really useful.
You can find more here https://www.puppeteersharp.com/index.html
Just for information, below is a sample code to generate a PDF file from a website page.
The "UserDataDir" allow to save the session as with a common browser.
/*/ nuget -\PuppeteerSharp; /*/
using System.Windows.Forms;
using PuppeteerSharp;
using var browserFetcher = new BrowserFetcher();
await browserFetcher.DownloadAsync(BrowserFetcher.DefaultChromiumRevision);
var browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
Headless = false,
UserDataDir = @"D:\LibreAutomate - WorkSpaces\LibreAutomate\files\UserData" // Spécifiez le répertoire souhaité pour stocker les données du profil utilisateur
});
var page = await browser.NewPageAsync();
await page.GoToAsync("https://fr.wikipedia.org/wiki/Mario_Kart:_Super_Circuit");
await page.PdfAsync(@"D:\test\page.pdf");
// Fermez la page
await page.CloseAsync();
Victor-P, thanks for steering me back into web scraping after a couple of years of neglect! Gintaras, thank you for LA! It seems to me that this can do quite a bit of RPA-ish stuff.
I wanted to post this PuppeteerSharp code that scrapes Hacker News for links, uses 'print.it' to show them, and then kills the browser. (I noticed that 'await page.CloseAsync()' still leaves that 'about:blank' page open, so I took the route below. I still need to figure out how to get rid of that 'about:blank' page when the browser starts, but I'm sure a little googling and fiddling will get me there in short order.
/*/ nuget -\PuppeteerSharp; /*/ //.
using PuppeteerSharp;
script.setup(trayIcon: true, sleepExit: true);
//..
using var browserFetcher = new BrowserFetcher();
await browserFetcher.DownloadAsync(BrowserFetcher.DefaultChromiumRevision);
var browser = await Puppeteer.LaunchAsync(new LaunchOptions {
Headless = false,
UserDataDir = @"E:\LibreAutomate\UserData" // Pick your own data dir!
});
var page = await browser.NewPageAsync();
{
await page.GoToAsync("https://news.ycombinator.com/");
print.it("Get all urls from page");
var jsCode = @"() => {
var arr = [], l = document.links;
for(var i=0; i<l.length; i++) {
arr.push(l[i].href);
}
return arr;
}";
var results = await page.EvaluateFunctionAsync(jsCode);
foreach (var result in results)
{
print.it(result.ToString());
}
print.it("Finished.");
}
browser.Disconnect();
await browser.CloseAsync();
Hi, Unfortunately I cannot help you on this one my friend as I have switched form PuppeeterSHarp to https://playwright.dev which is more powerfull and also because the developper of PuppeeterSHarp has joined the team of Playwright.
Another avantage of Playwright is that it's a Microsoft project.
Hi birdywen, unfortunately I can't help either. I just ran that script, and for me it still runs without errors. I do see that I also have PuppeteerExtraSharp installed from Nuget, not sure if that would make a difference, it shouldn't since I don't reference it in that script.
11-23-2023, 06:20 PM (This post was last modified: 11-23-2023, 06:21 PM by birdywen.)
Hi Friend, I have fixed that issue by deleting that line of code. I don’t know the logic behind but at least it worked. Thanks! By the way, playwright is very powerful. I hope you could shared some real world code example about playwright. I really love that
/*/ role exeProgram; outputPath %folders.Documents%\YourFolder; icon .\Robot.ico; nuget Base\microsoft.playwright; /*/
using Au;
using Au.Types;
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Playwright;
using System.Reflection;
using System.Windows.Forms;
using System.Drawing;
await Task.Delay(3000);
/*Group Row of the page. 1st filter by Role must contain the text "payments"
We combine the Roles to construct the Locator named “rowLocator”*/
var rowLocator = page
.GetByRole(AriaRole.Rowgroup)
.Filter(new() { HasText = "payments" })
//The 2nd filter by Role must not contain the text "Activity"
.GetByRole(AriaRole.Row)
.Filter(new() { HasNotText = "Activité" });
/*We filter the Locator; it must not contain the text “organization”
locate the Download button by Xpath and click */
await rowLocator
.Filter(new() { HasNotText = "organisation" })
.Locator("xpath=//button").First.ClickAsync();
Console.WriteLine("Download");
LogEvent("Download button clicked");
await Task.Delay(8000);
LogEvent("Closing browser");
await browser.CloseAsync();
dialog.show("Task completed","You can click OK to close", title: "Your_Project", icon: DIcon.Info);
}
}
}
catch (Exception ex)
{
LogException(ex);
ShowErrorMessage(ex.Message);
}
// Ajouter une ligne de fin au fichier de log
using (StreamWriter logFile = File.AppendText(logFilePath))
{
logFile.WriteLine($"--- Log Ended: {DateTime.Now} ---");
}
}
public class Program
{
public static async Task Main(string[] args)
{
var bot = new VTCBot();
await bot.Main(args);
}
}
}
Hello guys,
I'm going to share a little script that use Playwright. I've moved to Playwright mainly for this script.
The purpose was to connect a specific website with login to collect the weekly results of specifi VCT driver.
The first challenge was to use a persistent browser, in order to avoid having to fulfill the verification process.
The second part was the need of various options to fulfill the navigation requirements until the download of the file.
Hopefuly Playwright provide a very large choice of Locators and options to reach your goal.
I have provided comments for some locators.
I'm not a Dev, so the script can appear to be uggly.
Hope everything is clear, as french is my native language.