I am new in C# language and I have been trying to automate a website using .NET based webbrowser for ONLY personal use in Visual Studio 2015.
I have done document parsing, used Timer, used DocumentCompleted event properly to wait for the webpage to load completely and then parse the content, tried to make async events to behave like sync events (in order to load HTML content generated by clicking a link in a fully loaded webpage), etc to go through the phases in webpage automation: login -> get trains between stations -> click the Book now link -> go to the next page and fill in the passenger details.
Everything works fine but I am now stuck at the last phase, i.e., "go to the next page and fill in the passenger details" has a captcha image that must be resolved to go to the payment page. Don't get me wrong because I am not trying to get this captcha resolved automatically. The problem here is that I do not see the captch image which turned to be loaded only when this javascript call is invoked $(document).ready.
I thought my project has some buggy code which is stopping to load the captcha and therefore, I created a very basic new project, only added below code and navigated through different phases myself to see if the captcha really loads but unfortunately it would not load.
namespace TestWebBrowser
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
webBrowser1.Navigate("https://www.irctc.co.in/eticketing/loginHome.jsf");
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
}
}
}
Please see below. The highlighted part is where I am expecting a captcha.
I must tell you that I am not a web designer and therefore I only understand very basic of how websites work.
I went through several questions on this forum and nothing helped me.
Internet explorer is also using .NET browser from behind but while using IE, I can see the captcha is getting loaded. So, why is this javascript call $(document).ready is not getting invoked in .NET browser. Please see below:
I have later tried to use CefSharp in a fresh new project and I can see the captcha is getting loaded in its chromium based webbrowser. But I have done so much coding with .NET based webbrowser already and therefore I want to stick to the latter at this moment in order to get this resolved.
Is this happening because .NET webbrowser is using some very old IE version configurations?
Please help me to understand.
UPDATE 1: Adding the javascript
<script type="text/javascript">
$(document).ready(function(){
var isJsBlocked=0;
if (typeof(nlpLoadCaptchaAsync) == 'function'){
nlpLoadCaptchaAsync();
}else{
isJsBlocked=1;
}
setTimeout(function(){
var isNLPCaptcha = document.getElementById('nlpIdentifier');
if(isNLPCaptcha == null || isNLPCaptcha=='' ) {
var nlptrack = new Image();
nlptrack.src="http://irctclive.nlpcaptcha.in/temp_redirect_count/irctc_timeout.php?ref=f2c5d744485b0b4251461454db791111&isJsBlocked="+isJsBlocked+"&dynamicParameter="+Date.now();
nlpCaptchaTimeOut(true);
}
}, 5000 );
});
</script>
The answer shared here: Use latest version of Internet Explorer in the webbrowser control solved my issue.
I basically had to change the version of IE version used by my webbrowser control.
Thanks to Matthias herrmann
Related
I made a simple html testpage. If the page is opened (http://localhost/cut/public/updateFile) then I send a timestamp to my webserver, where the value is written into a file date.txt e.g.:
4/8/2017 # 14:50:19
I also wrote a C# Windows Forms App which makes a request to the webserver to get the value of this file (date.txt), every X seconds.
My goal is to show the current time in a notification box every X seconds by reading it from the file on the server (for practice only)
However, before I get the file content, I need to update it first of course to get the current date and time.
Is it possible to solve this with the rules defined above?
This is my attempt:
private void timerA_Tick(object sender, EventArgs e)
{
sendWebRequest("http://localhost/cut/public/updateFile");
timerA.Stop();
timerA2.Interval = 5000;
timerA2.Start();
}
private void timerA2_tick(object sender, EventArgs e)
{
//Returns the content of date.txt
string response = sendWebRequest("http://localhost/cut/public/fileApi?action=read&targetFile=date");
//Show Notification
notifyIcon1.Visible = true;
notifyIcon1.Icon = SystemIcons.Exclamation;
notifyIcon1.BalloonTipTitle = "File content";
notifyIcon1.BalloonTipText = response;
notifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
notifyIcon1.ShowBalloonTip(10000);
timerA2.Stop();
timerA.Start();
}
/**
* Send request and get response as string
*/
public static string sendWebRequest(string URL)
{
WebRequest request = WebRequest.Create(URL);
WebResponse response = request.GetResponse();
StreamReader streamReader = new StreamReader(response.GetResponseStream());
return (string)streamReader.ReadToEnd();
}
However I always get 4/8/2017 # 14:50:19 it does not update as It should.
It obviously does not work this way, since WebRequest.Create does only gets the html file as it is and delivers it back to my C# Application, but it does not execute the javascript where I make the request to the server.
I only created this example above to ask if it is somehow possible to achive this at this way or if C# is not designed to solve problems like this?
My only idea is to create a hidden webbrowser in my Form1 and open http://localhost/cut/public/updateFile to start the update but I am not sure if this even works.
I created a webbrowser element and call the update URL like this:
webBrowser1.Navigate("http://localhost/cut/public/updateFile");
However, there are plenty of script error messages, which are found in the jquery file.
And my script won't work either because of the not working jquery.
So I guess it would work, but not with jquery, or I have to fix all errors in the jQuery file.
How to solve this problem?
You should open http://localhost/cut/public/updateFile from a real browser, which will execute the javascript on the page. Requesting this page from a windows form application with a WebRequest will just return the contents of the page, but will not process or execute the javascript on the page because it is not rendered or processed.
I solved it by adding a webbrowser element to my Form, and calling the URL inside of it like this:
webBrowser1.Navigate("http://localhost/cut/public/updateFile");
However, I had to rewrote my javascript to make it work without jQuery, since the C# Webbrowser appears to be an extremly old Internet Explorer with no support for nothing. There were plenty alerts pointing to script errors like the one below:
Now it works as expected. I hope someday somebody will come across with a much better solution than this though.
UPDATE
I was able to change the browser to the latest available by adding this line to my html head section:
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<!-- IE=edge instructs the WebBrowser control to use the latest version of IE it supports -->
I am trying to use Dialog API of Office addin.
I can successfully open a Dialog box from my task pane by:
$scope.openDialog = function () {
Office.context.ui.displayDialogAsync('https://localhost:3000/home',
function (asyncResult) {
dialog = asyncResult.value;
dialog.addEventHandler(Office.EventType.DialogMessageReceived, processMessage);
});
}
My Dialog box is a mean-stack site. I have added <script src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js"></script> in the index.html. And I tried to use Office.context.ui.messageParent(true);, it shows an error in console:
And I see in the doc that I don't understand quite well:
The Office JavaScript library is loaded in the page. (Like any page
that uses the Office JavaScript library, script for the page must
assign a method to the Office.initialize property, although it can be
an empty method. For details, see Initializing your add-in.)
I also tried to add Office.initialize = function () { }; in index.html, the error is still there, and processMessage of the task pane does not seem to receive anything.
So is there anything special I should do to my mean-stack site so that it could use messagePerent?
The console error will not introduce any bad effect to the dialog. We already fixed it internal. You can just ignore this error. Did you check whether office.context.ui.messageParent is null or undefined ? if it is not, then the dialog has been initialized successfully. Then it will be only something wrong with the postMessage method, what system and browser version are you using ?
1. If it is Win10 and latest version of IE, please make sure the dialog first page domain is same with the taskpane domain. Or you can use other browser to try it.
2. If it is Win7&8&8.1 and IE, then you can just try in other browser to see whether the messageParent api is work. We have already done a code change to fix the IE issue. It will be deployed to prod soon.
So, I have a project where I need to get the photos from a profile.
I am able to navigate to the photos page of a profile, but I believe the JavaScript is not loading.
I am currently using HtmlUnit but if you know of another Java API that would help I'm all ears.
Basically, when I view Facebook in a normal browser, it will load all of the pages and I can inspect the elements.
When inspecting, there is a div called fbStarGrid and a few other modifiers. This div contains all the images for a user's profile.
When I use HTMLUnit, I cannot find the div. I had it print the full page XML to a file, and I found that the div is commented out. I believe this means the Javascript never ran to load the content.
After browsing a lot of javascript help on SO, I have found a few things that help with debugging but can't seem to fix the problem.
The first thing I've done is create an instance of a JavaScriptJobManager. I used it to see how much JavaScript is not complete. After waiting for a while (10+ seconds) it says there are still 3 JS jobs uncomplete. After a very long time (about 60 seconds), it says there are 2 JS jobs uncomplete.
I do not know what is hanging with those JS jobs.
I get a warning upon page load about application/ld+json not running but I do not believe that part of the website is related to the photos.
Is there something I can do to force the JS to run? Is there a job it's stuck on and won't proceed to the next job?
I've also wondered if it's an issue with the page not re-syncing.
I've tried two solutions related to this:
Setting the AjaxController to NicelyResynchronizingAjaxController()
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
And someone suggested creating a custom controller that forces syncing.
webClient.setAjaxController(new AjaxController(){
#Override
public boolean processSynchron(HtmlPage page, WebRequest request, boolean async)
{
return true;
}
});
Neither of these seemed to effect the page.
If HTMLUnit is not the right library for the job, any other ideas? I need this to be headless/guiless to run on a linux server. Java is preferred, but I can switch languages if necessary.
I am really a fresh guy for asp.net. I am using Visual Studio 2012.
I'm creating a login page, were there are two buttons Login and Exit.
When i click the Exit button then application have to be close and also stop the debugging.
My try: referred
I know there are other solutions on the given link for this problem but i prefer the following approach.
protected void Page_Load(object sender, EventArgs e)
{
ClientScript.RegisterOnSubmitStatement(typeof(Page), "closePage", "window.onunload = CloseWindow();");
}
and written the following javascript function within the Login.aspx.
<script language="javascript" type="text/javascript">
function CloseWindow() {
window.close();
}
</script>
Note: The above script works fine if i click on exit button but application also get close when i click Login button and also debugging is not getting stop.
Your page is doing exactly what you asked it to do.
In your Page_Load, you called:
ClientScript.RegisterOnSubmitStatement()
That method causes every form submission to execute the script you provided; that means all of your buttons will run your CloseWindow procedure when they are clicked.
If you only want one of your buttons to close the window, then you should only attach the CloseWindow method to one of them. The answer you selected from the linked question only works because there's only one button on the form. I'd recommend you go with one of the other answers, e.g. using:
OnClientClick="javascript:window.close();"
as an attribute on your Exit button.
Handle the two buttons separately. Do whatever you want with the login button to submit the page and handle the postback but don't tie any events to the CloseWindow() function. Then, simply create and handle the Exit button like:
<input type="button" onclick="CloseWindow();" value="Exit"/>
The easy answer for your note is to use Internet Explorer as the default launch browser in the debug toolbar.
Unlike a winforms application, an ASP.Net application is stateless. The code that runs in the browser is not dependent on the same resources as the code that is running in the Visual Studio debugger. The only connection between them are the requests that the browser makes to the server (VS 2012 debugger either treats itself as a server or uses IISExpress) and the responses that the server sends back as part of those requests.
In most cases, this means that, when you close the browser, the server keeps on going, waiting for more requests. Internet Explorer works a little differently than the other browsers with Visual Studio. When the IE instance that Visual Studio launches gets closed, the debugger process also closes.
If you really are just starting out the ASP.Net, you should try the ASP.Net MVC framework. It has a cleaner separation between server and client side code, which may help you avoid some of these types of issues.
http://swfupload.org/node/7/release
The .Net 2.0 sample is not working, has anyone faced the same thing and have been able to make it work?
The solution is compiling and the default page is opening normal in FireFox but when I click on the button nothing happen, and in IE the button is not appearing at all
A few things note with the SWFUploader is that although there is a .Net test project it is a little miss leading and does a lot more than is really required, its great if you want to upload images but dosn't have the progress bar or allow file uploads.
Make sure you get the source code form http://code.google.com/p/swfupload/ not the site, this will fix any issues you have with version 10 / 11 of flash not working.
To get any of the PHP demo working with .Net just make a file called upload.aspx to replace the upload.php with the following code in.
try
{
// Get the data
HttpPostedFile postedfile = Request.Files["Filedata"];
postedfile.SaveAs(Server.MapPath(".\\") + postedfile.FileName);
}
catch
{
// If any kind of error occurs return a 500 Internal Server error
Response.StatusCode = 500;
Response.Write("An error occured");
Response.End();
}
finally
{
Response.End();
}
Change the the default page name to html or .aspx and change the call inside to your new save script.
It will now save the files in the root of the website.
I was having this issue but what fixed it for me was getting the newest version of the swfupload controls. I didnt realize that the stuff you download off google source had the .net stuff in it as well, i was using the out of date controls off the .net page on their website.
if you go to http://code.google.com/p/swfupload/ get the "SWFUpload [version number] Samples.zip" and it has .net stuff in it, which i was able to get working just fine.
i got it, because my Flash version is 10 and SWF is not working with Flash player 10 as its not allowed in Flash player 10 to call a swf file, so the solution for that i putting the object its self on the page and make it as a button to make the user click on the object its self.