How to set up Selenium and ChromeDriver to run in headless mode - c#

I am trying to run a browser test using xUnit, Selenium, and Chrome Canary (headless mode) but I keep getting this error:
OpenQA.Selenium.WebDriverException
The HTTP request to the remote WebDriver server for URL
http://localhost:58692/session timed out after 60 seconds.
Here's my code:
var chromeOptions = new ChromeOptions
{
BinaryLocation = #"C:\Users\<USERNAME>\AppData\Local\Google\Chrome SxS\Application\chrome.exe",
DebuggerAddress = "127.0.0.1:9222"
};
chromeOptions.AddArguments("no-sandbox", "headless", "disable-gpu");
_driver = new ChromeDriver(chromeOptions) {Url = Url};
I'm quite new to C# so I'm not sure if I'm doing something blatantly wrong, or if I'm just missing a setting. Googling the above error told me that I need to set the debugger address and use the no-sandbox flag, but neither seem to be helping.
Using these versions:
selenium 3.4
chromedriver 2.29
xunit 2.2

Taking out the debugger address made it work.
var chromeOptions = new ChromeOptions
{
BinaryLocation = #"C:\Users\<USERNAME>\AppData\Local\Google\Chrome SxS\Application\chrome.exe",
};

In case that others will get here while googling...
What worked for me was to download the latest chrome-driver from this link:
https://sites.google.com/a/chromium.org/chromedriver/downloads
Happy coding :)

case "chrome9515headless":
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.AddArgument("--headless");
chromeOptions.AddArgument("--disable-gpu");
chromeOptions.AddArgument("--disable-infobars");
chromeOptions.AddArgument("--disable-extensions");
chromeOptions.AddArgument("--window-size=1200,900");
chromeOptions.AddArgument("--disable-browser-side-navigation");
webDriver = new RemoteWebDriver(new Uri("http://127.0.0.1:9515"),
chromeOptions.ToCapabilities());
break;
case "chrome9515canary":
ChromeOptions chromeOptionsCanary = new ChromeOptions();
chromeOptionsCanary.BinaryLocation= #"C:\Users\********\AppData\Local\Google\Chrome SxS\Application\chrome.exe";
//chromeOptionsCanary.AddArgument("--headless");
//chromeOptions.AddArgument("--disable-gpu");
chromeOptionsCanary.AddArgument("--disable-infobars");
chromeOptionsCanary.AddArgument("--disable-extensions");
chromeOptionsCanary.AddArgument("--window-size=1200,900");
chromeOptionsCanary.AddArgument("--disable-browser-side-navigation");
webDriver = new RemoteWebDriver(new Uri("http://127.0.0.1:9515"),
chromeOptionsCanary.ToCapabilities());
break;

Related

How to initialze an appium android driver on version 4.0.0 with selenium 3.141.0

