Selenium ChromeDriver doesn't Navigate with --headless in console application c# - c#

Good day to all! I have a problem using Selenium ChromeDriver in console application, i use code like this:
var options = new ChromeOptions();
options.AddArgument("--headless");
options.AddArgument("--no-sandbox");
options.AddArgument(#"user-data-dir=G:\Data\ProfilesData\Profile1");
var driverService = ChromeDriverService.CreateDefaultService(Directory.GetCurrentDirectory());
driverService.HideCommandPromptWindow = true;
WebDriver driver = new ChromeDriver(driverService, options);
driver.Navigate().GoToUrl(URL);
Please help me understand so that the browser goes to the desired URL in the console application
I tried to remove the --headless parameter, the browser starts and goes to the desired page, this process does not work without visualization

I found a problem in using the profile, when commenting out the line with connecting the profile everything works, I found a comment on one of the sites that says about a Chrome bug, it cannot work with a profile in headless mode. But everything works fine on my local machine, it doesn't work on the server machine. It is necessary to look for some settings of the server machine.

Related

Selenium tests open IE in background on TFS build server

we are trying to use Selenium for testing our MVC application. On localhost in VS2017 , it´s running correct, the tests open IE, run the test and then close the IE.
On TFS build server, the tests start IE somehow on background (in Task manager I see two iexplorer.exe processes), but the window of IE is not visible. The tests find elements, but they are not able to write text in textbox, always get error like "Element cannot be interacted with via the keyboard because it is not focusable"
Localy I run Win10 and IE11, TFS build server run Windows Server 2012 R2 and IE11 .
//initialize driver in test constructor
InternetExplorerOptions options = new InternetExplorerOptions();
options.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
options.RequireWindowFocus = true;
driver = new InternetExplorerDriver(options);
driver.Manage().Window.Maximize();
//test itself
driver.Navigate().GoToUrl(appURL);
var x = driver.FindElement(By.Id("FiltrADuvodDotazu_DuvodDotazu"));
x.SendKeys("Automatizovaný test"); //here I get error
Is there way to run IE visibly, so the tests can interact with it?
I guess your agent run as a service and this is the reason the tests run on "headless mode" (and IE not supports it, like mentioned in the comments).
To solve it you need to configure the agent as an interactive process with auto-logon enabled.
When configuring the agent, select 'No' when prompted to run as a service. subsequent steps then allow you to configure the agent with auto-logon.
More info you can find here.

Appium : how to test webappication in chrome on windows 10?

I am trying to automate tests for a web application running in chrome on Windows 10 using Appium.
I have code like below that works perfectly fine uses chromedriver. I want to move this to appium based approach.
RemoteWebDriver driver = new ChromeDriver(#"C:\Users\Administrator\Downloads\chromedriver_win32");
driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(60));
driver.Url = "https://www.bing.com/";
RemoteWebElement element = (RemoteWebElement)driver.FindElementById("sb_form_q");
element.SendKeys("webdriver");
element.SendKeys(Keys.Enter);
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(60));
wait.Until(x => x.Title.Contains("webdriver"));
I searched internet and find a lot of code to test webapplications on chrome on android emulators on windows devices. I did not find any sample that runs chrome browser directly on windows 10.
Based on what i understood, i tried adopting the code by making changes to desired capabilities like below.
DesiredCapabilities caps = DesiredCapabilities.Chrome();
caps.SetCapability(CapabilityType.BrowserName, "chrome");
caps.SetCapability(CapabilityType.Version, "60");
caps.SetCapability(CapabilityType.Platform, "Windows 10");
caps.SetCapability("platformName", "Windows");
//caps.SetCapability("app", #"C:\Users\Administrator\Downloads\chromedriver_win32\Chromedriver.exe");
caps.SetCapability("app", #"Chrome");
caps.SetCapability("deviceName", "WindowsPC");
driver = new RemoteWebDriver(new Uri("http://127.0.0.1:4725/wd/hub"), caps, TimeSpan.FromSeconds(60));
driver.Url = "https://www.bing.com/";
((IWebDriver)driver).Navigate().GoToUrl("https://www.bing.com/");
RemoteWebElement element = (RemoteWebElement)driver.FindElementById("sb_form_q");
element.SendKeys("webdriver");
element.SendKeys(Keys.Enter);
Thread.Sleep(5000);
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(60));
wait.Until(x => x.Title.Contains("webdriver"));
This however doesn't work.
I think the problem is I am not able to figure out how to set desired capabilities so that appium service running on http://127.0.0.1:4725/wd/hub is able to launch chromedriver and run tests.
Can someone please help point out mistake with the above code?
Appreciate your help.
you need to pass the capabilities of the android phone not the windwos pc as you passes
caps.SetCapability("platformName", "Windows");
Like that and you need to pass the OS version of the emulator or android phone if you are using it..
For reference visit this site you will know everything that you need to know..
http://www.automationtestinghub.com/launch-chrome-browser-on-mobile-device/
Happy to help.. let us know if that solution works for you
As nicely put up here - https://crossbrowsertesting.com/blog/appium/appium-native-web-hybrid-applications/
Appium is an open source test automation frameworktes that drives iOS and Android native, mobile, and hybrid apps using the WebDriver protocol a.k.a. The Selenium API. That’s to say, Appium is like Selenium’s cousin, and used for testing mobile apps instead of web applications in desktop browsers.
Per the discussion here - https://discuss.appium.io/t/can-appium-automate-desktop-web-browsers/746/5 too it seems appium is for testing on mobile devices only.

