I’m using edge version 131.0.2903.86 (Official build) (64-bit)
and Selenium.WebDriver.4.27.0 through the NuGet Package Manager > Package Manager Console.
if I Don’t use the line
options.AddArgument("--headless");
then it will save the source pages files to the hard disk fine. each source file is about 500 KB size.
but if i will use the line
options.AddArgument("--headless");
then each saved source file will be about 5 KB size missing most of the content.
this is my code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Support.UI;
namespace Downloads
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Download();
}
private void Download()
{
// Define the output folder and file
string outputFolder = @"D:Links"; // Folder to save text files
Directory.CreateDirectory(outputFolder); // Ensure the folder exists
var urls = new List<string>
{
"https://test.com/page1.html"
};
var options = new ChromeOptions();
options.AddArgument("--headless");
options.AddArgument("--disable-gpu");
options.AddArgument("--no-sandbox");
using (var driver = new ChromeDriver(options))
{
var allLinks = new List<string>();
foreach (var url in urls)
{
try
{
driver.Navigate().GoToUrl(url);
// Save the page source for debugging
File.WriteAllText($"D:\page_source_{Guid.NewGuid()}.html", driver.PageSource);
}
catch { }
}
}
}
}
}
In my code, I have replaced the real URLs with a dummy link (https://test.com/page1.html) for privacy reasons, but in reality, the links I’m using are valid and working fine.