I can't find any documentation outlining how to initialize an appium android driver.
I had this test somewhat working with appium 3.0.0.2 and selenium 3.11 but then received the following error when trying to find an element by class name:
OpenQA.Selenium.InvalidSelectorException: 'Locator Strategy 'css selector' is not supported for this session
Ideally I don't want to go back to an older version of selenium as I already have a set of working tests using 3.14. This is my test at the moment, and I cant find clear documentation telling me how to implement an appium driver in C#.
public void androidTest(){
AndroidDriver<AndroidElement> driver;
DesiredCapabilities cap = new DesiredCapabilities();
cap.SetCapability("deviceName", "myName");
cap.SetCapability("platformName", "Android");
cap.SetCapability("automationName", "UiAutomator2");
cap.SetCapability("appPackage", "myPackage");
cap.SetCapability("appActivity", "myActivity");
Uri url = new Uri("http://127.0.0.1:4723/wd/hub");
driver = new AndroidDriver<IWebElement>(url, cap);
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
driver.FindElementByName("android.widget.EditText").SendKeys("testString");
}
I get the following two error messages
Argument 1:
cannot convert from 'System.Uri' to 'OpenQA.Selenium.Remote.ICommandExecutor'
Argument 2:
cannot convert from 'OpenQA.Selenium.Remote.DesiredCapabilities' to 'OpenQA.Selenium.DriverOptions' ArenaTests
What arguments do I need to pass into this constructor to get this setup working?
I worked this out. If anyone runs into this, the following initialisation works for me.
public void androidTest(){
AndroidDriver<AndroidElement> driver;
AppiumOptions options = new AppiumOptions();
options.PlatformName = "Android";
options.AddAdditionalCapability("deviceName", "MyDevice");
options.AddAdditionalCapability("platformVersion", "PlatformV");
options.AddAdditionalCapability("automationName", "UiAutomator2");
options.AddAdditionalCapability("appPackage", "MyPackage");
options.AddAdditionalCapability("appActivity", "MyActivity");
Uri url = new Uri("http://127.0.0.1:4723/wd/hub");
driver = new AndroidDriver<AndroidElement>(url, options);
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
// Some example selectors
driver.FindElementByClassName("android.widget.EditText").SendKeys("test");
driver.FindElement(MobileBy.AndroidUIAutomator("new UiSelector().className(\"android.widget.EditText\").instance(1)")).SendKeys(Username);
}
thanks h brooker you helped me alot
for UWP users:
AppiumOptions options = new AppiumOptions();
options.PlatformName = "UWP";
options.AddAdditionalCapability("deviceName", "WindowsPC");
options.AddAdditionalCapability("app", AppId);
session = new WindowsDriver<WindowsElement>(new
Uri("http://127.0.0.1:4723"), options);
I think the URI is creating an issue ..
Try using URL like the following snippet..
URL url = new URL("http://127.0.0.1:4723/wd/hub");
driver = new AppiumDriver<MobileElement>(url,cap);

Chrome driver block image extension freeze the page?

