Hello Ganesh Babar,
To set up and deploy an Azure WebJob using Selenium WebDriver (Edge) with .NET 6, follow these steps carefully.
First, install the required packages by running these commands in the Package Manager Console:
Install-Package Selenium.WebDriver
Install-Package Selenium.WebDriver.ChromeDriver
Install-Package Microsoft.Edge.SeleniumTools
Your project is targeting .NET 6. You can check this in your .csproj
file. If it’s not set, update it like this:
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
Next, download the Edge WebDriver from the official Microsoft site:
Download Edge WebDriver
Once downloaded, extract the file and place msedgedriver.exe
inside your WebJob project under the Drivers folder: Drivers/msedgedriver.exe
After placing the WebDriver file, go to Visual Studio and make sure to set its properties correctly:
right click onmsedgedriver.exe
file and update
- Build Action:
Content
- Copy to Output Directory:
Copy if newer
Now, add the code for handling the queue trigger and automation using Selenium.
Functions.cs
using System;
using System.IO;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using OpenQA.Selenium;
using OpenQA.Selenium.Edge;
namespace WebJob1
{
public class Functions
{
public static void ProcessQueueMessage([QueueTrigger("queue")] string message, ILogger logger)
{
logger.LogInformation($"[Queue Trigger Fired] Queue Message Received: {message}");
RunEdgeAutomation(logger);
}
public static void RunEdgeAutomation(ILogger logger)
{
try
{
string driverPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Drivers");
logger.LogInformation($"Computed Edge WebDriver path: {driverPath}");
var options = new EdgeOptions();
options.AddArgument("--headless");
options.AddArgument("--disable-gpu");
options.AddArgument("--no-sandbox");
options.AddArgument("--inprivate");
options.AddArgument("--remote-debugging-port=9222");
options.AddArgument("--disable-dev-shm-usage");
using (IWebDriver driver = new EdgeDriver(driverPath, options))
{
driver.Navigate().GoToUrl("https://en.wikipedia.org/wiki/Mahesh_Babu");
logger.LogInformation("Successfully navigated to the target URL");
var pageTitle = driver.Title;
logger.LogInformation($"Page Title: {pageTitle}");
}
}
catch (Exception ex)
{
logger.LogError($"Edge WebDriver automation failed: {ex.Message}");
}
}
}
}
Now, create the entry point for your Web Job inside Program.cs
.
Program.cs
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace WebJob1
{
internal class Program
{
static async Task Main(string[] args)
{
var host = CreateHostBuilder(args).Build();
using (host)
{
await host.RunAsync();
}
}
static IHostBuilder CreateHostBuilder(string[] args) =>
new HostBuilder()
.ConfigureWebJobs(b =>
{
b.AddAzureStorageCoreServices();
b.AddAzureStorageQueues();
})
.ConfigureLogging((context, loggingBuilder) =>
{
loggingBuilder.SetMinimumLevel(LogLevel.Error);
loggingBuilder.AddFilter("Function", LogLevel.Information);
loggingBuilder.AddFilter("Host", LogLevel.Debug);
loggingBuilder.AddConsole();
});
}
}
After setting up the code, you need to configure your appsettings.json
file. This file contains the connection string for Azure Storage.
appsettings.json
{
"AzureWebJobsStorage":"DefaultEndpointsProtocol=https;AccountName=myaccountname;AccountKey=mykey;EndpointSuffix=core.windows.net"
}
Replace myaccountname
and mykey
with your actual Azure Storage Account details.
Once everything is set up, you can run your WebJob locally. Just press F5 in Visual Studio and check the logs to see if the automation works as expected.
When you are ready to deploy, right-click on your WebJob project in Solution Explorer, select Publish, and choose Azure App Service as your target. Follow the deployment steps, and once it’s published, check the logs in Azure to make sure everything is running fine.
- When deploying to Azure, change your path to Azure. The sample Azure path will be like below:
C:\home\site\wwwroot\app_data\Jobs\Triggered\WebJob1\Drivers
For more details, please refer to this Stack Overflow post.
I hope this help!
If the answer is helpful, please click Accept Answer and kindly upvote it. If you have any further questions about this answer, please click Comment