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!
Related
Im new at programming and I only found how to repair it in python.
This code was working for first but when i tried it later error appear.
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System.Threading;
namespace AternosServer
{
class Program
{
static void Main(string[] args)
{
IWebDriver driver = new ChromeDriver();
ChromeOptions options = new ChromeOptions();
options.AddArgument("--ignore-certificate-errors");
options.AddArgument("--ignore-ssl-errors");
options.AcceptInsecureCertificates = true;
driver.Navigate().GoToUrl("https://aternos.org/go/");
driver.FindElement(By.Id("user")).SendKeys("X");
driver.FindElement(By.Id("password")).SendKeys("X");
driver.FindElement(By.Id("login")).Click();
driver.Navigate().GoToUrl("https://aternos.org/server/");
}
}
}
I run your code 4 times using selenium with c#.
also sign up in your site - https://aternos.org/servers/
after that put my credential and test it, and it is working fine, no any issue found.
No need to write last line of your code -
driver.Navigate().GoToUrl("https://aternos.org/server/");
when you click on login button it will redirect on that page.
let me know if any issue regarding selenium with c#.
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'm using the 32 bit, version 3.14 version of the IEDriverServer.
I'm using IE v11 and have turned on protected mode in all zones.
I have the following code:
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
namespace ConsoleApp1
{
internal class Program
{
private static void Main(string[] args)
{
var options = new InternetExplorerOptions
{
IntroduceInstabilityByIgnoringProtectedModeSettings = true,
EnableNativeEvents = false,
IgnoreZoomLevel = true,
EnsureCleanSession = true,
ForceCreateProcessApi = true,
};
IWebDriver driver = new InternetExplorerDriver(<path to IEDriverServer>, options);
driver.Navigate().GoToUrl("http://localhost:60448/Account/Login");
}
}
}
When I run the console app, I get a console telling me the IEDriverServer version and what port it is listening on.
I see an IE dialog pop up that is deleting the browser cache and history.
Then IE v11 opens up with localhost:<whatever port the IEDriverServer is listening on>
In the browser, there is the text: This is the initial start page for the WebDriver server.
A minute later, Visual Studio shows me this exception:
The HTTP request to the remote WebDriver server for URL
http://localhost:<whatever port the console said the driver was listening on>/session timed out after 60 seconds.
The exception is happening on the line that instantiates the new InternetExplorerDriver even though I see an open instance of IE and it is displaying the page that is supposedly not responding.
Please help if you know how to get past this line.
I've already written few lines of code in C# using Selenium webdriver. As my application was transferred to the Electron framework everything has changed and honestly, I don't know how to cope with it right now.
Could you please clarify it to me? What steps should I take to simple start... I would like to continue my work in the current project (selenium, C#), but I'm not sure that it's possible, or I should completely start from scratch using a different language and framework?
I've read about Spectron, and checked the internet resources like stackoverflow, however I'm still in the point of unawareness...
Spectron with mocha is supposed to be faster.
But still here is what you need.This is Java & Selenium.
System.setProperty("webdriver.chrome.driver","C:\\electron-chromedriver\\bin\\chromedriver.exe");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setBinary("C:\\Users\\app.exe");
chromeOptions.addArguments("start-maximized");
DesiredCapabilities capability = new DesiredCapabilities();
capability.setCapability(CapabilityType.BROWSER_NAME, "Chrome");
capability.setCapability("chromeOptions", chromeOptions);
driver = new ChromeDriver(chromeOptions);
I have used the packaged electron app for binary (i.e) app.exe .
I think this is what you need.
Described below is related to using of Electron with .Net C# OpenQA.Selenium.
If you want to run electron app which being developed (consists of files index.html, main.js, etc.) you have to add following options (pay attention to 'app=' in cmd argument):
var options = new ChromeOptions();
options.BinaryLocation = #"your_path_to_electron\electron.exe";
options.AddArgument(#" app=path_to_folder_with_your_electron_app_src");
But if you want to run packaged electron app (*.exe) it is enough to use:
var options = new ChromeOptions();
options.BinaryLocation = #"path_to_folder_with_your_electron_app\your_electron_app.exe";
Also you can start any version of chromedriver.exe:
var service = ChromeDriverService.CreateDefaultService(path_to_folder_with_driver);
var driver = new ChromeDriver(service, options);
It might be helpful because I know that different electron applications are built on using of different version's drivers.
Try this for Electron application Initialization:
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
namespace Selenium_Demo
{
class Selenium_Demo
{
IWebDriver driver;
[SetUp]
public void Start_Browser()
{
ChromeOptions options = new ChromeOptions();
ChromeDriverService chromeService = ChromeDriverService.CreateDefaultService(#"C:\\selenium\\chromedriver_win32v\\chromedriver.exe",
#"C:\\Program Files\\Cerebrata\\Cerebrata.exe");
driver = new ChromeDriver(chromeService, options);
}
[Test]
public void Test()
{
System.Threading.Thread.Sleep(6000);
Console.WriteLine("Test Passed");
}
[TearDown]
public void Close_Browser()
{
driver.Quit();
}
}
}
IDE: Visual Studio 2015
Geckodriver.exe (Version 0.19.0) - Published: 9/25/2017
Firefox version: 56.0b8 (64-bit)
Selenium webdriver version: 3.6.0
Using Selenium and C # I did as lines of code below:
using (FirefoxDriver = new FirefoxDriver () driver)
{
driver.Navigate (). GoToUrl("https://www.teste.gov.br/seguro/loginPortal.asp");
Thread.Sleep (1000 * 60);
}
The page opens a message of "Your connection is not private", error "Your connection is not secure".
To resolve this issue you have already used the following codes:
profile = webdriver.FirefoxProfile ()
profile.accept_untrusted_certs = True
driver = new FirefoxDriver (profile)
profile = webdriver.FirefoxProfile ()
profile.accept_untrusted_certs = True
driver = new FirefoxDriver (profile)
profile.setAcceptUntrustedCertificates (true);
profile.setAssumeUntrustedCertificateIssuer (false);
driver = new FirefoxDriver (profile)
ffProfile.setAcceptUntrustedCertificates (true)
ffProfile.setAssumeUntrustedCertificateIssuer (false)
driver = new FirefoxDriver (ffProfile)
and other codes ...
But the problem continues. How to solve this problem?
TL:DR; There is no reasonable approach to solving this problem.
If it's absolutely necessary to visit the site only, e.g. to create a cookie, use PhantomJS. Every browser driver I've tried gives the error, and it's impossible to bypass without some sort of security exploit.
The browser is literally letting you know the site is insecure. Albeit its a government site, it might be compromised.
On a separate note, its probably cleaner to do this:
FirefoxDriver() ffDriver = new FirefoxDriver();
ffDriver.Navigate("myCoolSite.url");