Selenium c# Multiple Options - c#

I'm trying to use multiple options on my selenium project with Chrome as driver, but when i try to add headless + extension i got an error
System.InvalidOperationException occurred HResult=0x80131509
Message=unknown error: failed to wait for extension background page to
load:
chrome-extension://jmphljmgnagblkombahigniilhnbadca/_generated_background_page.html
from unknown error: page could not be found:
chrome-extension://jmphljmgnagblkombahigniilhnbadca/_generated_background_page.html
(Driver info: chromedriver=2.33.506120
(e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT
10.0.15063 x86_64) Source=WebDriver StackTrace: at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response
errorResponse) at
OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String
driverCommandToExecute, Dictionary`2 parameters) at
OpenQA.Selenium.Remote.RemoteWebDriver.StartSession(ICapabilities
desiredCapabilities) at
OpenQA.Selenium.Remote.RemoteWebDriver..ctor(ICommandExecutor
commandExecutor, ICapabilities desiredCapabilities) at
OpenQA.Selenium.Chrome.ChromeDriver..ctor(ChromeOptions options) at
SeleniumTest.Form1.<>c__DisplayClass5_0.b__0() in
C:\Users\Ussagui\source\repos\SeleniumTest\SeleniumTest\Form1.cs:line
53 at System.Threading.Tasks.Task.InnerInvoke() at
System.Threading.Tasks.Task.Execute()
my code is:
var option = new ChromeOptions();
option.AddArguments("--headless --disable-gpu");
option.AddExtensions(#"EXTENSION PATH");
var driver = new ChromeDriver(option);
var action = new Actions(driver);
driver.Navigate().GoToUrl(URL);
if i use only the extension or argument it works.
How can i use both? Or i cant?
Thank in advance.

A few points:
Though the arguments --headless and --disable-gpu are interrelated but are used for separate tasks. Hence we need to specify them separately.
Use optimal code block and comment/remove the debug/unused code. As driver.Navigate().GoToUrl(URL); doesn't need WebDriver instance to be casted so we need to comment/remove var action = new Actions(driver);
Your final code block will look like:
var option = new ChromeOptions();
option.AddArguments("--headless");
option.AddArguments("--disable-gpu");
option.AddExtensions(#"EXTENSION PATH");
var driver = new ChromeDriver(option);
driver.Navigate().GoToUrl(URL);

Related

Firefox headless not working as expected on azure devops raising NoSuchElementException

I'm currently facing an issue trying to run a selenium web automation with firefox(headless) on azure devops, I have the same browser version installed on the windows agent but still I get errors.
It works fine for Chrome on Azure and also locally it works fine for firefox so not really sure what is causing this issue.
These are the logs:
NUnit3TestExecutor discovered 2 of 2 NUnit test cases using Current Discovery mode, Non-Explicit run
1640697828054 geckodriver INFO Listening on [::1]:58930
1640697828085 mozrunner::runner INFO Running command: "C:\\Program Files\\Mozilla Firefox\\firefox.exe" "--marionette" "enable-automation" "--no-sandbox" "--headless" "--disable-gpu" "--window-size=1920,1080" "-foreground" "-no-remote" "-profile" "C:\\Users\\agent\\AppData\\Local\\Temp\\rust_mozprofileQUnaTu"
1640697829048 Marionette INFO Marionette enabled
[GFX1-]: RenderCompositorSWGL failed mapping default framebuffer, no dt
console.warn: SearchSettings: "get: No settings file exists, new profile?" (new NotFoundError("Could not open the file at C:\\Users\\agent\\AppData\\Local\\Temp\\rust_mozprofileQUnaTu\\search.json.mozlz4", (void 0)))
1640697832365 Marionette INFO Listening on port 58933
1640697832633 RemoteAgent WARN TLS certificate errors will be ignored for this session
1640697866771 Marionette INFO Stopped listening on port 58933
Given is presented the home page
-> error: Timed out after 30 seconds (33.8s)
And the user enters his credentials "empty"
-> skipped because of previous errors
Then Corresponding error message is displayed "empty"
-> skipped because of previous errors
1640697868763 geckodriver INFO Listening on [::1]:59006
Failed LoginWithBadInput("empty",null) [41 s]
Error Message:
OpenQA.Selenium.WebDriverTimeoutException : Timed out after 30 seconds
----> OpenQA.Selenium.NoSuchElementException : Unable to locate element: //h2[contains(text(),'Sign in with your email address')]
Stack Trace:
at OpenQA.Selenium.Support.UI.DefaultWait`1.Until[TResult](Func`2 condition)
at LFL.Automation.Framework.Actions.BaseActions.WaitFor(By by) in C:\agent\_work\3\s\Automation.Financial.Framework\Actions\BaseActions.cs:line 34
at ClientPortalAutomationFinal.Pages.LoginPage.ConfirmLoginPage() in C:\agent\_work\3\s\ClientPortalAutomationFinal\Pages\LoginPage.cs:line 49
at ClientPortalAutomationFinal.Steps.LoginSteps.GivenIsPresentedTheHomePage() in C:\agent\_work\3\s\ClientPortalAutomationFinal\Steps\LoginSteps.cs:line 29
at TechTalk.SpecFlow.Bindings.BindingInvoker.InvokeBinding(IBinding binding, IContextManager contextManager, Object[] arguments, ITestTracer testTracer, TimeSpan& duration)
at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.ExecuteStepMatch(BindingMatch match, Object[] arguments, TimeSpan& duration)
at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.ExecuteStep(IContextManager contextManager, StepInstance stepInstance)
at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.OnAfterLastStep()
at TechTalk.SpecFlow.TestRunner.CollectScenarioErrors()
at ClientPortalAutomationFinal.Features.LoginFeature.ScenarioCleanup()
at ClientPortalAutomationFinal.Features.LoginFeature.LoginWithBadInput(String flag, String[] exampleTags) in C:\agent\_work\3\s\ClientPortalAutomationFinal\Features\Login.feature:line 8
--NoSuchElementException
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(String mechanism, String value)
at OpenQA.Selenium.Remote.RemoteWebDriver.FindElementByXPath(String xpath)
at OpenQA.Selenium.By.<>c__DisplayClass19_0.<XPath>b__0(ISearchContext context)
at OpenQA.Selenium.By.FindElement(ISearchContext context)
at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(By by)
at LFL.Automation.Framework.Actions.BaseActions.<>c__DisplayClass11_0.<WaitFor>b__0(IWebDriver drv) in C:\agent\_work\3\s\Automation.Financial.Framework\Actions\BaseActions.cs:line 34
at OpenQA.Selenium.Support.UI.DefaultWait`1.Until[TResult](Func`2 condition)
Based on this looks like the browser it's opening but not able to navigate the website not really sure.
Hopefully someone has some idea of what could be the issue, thanks!
This error message...
OpenQA.Selenium.WebDriverTimeoutException : Timed out after 30 seconds
----> OpenQA.Selenium.NoSuchElementException : Unable to locate element: //h2[contains(text(),'Sign in with your email address')]
...implies that NoSuchElementException while trying to locate the element with the locator strategy.
Generally <h2> tags are header elements. Ideally, to locate any visible element you need to induce WebDriverWait for the ElementIsVisible() and you can use the following Locator Strategy:
Using XPath:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementIsVisible(By.XPath("//h2[contains(., 'Sign in with your email address')]")));

C# - selenium webdriver unable to open chrome browser window

Below is the selenium script to open chrome browser in C# on Windows 10 Visual Studio Code. I'm using Chrome 74 (64 bit) and corresponding ChromeDriver 74.0.3729.6. One thing to note is Chrome is not installed in default directory. This is due to our Office security purpose. It has installed in a different location. When I run the script It gives me error -Failed to create a Chrome process. Could some one please help me to understand why chrome browser isn't opening?
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using NUnit.Framework;
using SeleniumExtras.PageObjects;
namespace NAS
{
[TestFixture]
class Program
{
public static IWebDriver driver;
public Program(){}
[OneTimeSetUp]
public void startBrowser()
{
ChromeOptions opt = new ChromeOptions();
opt.BinaryLocation = #"C:\\ProgramData\\Microsoft\\AppV\\Client\\Integration\\520B8677-106E-430B-8927-6F9261C56329\\Root\\VFS\\ProgramFilesX86\\Google\\Chrome\\Application\\";
driver = new ChromeDriver(#"C:\\Progra~1\\Selenium",opt);
driver.Navigate().GoToUrl("http://www.google.com");
driver.Manage().Window.Maximize();
}
[Test]
public void testLogin()
{
Console.WriteLine("test running..");
}
[OneTimeTearDown]
public void closeBrowser()
{
driver.Close();
}
}
}
Error:
Running selected tests in c:\NAS\bin\Debug\netcoreapp2.2\NAS.dll
NUnit3TestExecutor converted 1 of 1 NUnit test cases
TearDown failed for test fixture NAS.Program
**OpenQA.Selenium.WebDriverException : unknown error: Failed to create a Chrome process.**
(Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729#{#29}),platform=Windows NT 10.0.14393 x86_64)
TearDown : System.NullReferenceException : Object reference not set to an instance of an object.
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver.StartSession(ICapabilities desiredCapabilities)
at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(ICommandExecutor commandExecutor, ICapabilities desiredCapabilities)
at OpenQA.Selenium.Chrome.ChromeDriver..ctor(ChromeDriverService service, ChromeOptions options, TimeSpan commandTimeout)
at NAS.Program.startBrowser() in c:\NAS\Program.cs:line 30
--TearDown
at NAS.Program.closeBrowser() in c:\NAS\Program.cs:line 46
NUnit Adapter 3.12.0.0: Test execution complete
NAS.Program.testLogin: failed
Total tests: 1. Passed: 0. Failed: 1. Skipped: 0
After adding executable name - chrome.exe
Running selected tests in c:\NAS\bin\Debug\netcoreapp2.2\NAS.dll
NUnit3TestExecutor converted 1 of 1 NUnit test cases
SetUp failed for test fixture NAS.Program
OpenQA.Selenium.WebDriverException : unknown error: Chrome failed to start: crashed
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location C:\ProgramData\Microsoft\AppV\Client\Integration\520B8677-106E-430B-8927-6F9261C56329\Root\VFS\ProgramFilesX86\Google\Chrome\Application\chrome.exe is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
(Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729#{#29}),platform=Windows NT 10.0.14393 x86_64)
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver.StartSession(ICapabilities desiredCapabilities)
at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(ICommandExecutor commandExecutor, ICapabilities desiredCapabilities)
at OpenQA.Selenium.Chrome.ChromeDriver..ctor(ChromeDriverService service, ChromeOptions options, TimeSpan commandTimeout)
at NAS.Program.startBrowser() in c:\NAS\Program.cs:line 28
NUnit Adapter 3.12.0.0: Test execution complete
NAS.Program.testLogin: failed
Total tests: 1. Passed: 0. Failed: 1. Skipped: 0
I think your just missing the executable name, like so for example
var chromeOptions = new ChromeOptions()
{
BinaryLocation = "D:\\Chrome\\chrome.exe"
};
var driver = new ChromeDriver(chromeOptions);

Not able to start a Selenium RemoteWebDriver instance in C# .NET4.5.2

here is my setup:
I've tried various driver/webdriver/grid versions, added capabilities just in case one mandatory was missing but had the same result.
Error also present if I delete the driver.exe file so this shouldn't be considered.
Start grid with bat file
start cmd /k java -jar grid3.14.jar -role hub -newSessionWaitTimeout 10
start cmd /k java -jar grid3.14.jar -role node -hub http://192.168.1.229:4444/grid/register
Execute C# code bellow
var options = new ChromeOptions();
var uri = new Uri("http://192.168.1.229:4444/wd/hub");
var driver = new RemoteWebDriver(uri, options);
Test fails with error:
Result StackTrace:
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver.StartSession(ICapabilities desiredCapabilities)
at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(ICommandExecutor commandExecutor, ICapabilities desiredCapabilities)
at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(Uri remoteAddress, ICapabilities desiredCapabilities)
at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(Uri remoteAddress, DriverOptions options)
at UnitTests.CodeRUnner.Run() in C:\UnitTests\UnitTests\Main.cs:line 26
Result Message:
Test method UnitTests.Main.Run threw exception:
System.InvalidOperationException: Unable to create new service: ChromeDriverService
Build info: version: '3.10.0', revision: '176b4a9', time: '2018-03-02T19:11:12.151Z'
System info: host: 'DESKTOP-F9MP198', ip: '192.168.1.229', os.name: 'Windows 10', os.arch: 'x86', os.version: '10.0', java.version: '1.8.0_181'
Driver info: driver.version: unknown (SessionNotCreated)
Node console log
17:19:01.903 INFO [ActiveSessionFactory.lambda$apply$11] - Matched factory org.openqa.selenium.remote.server.ServicedSession$Factory (provider: org.openqa.selenium.chrome.ChromeDriverService)
Hub log
17:19:01.895 INFO [RequestHandler.process] - Got a request to create a new session: Capabilities {browserName: chrome, goog:chromeOptions: {}}
17:19:01.896 INFO [TestSlot.getNewSession] - Trying to create a new session on test slot {server:CONFIG_UUID=2feea0b8-ace3-4bf5-8410-834034caba1c, seleniumProtocol=WebDriver, browserName=chrome, maxInstances=5, platformName=WIN10, platform=WIN10}

Issue with Selenium Grid

I am trying to get selenium grid working for the first time.
In the setup i have the following
Driver = new ChromeDriver(AppDomain.CurrentDomain.BaseDirectory + "../../../Drivers");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities = DesiredCapabilities.Chrome();
capabilities.SetCapability(CapabilityType.BrowserName, "chrome");
capabilities.SetCapability(CapabilityType.Platform, new Platform(PlatformType.Windows));
Driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), capabilities);
This is in c# using XUNIT and executed using resharper.
I get the error in visual studio
OpenQA.Selenium.WebDriverException
Unexpected error.
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response
errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String
driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver.StartSession(ICapabilities
desiredCapabilities)
at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(ICommandExecutor
commandExecutor, ICapabilities desiredCapabilities)
at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(Uri remoteAddress,
ICapabilities desiredCapabilities)
at MoneyUITests.Methods.General.StartBrowserDriver() in
C:\Projects\Money_AT\MoneyUITests\Methods\General.cs:line 276
at MoneyUITests.Tests.Navigation.NavigationExecuteTests..ctor() in
C:\Projects\XXXX\XXXX\Tests\Navigation\NavigationTests.cs:line
36
In the console, i get the following error
2017-11-06 17:08:35.821:WARN:osjs.HttpChannel:qtp22982090-131:
/wd/hub/session
java.io.IOException: org.openqa.grid.common.exception.GridException: Cannot
extract a capabilities from the request: {"desiredCapabilities":{"browserName":"chrome","version":"","platform":"WINDOWS","javascriptEnabled":true},"capabilities":{"firstMatch":[{"browserName":"chrome","javascriptEnabled":true}]}}
at org.openqa.grid.web.servlet.DriverServlet.process(DriverServlet.java:135)
and
Caused by: org.openqa.grid.common.exception.GridException: Cannot extract a capabilities from the request: {"desiredCapabilities":{"browserName":"chrome","version":"","platform":"WINDOWS","javascriptEnabled":true},"capabilities":{"firstMatch":[{"browserName":"chrome","javascriptEnabled":true}]}}
and
Caused by:java.lang.IllegalArgumentException: Illegal key values seen in w3c capabilities: [javascriptEnabled]
This is my first attempt to work with selenium grid, any guides or tips will be extremely appreciated.
I followed this guide - https://seleniumautomation84.wordpress.com/2014/08/06/selenium-grid-fundamentals-implemented-in-c/
Thanks in advance
I found this
https://github.com/SeleniumHQ/selenium/issues/4461
After updating selenium to 3.7.0 I was no longer getting this error.
Apparently, there is an issue with Selenium Grid 2 and Selenium 3.4/5 so updating your NuGet package the latest version will fix this issue.

Webdriver C# - RemoteWebDriver returns Specified cast is not valid

I am trying to write a test using C# under Mac. I was able to run tests on Chrome, but now I am going to the next step, by trying to initialize Internet Explorer located on a Windows VM I am using.
So in the VM I started first the selenium standalone server with the following command:
java -jar selenium-server-standalone-3.3.0.jar -role hub
Then, I connected a hub to it from the same VM:
java -jar -Dwebdriver.ie.driver=IEDriverSetup.exe selenium-server-standalone-3.3.0.jar -role node -hub http://192.168.1.4:4444/grid/register
I see from the logs that the node registers to the hub and listens for events and also am able to see in the hub's log that the node has registered.
192.168.1.4 is the IP if the Windows VM, where host the hub and note are located
Until now everything seems to be fine, now I write the following C# code to connect to hub:
`
private IWebDriver driver;
try {
DesiredCapabilities ip = DesiredCapabilities.InternetExplorer();
Uri URL = new Uri("http://192.168.1.4:4444");
driver = new RemoteWebDriver(URL, ip);
catch (Exception e) {
string stack = e.StackTrace;
}
`
When executing the code I get the following error:
`
{System.InvalidCastException: Specified cast is not valid.
at OpenQA.Selenium.Remote.RemoteWebDriver.StartSession (OpenQA.Selenium.ICapabilities desiredCapabilities) [0x00048] in <3440d4b964694e75ab172947d6b08035>:0
at OpenQA.Selenium.Remote.RemoteWebDriver..ctor (OpenQA.Selenium.Remote.ICommandExecutor commandExecutor, OpenQA.Selenium.ICapabilities desiredCapabilities) [0x0001e] in <3440d4b964694e75ab172947d6b08035>:0
at OpenQA.Selenium.Remote.RemoteWebDriver..ctor (System.Uri remoteAddress, OpenQA.Selenium.ICapabilities desiredCapabilities, System.TimeSpan commandTimeout) [0x00008] in <3440d4b964694e75ab172947d6b08035>:0
at OpenQA.Selenium.Remote.RemoteWebDriver..ctor (System.Uri remoteAddress, OpenQA.Selenium.ICapabilities desiredCapabilities) [0x00000] in <3440d4b964694e75ab172947d6b08035>:0
at SeleniumCheck.BrowserDriver..ctor () [0x000d9] in
`
I tried with Webdriver and Internet explorer versions 3.1.0, 3.3.0 and 3.4.0
I get the same error.
Does anyone have any idea what am I doing wrong? Doesn't seem to be a cast I am doing. the issue occurs at this line:
driver = new RemoteWebDriver(URL, ip);
I even tried
driver = (IWebDriver)new RemoteWebDriver(URL, ip);, but still the same.
It's the same if I try initializing Firefox. I am pretty sure it's not the IE capabilities. I get the same thing if i use
DesiredCapabilities.Firefox()

Categories

Resources