selenium chromedriver browser not open after publish - c#

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

Related

How to create google translation using windows application without getting the error The Application Default Credentials are not available.?

I am trying to create a google api to extract text from image and later do a translation to English, my sample code bellow
try
{
var JsonFilePath = #"C:\Users\shanjeeva\source\repos\TranslatorWindowsApp\Translator-fa8e022a7ba1.json";
//Authenticate to the service by using Service Account
var credential = GoogleCredential.FromFile(JsonFilePath).CreateScoped(ImageAnnotatorClient.DefaultScopes);
var channel = new Grpc.Core.Channel(ImageAnnotatorClient.DefaultEndpoint.ToString(), credential.ToChannelCredentials());
var client = ImageAnnotatorClient.Create(); // getting an error here
var image = Image.FromFile(#"D:\Work Temp\Translate.JPG");
// Performs label detection on the image file
var response = client.DetectLabels(image);
foreach (var annotation in response)
{
if (annotation.Description != null)
Console.WriteLine(annotation.Description);
}
}
catch (AnnotateImageException ex)
{
AnnotateImageResponse response = ex.Response;
Console.WriteLine(response.Error);
}
catch (Exception ee)
{
Console.WriteLine(ee.Message);
throw;
}
}
also I have setup google cloud application, generated necessary json file eg- Translator-XXXXX.json
some suggested to setup environment variable with the key "GOOGLE_APPLICATION_CREDENTIALS" also I tried, but am getting bellow error
'The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.'
Can someone please help me?
I was able to resolve with experts help, sample code bellow, added bellow code just before all other code and it works fine
try
{
var GAC = Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS");
if (GAC == null)
{
var path = #"D:\Work Temp\translator-298603-2bb5a20655a2.json";
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", path);
}
}
catch (Exception)
{
}

HttpClient.GetByteArrayAsync(…) “deadlock” when there is no internet connection in .NET standard library calling from UWP

I am developing a UWP application for a document management system. I am trying to open documents from my application. When I click the open document, It is going to download the document and then open in the default application. But the problem is document is not downloaded if the internet is a disconnect in the middle of the process. It means when httpClient is already called. My code is as following
public async Task<DownloadFileDetail> DownloadFileAsync(int dmsFileId)
{
if (dmsFileId <= 0)
{
throw new ArgumentException("Invalid DMS File Id");
}
try
{
return await Task.Run(async () =>
{
DownloadFileDetail fileDetail = new DownloadFileDetail()
{
DocId = dmsFileId
};
string apiUrl = $"files/download/latest/{dmsFileId}";
HttpClient httpClient = new HttpClient();
httpClient.BaseAddress = new Uri(BaseApiUrl);
httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {SessionStore.Instance.AuthToken}");
var response = await httpClient.GetByteArrayAsync(apiUrl); --> gone deadlock
fileDetail.Content = response;
return fileDetail;
});
}
catch (Exception ex)
{
}
return new DownloadFileDetail()
{
DocId = dmsFileId
};
}
Download process called as UWP->.NET Standard Library (holds above code). It will be great if someone helps me to solve the problem.
Thanks
ss
Update:
The above code is working on my laptop and not working on any other laptop in dev environment
when there is no internet connection in .NET standar library calling from UWP
If the deadlock only occurs in no internet connection environment, you could check if internet is available before sending http request. Please check this NetworkHelper.
if (NetworkHelper.Instance.ConnectionInformation.IsInternetAvailable)
{
// sending the request.
}
First, remove the Task.Run(async () => ...) call:
try
{
DownloadFileDetail fileDetail = new DownloadFileDetail()
{
DocId = dmsFileId
};
string apiUrl = $"files/download/latest/{dmsFileId}";
HttpClient httpClient = new HttpClient();
httpClient.BaseAddress = new Uri(BaseApiUrl);
httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {SessionStore.Instance.AuthToken}");
var response = await httpClient.GetByteArrayAsync(apiUrl); --> gone deadlock
fileDetail.Content = response;
return fileDetail;
}

