I am trying to login into a website using a c# program. Right now my program accesses the website and opens the link I want but is redirected to a page where it shows a link to login I go to that and it shows a log in form. Right now I just have this program running in a console application and when I put it into a winform I'd like for the only input to be the username and password.
I have tried several different things using webclient, MSHTML and SHDocVw, and HTTPRequest.
<fieldset class="input">
<p id="com-form-login-username">
<label for="username">Username</label><br />
<input name="username" id="username" type="text" class="inputbox" alt="username" size="18" />
</p>
<p id="com-form-login-password">
<label for="passwd">Password</label><br />
<input type="password" id="passwd" name="passwd" class="inputbox" size="18" alt="password" />
</p>
<p id="com-form-login-remember">
<label for="remember">Remember Me</label>
<input type="checkbox" id="remember" name="remember" class="inputbox" value="yes" alt="Remember Me" />
</p>
<input type="submit" name="Submit" class="button" value="Login" />
</fieldset>
I would like to be able to access the first link and pull all the source code to grab the information that I need.
I think your looking for selenium its a really useful automation library it also can run in headless mode which means that it wont show the google tab where the automation is happening.
you can also get an elements value and many more things
you can see more at : https://www.seleniumhq.org/
Tutorial: https://www.guru99.com/selenium-csharp-tutorial.html
I managed to figure out something that works I couldn't get Selenium to work.
wb.Visible = true;
wb.Navigate(MainSite);
{
while (wb.Busy) { Thread.Sleep(100); }
document = ((HTMLDocument)wb.Document);
if (document.body.outerHTML.Contains("Logout")) { Loggedin = true; }
if (!(document.body.outerHTML.Contains("Logout"))) { Loggedin = false; }
if (Loggedin == false)
{
element = document.getElementById("Username");
username = (HTMLInputElement)element;
username.value = Username;
username = null;
element = document.getElementById("Passwd");
password = (HTMLInputElement)element;
password.value = Password;
password = null;
element = document.getElementById("Submit");
Submit = (HTMLInputElement)element;
Submit.click();
Submit = null;
while (wb.Busy) { Thread.Sleep(100); }
SourceCode = document.body.outerHTML;
}
}
Related
I am new to scrapySharp as well as web scraping. I am trying to scrape a site that is secured and has a login screen. The form element does not have a name/id attribute, thus making my life more complicated. I have been unable to figure out how to load the form using the code below. Any insight is greatly appreciated!
C#:
ScrapingBrowser browser = new ScrapingBrowser();
var homepage = browser.NavigateToPage(new Uri("https://somedomain.com/ProviderLogin.action/"));
var form1 = homepage.Find("form", ScrapySharp.Html.By.Text("form"));
var form2 = homepage.FindFormById("form[action='provider-login']");
HTML:
<form action="provider-login" method="post">
<div class="login-box">
<input type="text" name="username" id="username" autocomplete="false" placeholder="Username"
class="form-control input-lg login-input login-input-username" value="" />
<input type="password" id="password" name="password" placeholder="Password" type="password"
class="form-control input-lg login-input login-input-password" />
<button name="login" type="submit" class="btn btn-primary btn-block btn-md login-btn" >
Login
</button>
</div>
</form>
You can't achieve that using in ScrapySharp using the "By" since it has just four "Element Search Kinds" :
{
Text,
Id,
Name,
Class
}
In your case, you don't have one of them so consider to use "CssSelect" instead to achieve your purpose :
var form = homepage.Html.CssSelect("form[action='provider-login']");
//Or
var form = homepage.Html.CssSelect("form[action*='provider-login']");
You can find the first form node by tag, then use the PageWebForm constructor:
var browser = new ScrapingBrowser();
var homepage = browser.NavigateToPage(new Uri("https://somedomain.com/ProviderLogin.action/"));
var form1node = homepage.Html.SelectSingleNode("//form");
var form1 = new PageWebForm(form1node, browser); // this is where it happens!
form1["username"] = "some username";
form1["password"] = "some password";
form1.Method = HttpVerb.Post;
var webpage = form1.Submit();
I'm new to facebook SDK 6.0.10.0 for C#/Windows Form
I'm truing to find a solution to fill automatically the username and password field from login page in my WebBrowser Component.
Storing the token is not a solution, in my case I test the client application with 5 different account, and I'm spending too much time on this.
This is my HTMLDocument generated from my WebBrowser control: http://pastebin.com/3Xy49vLG
How can I automate the work and not lose much time in testing?
My reply doesn't use Facebook SDK, it just uses a System.Windows.Forms.WebBrowser control with loaded http://facebook.com web page having the following login html controls in its top right corner:
public void LoginToFaceBook(System.Windows.Forms.HtmlDocument doc, string email, string password)
{
//<input name="email" tabindex="1" class="inputtext" id="email" type="text" value="">
doc.GetElementById("email").SetAttribute("value", email);
//<input name="pass" tabindex="2" class="inputtext" id="pass" type="password">
doc.GetElementById("pass").SetAttribute("value", password);
//<input tabindex="4" id="u_0_n" type="submit" value="Log In">
doc.GetElementById("u_0_n").InvokeMember("click");
}
/// <remarks>Should be logged-in to call this method</remarks>
public void LogOutFromFaceBook(System.Windows.Forms.HtmlDocument doc)
{
// <input class="uiLinkButtonInput" type="submit" value="Log Out" />
(from tag in doc.GetElementsByTagName("input").OfType<System.Windows.Forms.HtmlElement>()
where tag.GetAttribute("className") == "uiLinkButtonInput" &&
tag.GetAttribute("type") == "submit" &&
tag.GetAttribute("value") == "Log Out"
select tag).First().InvokeMember("click");
}
I made a school website where teachers will be able to upload the students' marks. Up to now I have these three files:
The first one, will display every students name depending on the subject and course
<table class="check">
<tr class="arrow">
<th>Student</th>
<th>Add Mark</th>
</tr>
#foreach(var users in user){
<tr class="arrow">
<td>#users.Nombre #users.Apellido</td>
<td>
<form method="post">
<input type="text" id="user" style="display: none" name="user" #Validation.For("nombre") value="#users.UserId" />
<input type="text" id="galleryId" style="display: none" name="galleryId" #Validation.For("nombre") value="#galleryId" />
<input type="text" id="note" name="button2" name="note" #Validation.For("nombre") />
<input type="button" value="Ready" title="Ready" onclick="loco(document.getElementById('user').value, document.getElementById('galleryId').value, document.getElementById('note').value)" />
</form>
</td>
</tr>
}
</table>
Here you can see that I used foreach to display every student's name, and then next to it there should be displayed an input textbox for the teacher to write the mark of a specific student. That's why I included the form in the foreach.Next is the ajax file:
function loco(user, gallery, note) {
var xmlhttp;
var user = document.getElementById("user").value;
var galleryid = document.getElementById("galleryId").value;
var note = document.getElementById("note").value;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", "/Marks/add1/" + gallery + "/" + user + "/" + note, true);
xmlhttp.send();
}
And finally, we have the page which will insert the marks to the database without returning any table or div, just uploading the mark.
#{
var db2 = Database.Open("ica");
var user = UrlData[1];
var galleryId = UrlData[0];
var note = UrlData[2].AsInt();
db2.Execute("INSERT INTO Notas (Nota, UserId, Sub_Id) VALUES (#0, #1, #2)", note, user, galleryId);
}
So, why does the ajax send the values of the first student to the upload file and not the second, third, etc? Why, when I click on the submit button of the second student does it sends the mark of the first student again, and only that of the first student?
You're using hard-coded IDs in the HTML inside your for-each loop, so you're going to get multiple elements on your page with the same IDs for user, galleryId and note. That means that your selector can only select the first no matter which you're actually trying to use. You need to do something such as adding an index number to the end of the ID instead of fully hard-coded IDs, so that they can be distinguished from one another.
NOTE:
I observed you using name multiple times in same input please remove that:
<input type="text" id="note" name="button2" name="note" #Validation.For("nombre") />
here you gave name as button2 as well note please remove button2
Make the changes as below:
function loco(form) {
var xmlhttp;
var user = form.name.value;
var gallery = form.galleryId.value;
var note = form.note.value;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", "/Marks/add1/" + gallery + "/" + user + "/" + note, true);
xmlhttp.send();
return false;
}
change your html related code as below:
<form onsubmit="return loco(this)">
<input type="text" id="user" style="display: none" name="user" #Validation.For("nombre") value="#users.UserId" />
<input type="text" id="galleryId" style="display: none" name="galleryId" #Validation.For("nombre") value="#galleryId" />
<input type="text" id="note" name="note" #Validation.For("nombre") />
<input type="submit" value="Ready" title="Ready" />
</form>
I'm creating a form so members of a site can change the passwords for their accounts, and I'd like to display a message explaining mistakes the users made while filling the fields (such as password too short, needs at least a non-alphanumeric character, etc.). I'd like to display these messages in the same page, beside the field names. Here's my code:
#helper RenderForm()
{
<form method="post">
<p>Change your password below</p>
<div><label for="currentPassword">Current Password</label>
<input type="password" id="currentPassword" name="currentPassword"/></div>
<div><label for="newPassword">New Password:</label>
<input type="password" id="newPassword" name="newPassword"/></div>
<div><label for="confirmPassword">Confirm New Password</label>
<input type="password" id="confirmPassword" name="confirmPassword"/></div>
<div><input type="submit" id="submit" name="submit" value="submit"/></div>
</form>
}
#helper Message(string message)
{
<p>#message</p>
}
<style type="text/css">
p,label {color:black;}
</style>
#{
if(!IsPost) {
#RenderForm();
}
else {
var account = Membership.GetUser();
var currentPassword = HttpContext.Current.Request["currentPassword"];
if(Membership.ValidateUser(account.UserName, currentPassword)){
var newPassword = HttpContext.Current.Request["newPassword"];
var confirmPassword = HttpContext.Current.Request["currentPassword"];
if(check(newPassword, confirmPassword)){
account.ChangePassword(account.ResetPassword(), newPassword);
}
}
else {
#Message("The password provided didn't match with the database.");
}
}
}
#functions{
List<string> check(string newPassword, string confirmPassword)
{
//just a place holder
return false;
}
}
I've tried adding a List to be filled when an error was found, and when the form was reloaded the message would be displayed, but the RenderForm() function can't find any reference to the List. How can I display these messages?
You should use the built in validation that comes with the Web Pages framework. I have an article that explains how to use it: http://www.mikesdotnetting.com/Article/191/Validation-In-Razor-Web-Pages-2
I'm trying to allow a web form to use a PayPal buy it now.
The relevant page is here.
Based on which radio button a user selects, depends on which paypal button they are "redirected" to.
The subscriptions are easy - they are just a simple redirect.
The last option, requires a user select a venue.
For this, i require to use the form below:
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="10956105">
<table>
<tr><td><input type="hidden" name="on0" value="Venue">Venue</td></tr><tr><td>
<input type="text" name="os0" maxlength="60"></td></tr>
</table>
<input type="image" src="https://www.paypal.com/en_US/GB/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online.">
<img alt="" border="0" src="https://www.paypal.com/en_GB/i/scr/pixel.gif" width="1" height="1">
</form>
But of course, I want to do it without using that form.
What I have so far is:
if (singleOneOff.Checked)
{
paypalForm.Text = getPaypalForm(venueSelect.SelectedItem.Text);
var strJS = getPayPalPostJS("_xclick");
paypalJs.Text = strJS;
var xxx = "dd";
}
This determines if that particular radio button was ticked.
getPaypalForm
private String getPaypalForm(string venue)
{
StringBuilder strForm = new StringBuilder();
strForm.Append(#"<form action=""https://www.paypal.com/cgi-bin/webscr"" name=""_xclick"" method=""post"">");
strForm.Append(#"<input type=""hidden"" name=""cmd"" value=""_s-xclick"">");
strForm.Append(#"<input type=""hidden"" name=""hosted_button_id"" value=""10956105"">");
strForm.Append(#"<input type=""hidden"" name=""on0"" value=""Venue"">");
strForm.Append(#"<input type=""text"" name=""os0"" value=""{0}"" maxlength=""60"">");
strForm.Append("</form>");
return String.Format(strForm.ToString(), venue);
}
getPayPalPostJS
private String getPayPalPostJS(string strFormId)
{
StringBuilder strScript = new StringBuilder();
strScript.Append("<script language='javascript'>");
strScript.Append("var paypalForm = document.forms.namedItem('{0}');");
strScript.Append("paypalForm.submit();");
strScript.Append("</script>");
return String.Format(strScript.ToString(), strFormId);
}
However, if i select the radio button, and a venue, and press the button, nothing happens....
Any ideas where i've gone wrong?
I followed this guide: http://www.netomatix.com/development/postrequestform.aspx
Here's a much cleaner, server-side solution to the ASP.NET/PayPal single form problem:
http://codersbarn.com/post/2008/03/08/Solution-to-ASPNET-Form-PayPal-Problem.aspx
The root of the problem is that ASP.Net web forms wrap everything on the page inside of one large <form> tag. Your paypal code is rendering a nested form and that doesn't work.
Read this question for more information.