I'm quite new to C# and I'm working on a form that logs into a website, navigates to a specific page and then it should check if that page contains the words "Registered Plus" (This all in a webBrowser) Now I got this all working, except for the last part. I have been thinking and searching for hours about how to make my application check if the current webpage contains "Registered Plus"... This is my code for the button so far:
private void btnReboot_Click(object sender, EventArgs e)
{
webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("username")[0].SetAttribute("value", usernameBox.Text);
webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("password")[0].SetAttribute("value", passwordBox.Text);
webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("submit")[0].InvokeMember("click");
webBrowser1.Navigate("http://website.com/login.php?action=login");
}
Does anyone know how to make it check if this page: http://website.com/login.php?action=login contains "Registered Plus" ? Or maybe a tutorial about how to do something similar to this? Thanks alot in advance. Have been stuck on this part for quite a while now..
UPDATE:
Got a comment telling me about DocumentText.Contains, tried this:
private void btnReboot_Click(object sender, EventArgs e)
{
webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("username")[0].SetAttribute("value", usernameBox.Text);
webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("password")[0].SetAttribute("value", passwordBox.Text);
webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("submit")[0].InvokeMember("click");
webBrowser1.Navigate("http://darkbox.nl/usercp.php?action=usergroups");
if (webBrowser1.DocumentText.Contains("Registered Plus"))
{
label3.Text = "You're plus";
}
else
{
label3.Text = "You're not plus";
}
}
However it still tells me "You're not plus"
Am I doing it right this way? Or..
I am not able to test the code right now, but the big issue is that the call to the webBrowser1.Navigate is executed asynchroniously. Just like when you request it in IE or Chrome, it takes anywhere from a second to a minute for the page to load (or give an error.) On the other hand, your C# code takes barely a millisecond to move from the Navigate request to the next line of code.
You need to fire off your code checking the Document once the Navigate() method returns an event indicating it is done.
private bool shouldEvaluateReponse = false;
private void btnReboot_Click(object sender, EventArgs e)
{
webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("username")[0].SetAttribute("value", usernameBox.Text);
webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("password")[0].SetAttribute("value", passwordBox.Text);
webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("submit")[0].InvokeMember("click");
shouldEvaluateResponse = true;
webBrowser1.Navigate("http://darkbox.nl/usercp.php?action=usergroups");
}
public void WebBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
//ignore this method if the flag isn't set.
if (!shouldEvaluateResponse) return;
//reset the flag so this method doesn't keep executing
shouldEvaluateResponse = false;
if (webBrowser1.DocumentText.Contains("Registered Plus"))
{
label3.Text = "You're plus";
}
else
{
label3.Text = "You're not plus";
}
}
Related
I hope someone will be able to help me with my issue. i want to take a screenshot of the video at a specific time index, however when i try to change the time all i get is a blank black screen. i added buttons which play and pause, it allows me to play and pause the video, if i do that and then change the time index, i get an image. im confused as to why it doesn’t work using code. i even preform btnplay.PeformClick(); to play the video and when i do btnpause.PerformClick() to pause the video it doesn’t.
it seems that i can only get an image of the video if i have to physically hit the play and then pause button on my form, im trying to achieve this using code
private void Form1_Load(object sender, EventArgs e)
{
////////////////////LC4 VLC Settings///////////////////////////////////////////////////////////////////////////////////////////
control = new VlcControl();
var currentAssembly = Assembly.GetEntryAssembly();
var currentDirectory = new FileInfo(currentAssembly.Location).DirectoryName;
var libDirectory = new DirectoryInfo(Path.Combine(currentDirectory, "libvlc", IntPtr.Size == 4 ? "win-x86" : "win-x64"));
control.BeginInit();
control.VlcLibDirectory = libDirectory;
control.Dock = DockStyle.Fill;
control.EndInit();
panel1.Controls.Add(control);
main_form_LC4_data();
}
void main_form_LC4_data()
{
long vOut3 = 20;
playfile("path to file");
First_Frame(vOut3);
}
void playfile(string final)
{
control.SetMedia(new Uri(final).AbsoluteUri);
control.Time = 0;
control.Update();
}
void First_Frame(long vOut3)
{
control.Time = vOut3;
}
private void button9_Click(object sender, EventArgs e)
{
control.Play();
Console.WriteLine("PLAY");
}
private void button8_Click(object sender, EventArgs e)
{
control.Pause();
Console.WriteLine("PAUSE");
}
Above is my code in a nut shell
i have tried things like this
private void button10_Click(object sender, EventArgs e)
{
First_Frame(first_frame); // jump to index
}
and then calling up button10.PerformClick(); however it doesnt seem to work. once again if i physically hit the buttons on my form it works perfectly, however not in the way of coding it.
as an example :
play.PeformClick();
Pause.PeformClick();
time = vOut3;
I do hope this isnt to confusing im really stuck and am still hoping someone can help me
Thank you
A few things:
control.Update();
this does nothing.
You need to wait for the Playing event to be raised after setting the time, otherwise libvlc doesn't have the time to decode the frame and display it (setting the time is asynchronous)
Clarification: I am chilean, so my english is not perfect, sorry for the misspellings.
Hi, I am working with an image in c#.
I try to put an example image when the page open the first time, I used the post back for this, but when I press a button, execute the code in the post back section (which is right), after that it execute the Button code, but then, it pass again for the Page_Load method, and execute the "not post back" section, and i dont know why.
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
//Is post back
}
else // Is not post back
{
//Make things only when the page is open for the first time
}
}
I usually only use (!IsPostBack) on PageLoad for doing initial data loads or validations(like settings for users).
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (userIsAdmin)
{
button1.Enabled = true;
}
}
}
You could refer to the link for the explanation for PostBack https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.page.ispostback?view=netframework-4.8
I am trying to update a label on my windows form with statistics from another methods execution that scrapes a webpage and creates a zip file of the links and creates a sitemap page. Preferably this button would run the scraping operations and report the statistics properly. Currently the scraping process is working fine but the label I want to update with statistics data is not changing on the button click. Here is what my code looks like now:
protected void btn_click(object sender, EventArgs e)
{
//Run scrape work
scrape_work(sender, e);
//Run statistics work
statistics(sender, e);
}
protected void scrape_work(object sender, EventArgs e)
{
//Scraping work (works fine)
}
protected void statistics(object sender, EventArgs e)
{
int count = 0;
if (scriptBox.Text != null)
{
count += 1;
}
var extra = eventsBox.Text;
var extraArray = extra.Split('\n');
foreach (var item in extraArray)
{
count += 1;
}
//scrapeNumLbl is label I want to display text on
scrapeNumLbl.Text = count.ToString();
}
Would I have to use threading for this process or is there some other way I can get this process to work? I have already tried this solution but was having the same issue where the code runs but the label does not update. Any help would be greatly appreciated, this minor thing has been bugging me for hours now.
I eventually solved this by writing the path to the zip file to a label button on the form rather than sending it right to download on the client's browser on button click. The issue was that the request was ending after the zip file was sent for download. To ensure that both methods ran at the proper time I moved the call to the scrape_work method to inside of of statistics
In order for the path to be clickable and the file to download properly I had to make the "label" in the form a LinkButton in the .aspx page
<asp:LinkButton ID="lblDownload" runat="server" CssClass="xclass" OnClick="lblDownload_Click"></asp:LinkButton>
And made the lblDownload_Click run like the following:
Response.Clear();
Response.BufferOutput = false;
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "attachment; filename=zipFiles.zip");
string folder = Server.MapPath("~/zip");
string endPath = folder + "/zipFiles.zip";
Response.TransmitFile(endPath);
Response.End();
Running it this way the page reloads with the new labels properly written and the zip file available to download as well.
ASSUMING THIS CODE IS RUNNING SYNCHRONOUSLY (you aren't threading the scraping in a call I don't see), Are you sure you are reaching the code that sets the label text? I simplified your code as below (removing the iteration over the array and just setting the label text to a stringified integer) and am not having any trouble changing the text of a label.
namespace SimpleTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
scrape_work(sender, e);
statistics(sender, e);
}
protected void scrape_work(object sender, EventArgs e)
{
//Scraping work (works fine)
}
protected void statistics(object sender, EventArgs e)
{
int count = 666;
scrapeNumLbl.Text = count.ToString();
}
}
}
Result:
I know, i know there are lots and lots of questions asking on here about this error, each with their own response, but its easier to work off a response regarding your own code rather than someone else's
I have been working on this program for some time for a college assignment, and as soon as i started putting in the class to calculate the totals of things it now crashes,
I don't know where to look so i'll post my main code
enter code here
namespace Till
public partial class MainWindow : Window
{
Calculator calc = new Calculator();
public MainWindow()
{
InitializeComponent();
}
public bool User;
public bool tillopen = false;
private void button_Click(object sender, RoutedEventArgs e)
{
//button clone thingy
Button btn = (Button)sender;
label.Content = label.Content + btn.Content.ToString();
Console.Beep(); // makes the buttons beep
}
private void clear_Click(object sender, RoutedEventArgs e)
{
// Clear
label.Content = "";
}
private void Button_Submit_Click(object sender, RoutedEventArgs e)
{
// submit
listView.Items.Add(label.Content);
label.Content = "";
calc.setSoldItems(Convert.ToInt32(label.Content)); /// it breaks on this line///
}
private void button13_Click(object sender, RoutedEventArgs e)
{
//void sale
label.Content = "";
listView.Items.Clear();
}
private void button15_Click(object sender, RoutedEventArgs e)
{
//pound
label.Content = "1.00";
}
private void button12_Click(object sender, RoutedEventArgs e)
{
//till open close
tillopen = true;
}
private void button16_Click(object sender, RoutedEventArgs e)
{
Login m = new Login();
m.Show();
this.Close();
}
private void button14_Click(object sender, RoutedEventArgs e)
{
label.Content = "2.00"; // 2 pound
}
private void button17_Click(object sender, RoutedEventArgs e)
{
label.Content = calc.finish();
}
}
I have tried to re-create the error in another WPF (converting a to an int32) and it works fine, i know this is an issue with my code itself, i have tried using other machine and using different versions of visual studio itself, so we came to the assumption its this code itself and not a broken dll file
So before I sit down with my Teacher and spend all day going though my code step by step im asking around for help in order to save both of our time, This assignment is due in in 3 weeks, and it decides to break on me now.
thankies
To replicate this error, i press a number button on my Windows form, them hit the submit button i created (which starts the conversion) If a copy of my class which handles all of this is needed im happy to post it
In the method button_click, you have assigned value as
label.Content = label.Content + btn.Content.ToString();
which is a string value to the label and the values are concatenated instead of add.
and when you are reading it, you are converting it in Int32. which will give exception as it will not contain any integer value to it.
You can add the value like this:
label.Content = (Convert.ToInt32(label.Content) + Convert.ToInt32(btn.Content)).ToString();
and check before converting if the label has blank values in it, if it has do not convert them, and only convert the value if it has some values it will not give any error. Also do not assign any values other that numerical digits.
You are calculating:
Convert.ToInt32(label.Content)
but on the line before you set:
label.Content = "";
so this means you are calculating
Convert.ToInt32("")
which gives you a FormatException.
Perhaps you should use the value of label.Content before you overwrite it?
I've to send mail using webappplication.All works fine but i've to show a confirm message if attachments are not selected.In that confirm box when ok is clicked execution of the code should continue,when cancel button is clicked it should return.
This is my code
Code behind
protected void btnSend_Click(object sender, EventArgs e)
{
string str = "Are you sure, you want to proceed without attachment?";
this.ClientScript.RegisterStartupScript(typeof(Page), "Popup", "ConfirmApproval('" + str + "');", true);
...Send mail code goes here
}
ASPX
function ConfirmApproval(objMsg)
{
if(confirm(objMsg))
{
alert("execute code.");
return true;
}
else
return false;
}
It works fine, ie when ok button is clicked an alert "execute code" is displaying.Instead of displaying alert i want to continue execution of the code.How will we do that from client side ???
try this code,
protected void Page_Load(object sender, EventArgs e)
{
btnSend.Attributes.Add("onclick","return confirm('execute code');");
}
You need to use confirm("execute code") instead of 'alert'. Hope that helps :)
var agreed = confirm("execute code");
if(agreed) {
//do something
} else {
return false;
}
I am showing you one brute force way to do that.
put that code behind in the page_load(sender, e) of any web page you create. Now call it through XHR.
(this may be a bad reply).