The only way that I've found to enable downloads in Headless Chrome is with the following code:
var param = new Dictionary<string, object>();
param.Add("behavior", "allow");
param.Add("downloadPath", $"C:\\Users\\{Environment.UserName}\\Downloads\\");
driver.ExecuteChromeCommand("Page.setDownloadBehavior", param);
However, when using Selenium Grid, the driver must be initialized as RemoteWebDriver:
driver = new RemoteWebDriver(new Uri(url), options);
RemoteWebDriver doesn't have the ExecuteChromeCommand method and it can't be cast to ChromeDriver ((ChromeDriver)driver throws an exception).
Therefore, how can I enable downloads in headless chrome when using Selenium Grid?
Related
Need help to block save address pop up coming in chrome browser while executing selenium c# automation scripts.
Following all options already tried but no luck.
IWebDriver driver;
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.AddArgument("--disable-single-click-autofill");
chromeOptions.AddArgument("--disable-popup-blocking");
chromeOptions.AddExcludedArgument("--disable-infobars");
chromeOptions.AddAdditionalChromeOption("useAutomationExtension", "false");
chromeOptions.AddArgument("--disable-notifications");
chromeOptions.AddUserProfilePreference("disable-popup-blocking", "true");
new WebDriverManager.DriverManager().SetUpDriver(new ChromeConfig());
driver = new ChromeDriver(chromeOptions);
I was able to turn off the Save Address prompt using AddUserProfilePreference. It is called "autofill.profile_enabled". You can just add it to your ChromeOptions and pass those options in when instantiating the driver.
I got the name from this Reddit post:
https://www.reddit.com/r/selenium/comments/yicwf0/chromium_how_to_disable_save_and_autofill_address/
ChromeOptions options = new ChromeOptions();
options.AddArgument("start-maximized");
options.AddArgument("test-type");
options.AddArgument("disable-notifications");
options.AddUserProfilePreference("autofill.profile_enabled", false);
IWebDriver webDriver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), options, TimeSpan.FromSeconds(240));
Good morning.
I am developing a spider to review a few web pages. I can't do it without using Selenium. But the problem with Selenium is that it consumes a lot of resources and is slow. I am looking for the optimization way.
From what I see the main problem is that Selenium loads the entire website, with all its resources. But I just need javascript and html to work for me. But I don't need images. Can I somehow prevent images from loading in the Selenium browser in C #?
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using (IWebDriver driver = SeleniumUtility.GetChromeDriverHidden())
{
driver.Url = "https://stackoverflow.com/";
string html = driver.PageSource;
}
internal static ChromeDriver GetChromeDriverHidden(bool hidden = true)
{
ChromeDriverService service = ChromeDriverService.CreateDefaultService(".");
service.HideCommandPromptWindow = true; // Hide output commands in console
var options = new ChromeOptions()
{
AcceptInsecureCertificates = true // This lets the browser accept the insecure certificate. Set hidden = false
};
if (hidden)
{
options.AddArgument("headless"); // hide window if added to options
}
return new ChromeDriver(service, options);
}
I see one solution, but in C# I don't understand how to do it.
Try this, I hope it helps
ChromeOptions options = new ChromeOptions();
options.addArguments("headless","--blink-settings=imagesEnabled=false");
Or
IWebDriver driver;
ChromeOptions options = new ChromeOptions();
options.AddUserProfilePreference("profile.default_content_setting_values.images", 2);
driver = new ChromeDriver(options);
See the original answer here
I develop a small app (in C#) which automatize a test on a website with Selenium. Everything is going well. But when I try the same app with "headless" browser the test doesn't work. I have an issue with the code below :
var emailTextBox = driver.FindElement(By.Id("j_username"));
OpenQA.Selenium.WebDriverException : 'The HTTP request to the remote WebDriver server for URL http://localhost:49309/session/d4416c4b-e674-468b-8d6e-6a8bfc9bdf1d/element timed out after 60 seconds.'
The same test works with a normal browser but not in headless mode, I try to use Firefox, Chrome, PhantomJS (all in headless) and it doesn't work...
Do you have an idea ?
My whole code is :
'''
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
namespace MacDo
{
class Program
{
static void Main(string[] args)
{
var driverService = FirefoxDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;
var options = new FirefoxOptions();
options.AddArguments("-headless");
IWebDriver driver = new FirefoxDriver(driverService, options);
driver.Url = "https://www.mcdonalds.fr/";
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10000);
System.Threading.Thread.Sleep(2000);
var seConnecter = driver.FindElement(By.Id("seconnecter"));
seConnecter.Click();
driver.Close();
driver.Quit();
Console.ReadKey();
}
}
}
'''
I use Firefox Browser 73.0.1 (32 bits) (-> I use Geckodriver version 0.26.0)
As said before, it works well, but not in headless mode...
I had the same issue in past where script did not work in headless and I found out it was due to default resolution. You can try adding
options.addArguments("window-size=1920,1080");
Worth trying!
I'm trying to open a web page using Selenium ChromeDriver 76.0.3809.68 - chrome window opens but no success with loading the page itself.
all I get is 200 code, a favicon but no content at all.
The same web page loads normally on any browser including Chrome when not using Selenium.
(see error message in code section)
I've tried to set wait.Until to find the very final element of that page.
// Initiate driver
IWebDriver driver = new ChromeDriver(service, chromeOptions);
// open URL
Stopwatch sw = Stopwatch.StartNew();
driver.Navigate().GoToUrl(url);
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
IWebElement contentLoaded = wait.Until(x => x.FindElement(By.ClassName("bottom"))); // Here i get => OpenQA.Selenium.NoSuchElementException: 'no such element: Unable to locate element'
How to provide OperaWebDriver on c# use Selenium WebDriver
IWebDriver aDriver = new OperaDriver("path_to_operadriver.exe);
I have exception :
System.InvalidOperationException : unknown error: cannot find Opera
binary (Driver info: OperaDriver=0.2.0 )
Using setBinary gives me an error. I am able to resolve it by using this:
options.BinaryLocation = #"/path/to/opera";
Similarly for chrome as well.
You've got to download opera chromium driver.
Here's a snippet how it should work:
DesiredCapabilities capabilities = DesiredCapabilities.opera();
ChromeOptions options = new ChromeOptions();
options.setBinary("/path/to/opera");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);