How to open the Default Chrome Profile through Selenium, ChromeDriver and GoogleChrome

I want to load a new Selenium ChromeDriver that is using Chrome as if I open Chrome from my dock (Essentially it'll have all my extensions, history, etc.)
When I use the following code:
ChromeOptions options = new ChromeOptions();
options.AddArgument("user-data-dir=C:\\Users\\User\\AppData\\Local\\Google\\Chrome\\User Data\\");
options.AddArgument("disable-infobars");
options.AddArgument("--start-maximized");
ChromeDriver chromeDriver = new ChromeDriver(options);
It loads the Chrome browser with me signed into my Gmail and with all my extensions, just like I want, but the rest of my code:
chromeDriver.Navigate().GoToUrl("https://www.youtube.com/");
doesn't execute. But when I use the following
ChromeOptions options = new ChromeOptions();
options.AddArgument("user-data-dir=C:\\Users\\Andrea\\AppData\\Local\\Google\\Chrome\\User Data\\Default");
options.AddArgument("disable-infobars");
options.AddArgument("--start-maximized");
ChromeDriver chromeDriver = new ChromeDriver(options);
The rest of my code executes perfectly (Notice the 'Default' added to the end of the first Argument). Any tips or suggestions on how I can get the first block of code (The one without 'Default' on the end) to execute the rest of my program would be great. Thanks!
I know this is an old question, but what worked for me is to do remove the "C:\" and replace all of the backslashes with forward slashes. So, with that from the original question, this should work to load the default profile:
options.AddArgument("user-data-dir=/Users/User/AppData/Local/Google/Chrome/User Data");
The Default Chrome Profile which you use for your regular tasks may contain either/all of the following items:
History
Bookmarks
Cookies
Extensions
Themes
Customized Fonts
All these configurations untill and unless are part of your Test Specification it would be a overkill to load them into the session initiated by Selenium WebDriver. Hence it will be a better approach if you create a dedicated New Chrome Profile for your tests and configure it with all the required configuration.
Here you will find a detailed discussion on How to create and open a Chrome Profile
Once you have created the dedicated New Chrome Profile for your tests you can easily invoke the Chrome Profile as follows:
ChromeOptions options = new ChromeOptions();
options.AddArgument("user-data-dir=C:\\Users\\User\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 2");
options.AddArgument("disable-infobars");
options.AddArgument("--start-maximized");
ChromeDriver chromeDriver = new ChromeDriver(options);
chromeDriver.Navigate().GoToUrl("https://www.youtube.com/");
Here you will find a detailed discussion on How to open URL through default Chrome profile using Python Selenium Webdriver
I have the same issue. I don't know how to fix it, I guess the root cause is white space in profile path.
I know a workaround for this. Just copy the C:\\Users\\Andrea\\AppData\\Local\\Google\\Chrome\\User Data to c:\myUserData (no space in the path).
Then add the argument.
options.AddArgument("user-data-dir=C:\\myUserData");
This is an Old question, but if you are facing this issue, all you have to do is close all tabs, Just shut down the chrome window..
Selenium can't use the data since it is already in use.
Hope you fond this helpful.

C# chrome driver only opening as background process on IIS / Windows Server 2016

I am running chromedriver on windows server 2016 with IIS, i have my test project installed and invoking it with an MVC5 API. That all seems fine but chromedriver and chrome.exe only seems to open as a background processes.
The same code opens these fine locally, i am not using any of the driver flags for headless browsing either. if i return the drive page source i can see that chromedriver went to google and returned the correct html in my API.
It just does not work for normal / non headless tests with google or our application.
var driver = x.StartWebDriver();
driver.Manage().Window.Maximize();
driver.Navigate().GoToUrl("http://www.google.com");
return driver.PageSource;
Any ideas?
I have got the same issue and this is how I solve this:
Open a command prompt and navigate to your ChromeDriver location.
Execute chromedriver.exe and you will see the message as below.
image here
In the image above you see chrome driver is listening on port 9515.
Now change your code as below, rebuild and call you api to execute your test.
ChromeOptions options = new ChromeOptions();
//set your chromeoptions here
Uri uri = new Uri("http://localhost:**9515");
_driver = new RemoteWebDriver(uri, options);
Open the chrome driver and note the port number(eg:5353)
In java:
System.setProperty(chromeDriverName, chromeDriverLocation);
ChromeOptions options = new ChromeOptions();
URL uri = new URL(chromeDriverPort);//http://localhost:5353
WebDriver driver = new RemoteWebDriver(uri, options);
your problem gets solved in is server.

How to Detach Chrome Browser from Chrome Driver (Selenium Web Driver C#)

I want to use Selenium Web Driver in VS 2010 C# to open a Chrome browser, navigate to some web page and then close the driver but keep the browser open. I realize that I will have to manually close the browser afterwards and I'm okay with that.
So far I have:
DriverService service = ChromeDriverService.CreateDefaultService();
ChromeOptions options = new ChromeOptions();
options.AddAdditionalCapability("chrome.detach",true);
m_driver = new ChromeDriver(service, options, TimeSpan.FromMilliseconds(1000));
[m_driver does stuff like navigate page, double click stuff, etc]
[last line: try to close driver but not browser]
I have tried all the following as the last line
m_driver.Dispose(); // closes both browser and driver
m_driver.Close(); //closes just the browser and not the driver
m_driver.Quit(); // closes both browser and driver
service.Dispose(); // closes both browser and driver
Any ideas?
We can detach chrome instance from chromedriver using "detach" options.
Sample Code:
ChromeDriverService cdservice = new ChromeDriverService.Builder()
.usingDriverExecutable(new File("/path/to/chromedriver.exe"))
.withLogFile(new File("/path/to/chromedriver.log"))
.usingAnyFreePort().withVerbose(true).build();
cdservice.start();
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("detach", true);
ChromeDriver driver = new ChromeDriver(cdservice,options);
driver.manage().timeouts().implicitlyWait(1, TimeUnit.MINUTES);
driver.get("http://www.google.com/");
// Do not call driver.quit().. instead stop chromedriver service.
cdservice.stop();
This is simply not possible, that kind of separation does not exist.
chromeservice.driver.close()
has worked for me in the past but in this case you may need to write some coding for a method.
It is posible a least in c# you need the next lines:
driverOptions.AddExcludedArgument("enable-automation");
driverOptions.AddAdditionalCapability("useAutomationExtension", false);
(Python) When I called the selenium .get() function, Chrome would open up and close shortly after. I found online the advice to use the following code to remedy this issue: It worked for me in Python3; MacOSX 12.3 Monterey; Chrome Version 105.0.5195.102
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(chrome_options=options, executable_path='/Users/<user_acct_name>/.wdm/drivers/chromedriver/mac64/105.0.5195/chromedriver')

Categories