With the new Selenium.WebDriver.ChromeDriver.74.0.3729.6 update, the problem came out.
https://chrome.google.com/webstore/detail/block-image/pehaalcefcjfccdpbckoablngfkfgfgj?hl=tr
I'm trying to use the extension above.
ChromeDriverService chromeDriverService = ChromeDriverService.CreateDefaultService();
chromeDriverService.HideCommandPromptWindow = true;
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.AddExtensions(#Application.StartupPath.ToString() + #"\block-image.crx");
driver = new ChromeDriver(chromeDriverService, chromeOptions, TimeSpan.FromMinutes(10));
run the program. My result page remains "data :,". does not go to the page I want. different extensions have tried the same unfortunately.

How to set commandTimeout on RemoteWebdriver?

How can I set the commandTimeout for a RemoteWebdriver in Selenium?
If I would do the same on a ChromeDriver, I would just do something like:
var service = ChromeDriverService.CreateDefaultService(driverPath);
var options = new ChromeOptions();
driver = new ChromeDriver(service, options, TimeSpan.FromSeconds(120));
..but what is the equivalent to this when I'm using a RemoveWebdriver? My first guess is by using something like:
var capabilities = DesiredCapabilities.Chrome();
capabilities.SetCapability("Capability-name-here", TimeSpan.FromSeconds(120));
driver = new RemoteWebDriver(testserver, capabilities);
But I can't find any documentation on what capabilities that can be set, and what string/object I should pass to .SetCapability.
The constructor for RemoteWebDriver has an overload that takes a timeout argument. So the remote equivalent would be:
var options = new ChromeOptions();
var driver = new RemoteWebDriver(testserver, options.ToCapabilities(), TimeSpan.FromSeconds(120);
Note carefully that this timeout is for the HTTP requests between the local .NET bindings code and the Java remote Selenium server. It may or may not affect the command timeout between the Selenium server and its local instance of chromedriver.exe.

ChromeOptions to change the language from US to UK

What are the ChromeOptions (C#) to change the default language of the browser from US to UK using Selenium v3 and Chrome v60+?
Thanks
By using the lang argument
var options = new ChromeOptions();
options.AddArgument("lang=en-GB");
var driver = new ChromeDriver(options);
Try this code :-
ChromeOptions options = new ChromeOptions();
options.AddArguments("--lang=es");
ChromeDriver driver = new ChromeDriver(options);

How to start ChromeDriver in headless mode

I want to try out headless chrome, but I am running into this issue, that I can't start the driver in headless mode. I was following google documentation. am I missing something ? The code execution gets stuck in var browser = new ChromeDriver(); line
Here is my code:
var chromeOptions = new ChromeOptions
{
BinaryLocation = #"C:\Users\2-as Aukstas\Documents\Visual Studio 2017\Projects\ChromeTest\ChromeTest\bin\Debug\chromedriver.exe",
DebuggerAddress = "localhost:9222"
};
chromeOptions.AddArguments(new List<string>() {"headless", "disable-gpu" });
var browser = new ChromeDriver(chromeOptions);
browser.Navigate().GoToUrl("https://stackoverflow.com/");
Console.WriteLine(browser.FindElement(By.CssSelector("#h-top-questions")).Text);
UPDATE
Chrome version 60 is out so all you need to do is to download Chromdriver and Selenium via Nuget and use this simple code and everything works like a charm. Amazing.
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
...
var chromeOptions = new ChromeOptions();
chromeOptions.AddArguments("headless");
using (var browser = new ChromeDriver(chromeOptions))
{
// add your code here
}
DATED
There is a solution until the official release of Chrome 60 will be released. You can download Chrome Canary and use headless with it. After installation set BinaryLocation to point to chrome canary also comment out the DebuggerAddress line(it forces chrome to timeout):
var chromeOptions = new ChromeOptions
{
BinaryLocation = #"C:\Users\2-as Aukstas\AppData\Local\Google\Chrome SxS\Application\chrome.exe",
//DebuggerAddress = "127.0.0.1:9222"
};
chromeOptions.AddArguments(new List<string>() { "no-sandbox", "headless", "disable-gpu" });
var _driver = new ChromeDriver(chromeOptions);
For you that did not get reference for ChromeDriver.
Use this step :
Download the dll from this: http://seleniumtestings.com/selenium-download/
Extract, and you should see: Selenium.WebDriverBackedSelenium.dll, ThoughtWorks.Selenium.Core.dll, WebDriver.dll and WebDriver.Support.dll
Add those files via "Add Reference"
Now you can use it:
String url = "http://www.google.com";
var chromeOptions = new ChromeOptions();
chromeOptions.AddArguments(new List<string>() {
"--silent-launch",
"--no-startup-window",
"no-sandbox",
"headless",});
var chromeDriverService = ChromeDriverService.CreateDefaultService();
chromeDriverService.HideCommandPromptWindow = true; // This is to hidden the console.
ChromeDriver driver = new ChromeDriver(chromeDriverService, chromeOptions);
driver.Navigate().GoToUrl(url);
====
If after you run, you are still facing error about no ChromeDriver.exe file, try to add the Selenium.WebDriver.ChromeDriver, WebDriver.ChromeDriver, WebDriver.ChromeDriver.win32, Selenium.Chrome.WebDriver via nuget.
As alternative:
Add 2 libraries via NuGet like below picture.
Try below Code:
String url = "http://www.google.com";
var chromeOptions = new ChromeOptions();
chromeOptions.AddArguments(new List<string>() { "headless" });
var chromeDriverService = ChromeDriverService.CreateDefaultService();
ChromeDriver driver = new ChromeDriver(chromeDriverService, chromeOptions);
driver.Navigate().GoToUrl(url);
What OS you're running? I see on developers.google.com/web/updates/2017/04/headless-chrome that headless won't be available on Windows until Chrome 60.
Below i have given how to set the headless to true for firefox and chrome browsers.
FirefoxOptions ffopt = new FirefoxOptions();
FirefoxOptions option = ffopt.setHeadless(true);
WebDriver driver = new FirefoxDriver(option);
ChromeOptions coptions = new ChromeOptions();
ChromeOptions options = coptions.setHeadless(true);
WebDriver driver = new ChromeDriver(options);

Categories

Resources