C# Selenium Headless Chrome Access Browser Log - c#

For non-headless Chrome, when running an NUnit test with the debugger attached, this works fine.
For headless Chrome (without a debugger attached), it doesn't - there's nothing in driver.Manage().Logs.GetLog(LogType.Browser);.
My test setup is as follows:
ChromeOptions chromeOptions = new();
chromeOptions.AddArguments("headless");
IWebDriver driver = new ChromeDriver(chromeOptions);
// Set a window size (possibly not relevant but including it here just in case)
var size = driver.Manage().Window.Size;
size.Width = 1366;
size.Height = 768;
driver.Manage().Window.Size = size;
// Do the test which should cause a browser error
driver.Navigate().GoToUrl("invalid url");
ILogs logs = driver.Manage().Logs;
var errors = logs.GetLog(LogType.Browser).Where(x => x.Level == LogLevel.Severe).Select(x => x.Message).ToList();
Assert.That(errors.Count, Is.EqualTo(1));
As I mentioned above, this works in non-headless mode. In headless mode, there are no logs available at all.
A few things I've tried to make it work headlessly, to no avail yet:
chromeOptions.AddArguments("--log-level=ALL");
chromeOptions.AddArguments("--enable-logging --v=1");
chromeOptions.SetLoggingPreference(LogType.Browser, LogLevel.All);
What should I do to allow browser errors to be picked up in headless mode?

Related

C# Selenium - headless browser

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!

How to stop logging to console with selenium firefox driver

When I create a firefox driver with a firefox driver service this is logged to the console: 1564067211938 mozrunner::runner INFO Running command: "C:\\Program Files\\Mozilla Firefox\\firefox.exe" "-marionette" "-foreground" "-no-remote" "-profile" "C:\\Users\\minec\\AppData\\Local\\Temp\\rust_mozprofile.wCNOb94oHRE2" The problem is the console when this happens gets effectively separated from my program and doesn't close when I close it with the stop button in visual studio. I also can't log anything to the console after that.
Is there any way to disable it?
Here is my code:
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(geckoDriverPath.Replace(#"\geckodriver.exe", ""), "geckodriver.exe");
var driver = new FirefoxDriver(service);
I figured out how to remove all logs except fatal error logs.
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(geckoDriverPath.Replace(#"\geckodriver.exe", ""), "geckodriver.exe");
var options = new FirefoxOptions();
options.LogLevel = FirefoxDriverLogLevel.Fatal;
var driver = new FirefoxDriver(service, options);
Try adding System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE, "target" + File.separator + "browser.log");
before the browser is being initialized
ie. before
var driver = new FirefoxDriver(service);

Why wont chromedriver even start on windows 10 server?

currently I have a program that automates task filling.
I intend to run this program on my windows server I connect to via RDP however every time I run the program, selenium chromedriver seems to just go into an idle state and does nothing!
I have the latest chromedriver as of posting.
I've already tried a try-catch and display the errors however none seem to appear
My code for starting chromedriver:
ChromeOptions options = new ChromeOptions();
var driverService = ChromeDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;
options.AddArgument("--window-size=1920,1080");
options.AddArgument("--disable-gpu");
options.AddArgument("--disable-extensions");
options.AddArgument("--log-level=3");
//options.AddArgument("--headless");
options.AddArgument("--disable-notifications");
options.AddArgument("--disable-popup-blocking");
options.AddArgument("ignore-certificate-errors");
options.AddArgument("--proxy-bypass-list=*");
After setting the useragent and proxy I then try to navigate to the link:
//try timeout
try
{
chromedriver.Navigate().GoToUrl(links[rnd.Next(0, links.Length)]);
}
catch (OpenQA.Selenium.WebDriverTimeoutException e)
{
chromedriver.Quit();
Console.WriteLine("[#] Proxy Timeout - Thread Restarting...", Color.Orange);
errors++;
loopt += 10;
continue;
}
Note this program works 100% fine on normal pc's (I've tried 4)
Please help! Thanks in advance

Running a Selenium test in Firefox creates two tabs and runs the in the "non-active" tab

When I create an instance of the Firefox Web driver, it successfully opens Firefox. However, it opens it with two tabs (one "regular" Firefox tab and one IE tab; the IE tab is active and remains active for the duration of the testing unless I manually switch to the tab that the test is actually executing in).
It will run the test in the Firefox tab (i.e. the non-active one).
I'm instantiating my Firefox web driver like this:
var firefoxOptions = new FirefoxOptions()
{
Profile = new FirefoxProfile(),
UseLegacyImplementation = false,
BrowserExecutableLocation = #"C:\Program Files (x86)\Mozilla Firefox\Firefox.exe"
};
firefoxDriver = new FirefoxDriver(firefoxOptions);
firefoxDriver.Manage().Timeouts().ImplicitWait = new TimeSpan(0, 0, 10);
I'd include the code for the unit test, too, but the problem occurs during initialization prior to me running any tests.
Also, when I do cleanup like this:
[TestCleanup]
public void Cleanup()
{
if (firefoxDriver != null)
{
firefoxDriver.Close();
firefoxDriver.Dispose();
}
}
it closes the tab that the test was running in (the Firefox tab). However, it only closes that tab - the IE tab and the browser both stay open.
This question seems to be somewhat related, but the behavior's somewhat different because Selenium isn't trying to actually execute the test in both tabs - it only ever uses the one tab. Also, the OP there was using Firefox 20.0 and I'm using Firefox 52.2.0.
Simply, we can create profile and use it. I answered here Firefox 44.0.1 opening two tabs , when running selenium webdriver code
Another way, we can create profile pro-grammatically like
FirefoxProfile profile= new FirefoxProfile();
profile.setPreference(“browser.startup.homepage”,”https://...");
WebDriver driver = new FirefoxDriver(profile);
Just use about:config in firefox URL , it will provide settings.

Unable to resize the chrome window size when tests running on selenium Grid

I am facing one issue when tried to maximize the remote chrome browser to a specific size when tests are written in C# running on Selenium Grid.
I tried below options
options.AddArgument("--window-size=1920, 1080");
Driver.Manage().Window.Size = new Size(1920, 1080);
But, the browser does not maximize.
Can anyone please help me?
Try this:
ChromeOptions options = new ChromeOptions();
options.AddArguments("--disable-extensions");
_webDriver = new ChromeDriver("c:\path"), options);
_wait = new WebDriverWait(_webDriver, TimeSpan.FromSeconds(10));
_webDriver.Manage().Window.Maximize();

Categories