Unable to launch Selenium Web Driver on AWS lambda - c#

I try to launch Selenium web driver using Chrome but get such error:
Cannot start the driver service on http://localhost:45189
Here is my code:
public string FunctionHandler(string input, ILambdaContext context)
{
var driver = CreateLambdaChromeDriver();
driver.Url = "https://www.wikipedia.org";
var html = driver.PageSource;
return html;
}
public ChromeDriver CreateLambdaChromeDriver()
{
ChromeOptions options = new ChromeOptions();
// Set the location of the chrome binary from the resources folder
// Include these settings to allow Chrome to run in Lambda
options.AddArguments("disable-gpu");
options.AddArguments("headless");
options.AddArguments("window-size=1366,768");
options.AddArguments("no-sandbox");
return new ChromeDriver(options);
}
How can I fix this error?

Related

WebDriver throws timeout error when trying to access Url property

I click on button which downloads PDF file from server and opens it in new tab. After click i wait when the tab will be opened:
TimeSpan timeSpan = TimeSpan.FromSeconds(10);
WebDriverWait waiter = new WebDriverWait(driver, timeSpan);
waiter.Until(drv => drv.WindowHandles.Count == 2);
When this PDF file is opened i switch to new tab:
string handle = drv.WindowHandles.Last();
drv.SwitchTo().Window(handle);
Then i try to access Url property on web driver:
TimeSpan timeSpan = TimeSpan.FromSeconds(10);
WebDriverWait waiter = new WebDriverWait(driver, timeSpan);
waiter.Until(driver => driver.Url.StartsWith("blob:"));
But i get this error:
The HTTP request to the remote WebDriver server for URL http://localhost:44829/session/fc28864b05b7a2b3ad16fd69e29fb48d/url timed out after 60 seconds.
This issue appears only in Headless mode on linux machine:
public static IWebDriver Create()
{
ChromeOptions options = new ChromeOptions();
options.AddArgument("headless");
options.AddArgument("no-sandbox");
options.AddArgument("window-size=1920x1080");
IWebDriver driver = new ChromeDriver(options);
return driver;
}
But there are no problems in simple chrome mode on Windows:
public static IWebDriver Create()
{
IWebDriver driver = new ChromeDriver();
driver.Manage().Window.Maximize();
return driver;
}
This issue appears when trying to search elements too.

selenium chromedriver browser not open after publish

In my MVC web application, I'm using selenium C# web driver to read some data from HTML file. my application works properly when I execute my application through VS(HTML file opening through chrome and reading HTML properly). But after I publish and host application in IIS server HTML file not opening through the chrome browser. (browser not opening), here is my code.
public class CribController : Controller
{
public ActionResult Index()
{
try
{
IWebDriver driver = new ChromeDriver(#"C:\Selenium\");
driver.Navigate().GoToUrl("D:/Crib/toEdit_Foramted V2.html");
string text = driver.Title;
var table = driver.FindElement(By.Id("reportcontainerstyle-Ver2"));
var rowsss = table.FindElements(By.TagName("tr"));
//To get days arrears details
var mainTable = driver.FindElement(By.Name("ConsumerCreditDetails_Version3"));
var subTables = mainTable.FindElements(By.Id("bandstyle-Ver2"));
var rows = driver.FindElements(By.XPath("//table[.//td[normalize-space(.)='Credit Facility (CF) Details']][1]/following-sibling::table[1]//tr[not(#type='table-header')]"));
foreach (IWebElement row in rows)
{
//Some logic here
}
Thread.Sleep(3000);
driver.Close();
}
catch (Exception ex)
{
Logger.LogWriter("WebApplication2.Controllers", ex, "CribController", "Index");
Console.WriteLine(ex);
}
return View();
}
}
Why this not working after publishing. how can I solve this?
I think we need more context about the error it throws you.
There's a similar question the Selenium GitHub Repository and this was the response https://github.com/seleniumhq/selenium/issues/1125#issuecomment-257258747
You can declare the driver like this:
var driverService = ChromeDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;
var options = new ChromeOptions();
options.AddArguments(new List<string> { { "start-maximized" } });
IWebDriver driver;
driver = new ChromeDriver(driverService, options);
Other way that might help is: In the same solution, try to create a Console Application for the Selenium code and executions, calling its constructor from the controller (of the MVC project).

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);

Unable to instantiate Marionette driver with both a FirefoxProfile & FirefoxDriverService

I am currently updating my C# solution with the new Marionette driver for Firefox.
I have managed to get the driver to successfully launch a url with the following code
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(Directory.GetCurrentDirectory(),"wires-0.6.0-win.exe");
service.FirefoxBinaryPath = #"C:\Program Files (x86)\Mozilla Firefox\firefox.exe";
FirefoxDriver driver = new FirefoxDriver(service);
driver.Navigate().GoToUrl("http://www.google.com");
However i need to add a profile to the driver at initialisation in order to set the proxy. I was previously doing this like the following (for older Firefox versions).
private static IWebDriver InitialiseFirefoxDriver(Proxy proxy)
{
FirefoxProfile profile = new FirefoxProfile();
if (proxy != null)
{
profile.SetProxyPreferences(proxy);
}
return new FirefoxDriver(profile);
}
Unfortunately the FirefoxDriver constructor only allows me to pass in either a FirefoxDriverService or a FirefoxProfile. Does anybody know of a way that i can manage to give the driver both sets of config information before creating the driver (or even after)?
Thanks.
This is example code use FirefoxProfile & FirefoxDriverService. I want hide CommandPromptWindow and Mute.
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService();
service.HideCommandPromptWindow = true;
FirefoxProfile _Profile = new FirefoxProfile();
_Profile.SetPreference("media.volume_scale", "0.0");
FirefoxOptions option = new FirefoxOptions();
option.Profile = _Profile;
var driver = new FirefoxDriver(service,option,TimeSpan.FromSeconds(20));
driver.Navigate().GoToUrl("https://www.youtube.com/watch?v=hxiitzCKRek");

Selenium Firefox Profile Path not taken into account

I just want to test locally. With Internet Explorer it works. With Firefox, I get a timeout on line driver.FindElement :
var capabilitiesInternet = new OpenQA.Selenium.Remote.DesiredCapabilities();
capabilitiesInternet.SetCapability("ignoreProtectedModeSettings", true);
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl(url);
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement category = wait.Until<IWebElement>((d) =>
{
return d.FindElement(By.Name("login"));
});
// Login
driver.FindElement(By.Name("login")).SendKeys("test");
Error message is httpRequest to remotedriver timeout.
Upate: I think it's due to the fact that I have a portable version of Firefox 21 and an older version of FF which cannot work with Selenium whereas Selenium launch old version. So I tried to indicate the path of portable :
var capabilitiesInternet = new OpenQA.Selenium.Remote.DesiredCapabilities();
capabilitiesInternet.SetCapability("ignoreProtectedModeSettings", true);
string path = #"C:\Portable";
FirefoxProfile ffprofile = new FirefoxProfile(path);
IWebDriver driver = new FirefoxDriver(ffprofile);
Unfortunately it keeps running the old version (I cannot change the old version because of corporate environment).
Is there anyway to make this profile work ?
Not sure if it is your problem, but in order to 'point' Selenium to where Firefox is located, you are looking for the FirefoxBinary class:
var binary = new FirefoxBinary("pathtofirefox");
string path = #"C:\Portable";
FirefoxProfile ffprofile = new FirefoxProfile(path);
var driver = new FirefoxDriver(binary, profile);

Categories

Resources