Unable to launch Selenium Web Driver on AWS lambda

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?

Download file from Button link to specific folder on C drive

I am scraping the web page and navigating to correct location, however as being a new to the whole c# world I am stuck with downloading pdf file.
Link is hiding behind this
var reportDownloadButton = driver.FindElementById("company_report_link");
It is something like: www.link.com/key/489498-654gjgh6-6g5h4jh/link.pdf
How to download the file to C:\temp\?
Here is my code:
using System.Linq;
using OpenQA.Selenium.Chrome;
namespace WebDriverTest
{
class Program
{
static void Main(string[] args)
{
var chromeOptions = new ChromeOptions();
chromeOptions.AddArguments("headless");
// Initialize the Chrome Driver // chromeOptions
using (var driver = new ChromeDriver(chromeOptions))
{
// Go to the home page
driver.Navigate().GoToUrl("www.link.com");
driver.Manage().Timeouts().ImplicitWait = System.TimeSpan.FromSeconds(15);
// Get the page elements
var userNameField = driver.FindElementById("loginForm:username");
var userPasswordField = driver.FindElementById("loginForm:password");
var loginButton = driver.FindElementById("loginForm:loginButton");
// Type user name and password
userNameField.SendKeys("username");
userPasswordField.SendKeys("password");
// and click the login button
loginButton.Click();
driver.Navigate().GoToUrl("www.link2.com");
driver.Manage().Timeouts().ImplicitWait = System.TimeSpan.FromSeconds(15);
var reportSearchField = driver.FindElementByClassName("form-control");
reportSearchField.SendKeys("Company");
var reportSearchButton = driver.FindElementById("search_filter_button");
reportSearchButton.Click();
var reportDownloadButton = driver.FindElementById("company_report_link");
reportDownloadButton.Click();
EDIT:
EDIT 2:
I am not the sharpest pen on Stackoverflow community yet. I don't understand how to do it with Selenium. I have done it with
var reportDownloadButton = driver.FindElementById("company_report_link");
var text = reportDownloadButton.GetAttribute("href");
// driver.Manage().Timeouts().ImplicitWait = System.TimeSpan.FromSeconds(15);
WebClient client = new WebClient();
// Save the file to desktop for debugging
var desktop = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop);
string fileName = desktop + "\\myfile.pdf";
client.DownloadFile(text, fileName);
However web page seems to be a little bit tricky. I am getting
System.Net.WebException: 'The remote server returned an error: (401)
Unauthorized.'
Debugger pointing at:
client.DownloadFile(text, fileName);
I think it should really simulate Right click and Save Link As, otherwise this download will not work. Also if I just click on button, it opens PDF in new Chrome tab.
EDIT3:
Should it be like this?
using System.Linq;
using OpenQA.Selenium.Chrome;
namespace WebDriverTest
{
class Program
{
static void Main(string[] args)
{
// declare chrome options with prefs
var options = new ChromeOptionsWithPrefs();
options.AddArguments("headless"); // we add headless here
// declare prefs
options.prefs = new Dictionary<string, object>
{
{ "download.default_directory", downloadFilePath }
};
// declare driver with these options
//driver = new ChromeDriver(options); we don't need this because we already declare driver below.
// Initialize the Chrome Driver // chromeOptions
using (var driver = new ChromeDriver(options))
{
// Go to the home page
driver.Navigate().GoToUrl("www.link.com");
driver.Manage().Timeouts().ImplicitWait = System.TimeSpan.FromSeconds(15);
// Get the page elements
var userNameField = driver.FindElementById("loginForm:username");
var userPasswordField = driver.FindElementById("loginForm:password");
var loginButton = driver.FindElementById("loginForm:loginButton");
// Type user name and password
userNameField.SendKeys("username");
userPasswordField.SendKeys("password");
// and click the login button
loginButton.Click();
driver.Navigate().GoToUrl("www.link.com");
driver.Manage().Timeouts().ImplicitWait = System.TimeSpan.FromSeconds(15);
var reportSearchField = driver.FindElementByClassName("form-control");
reportSearchField.SendKeys("company");
var reportSearchButton = driver.FindElementById("search_filter_button");
reportSearchButton.Click();
driver.Manage().Timeouts().ImplicitWait = System.TimeSpan.FromSeconds(15);
driver.Navigate().GoToUrl("www.link.com");
// click the link to download
var reportDownloadButton = driver.FindElementById("company_report_link");
reportDownloadButton.Click();
// if clicking does not work, get href attribute and call GoToUrl() -- this may trigger download
var href = reportDownloadButton.GetAttribute("href");
driver.Navigate().GoToUrl(href);
}
}
}
}
}
You could try setting the download.default_directory Chrome driver preference:
// declare chrome options with prefs
var options = new ChromeOptionsWithPrefs();
// declare prefs
options.prefs = new Dictionary<string, object>
{
{ "download.default_directory", downloadFilePath }
};
// declare driver with these options
driver = new ChromeDriver(options);
// ... run your code here ...
// click the link to download
var reportDownloadButton = driver.FindElementById("company_report_link");
reportDownloadButton.Click();
// if clicking does not work, get href attribute and call GoToUrl() -- this may trigger download
var href = reportDownloadButton.GetAttribute("href");
driver.Navigate().GoToUrl(href);
If reportDownloadButton is a link that triggers a download, then the file should download to the filePath you have set in download.default_directory.
Neither of these threads are in C#, but they speak of a similar issue:
How to control the download of files with Selenium + Python bindings in Chrome
How to use chrome webdriver in selenium to download files in python?
You can use WebClient.DownloadFile for that.

Rotativa not working when impersonate enabled on server

I am using Rotativa to generate a PDF from a view. It works on my localhost, but when I push to my server it does not work at all. The Server has Windows Authentication and Impersonate enabled which I need to have for this site.
This is the error I get when I try to run the code on the server
Qt: Could not initialize OLE (error 80070005) Error: Failed loading
page
https://api.mydomain.com/Reports/RedBluePDF?community=CommunityName&procedure=GetTasks
(sometimes it will work just to ignore this error with
--load-error-handling ignore) Exit with code 1 due to http error: 1003
Here is my code:
public byte[] getReportsPDF(string community, string procedure)
{
byte[] pdfBytes = new byte[] { };
RouteData route = new RouteData();
route.Values.Add("controller", "SiteSuperReports");
route.Values.Add("action", "RedBluePDF");
this.ControllerContext = new ControllerContext(new HttpContextWrapper(System.Web.HttpContext.Current), route, this);
if (procedure == "GetProductionTasks")
{
var actionPDF = new Rotativa.ActionAsPdf("RedBluePDF", new { community = community, procedure = procedure })
{
PageSize = Size.A4,
PageOrientation = Rotativa.Options.Orientation.Landscape,
PageMargins = { Left = 1, Right = 1 }
};
try
{
pdfBytes = actionPDF.BuildFile(ControllerContext);
}
catch(Exception e)
{
Console.Write(e.Message.ToString());
}
}
return pdfBytes;
}
And here is RedBluePDF Method, this just returns a View:
public ActionResult RedBluePDF(string community, string procedure) {
return View();
}
What am I doing wrong and how come this is not working on my server, but is on my localhost? And How do I get it work on my server.
Try one of this solutions:
1- Go to IIS > Site > Authentication, click on "ASP.NET Impersonation" and DISABLE it.
2- If you're calling a script or a file or whatever, specify the used protocol:
src="//api.mydomain.com/?????
to:
src="http://api.mydomain.com/?????
3- In your Application Pool's configuration, under Process Model, there's an option "Load User Profile". It comes as False by default, set it as true.

Categories

Resources