This is what I have so far. I am trying to just read the XML from the URL and just get for example temperature, humidity....etc.... But every time I try something else it gives me an error. I want to retrieve the information and put it in a label.
namespace WindowsFormsApplication1 {
public partial class Form1: Form {
public Form1() {
InitializeComponent();
}
private void btnSubmit_Click(object sender, EventArgs e) {
String zip = txtZip.Text;
XmlDocument weatherURL = new XmlDocument();
weatherURL.Load("http://api.wunderground.com/api/"
your_key "/conditions/q/" + zip + ".xml");
foreach(XmlNode nodeselect in weatherURL.SelectNodes("response/current_observation"));
}
}
}
It took me a bit of trial and error but I've got it. In C# make sure you are using - using System.Xml;
Here is the code using wunderground API. In order for this to work make sure you sign up for a key other wise it will not work. Where is say this your_key that is where you put in your key. It should look like something like this. I used a button and three labels to display the information.
namespace wfats2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
XmlDocument doc1 = new XmlDocument();
doc1.Load("http://api.wunderground.com/api/your_key/conditions/q/92135.xml");
XmlElement root = doc1.DocumentElement;
XmlNodeList nodes = root.SelectNodes("/response/current_observation");
foreach (XmlNode node in nodes)
{
string tempf = node["temp_f"].InnerText;
string tempc = node["temp_c"].InnerText;
string feels = node["feelslike_f"].InnerText;
label2.Text = tempf;
label4.Text = tempc;
label6.Text = feels;
}
}
}
}
When you press the button you will get the information displayed in the labels assign. I am still experimenting and you are able to have some sort of refresh every so often instead of pressing the button every time to get an update.
First off yeah you need to give more information in your question but off hand I can see that you have "your_key" inside of your URL. You are probably needing to replace that with your API key for this to work.
Related
Hello everyone am working on a project for work and I just started using C# and selenium. I need to pick a value in this case a name from a list I provide inside a textbox for example jack david john eren kevin they dont need to be a specific number of names and I need them to be inserted into a TextBox from which the first name will be picked and sent to a webpages text field I have tried to use a random function but that did not work i need it to pick in order from first to last Jack->SENDKEYS and then after its done filling up a request it would go to the following David->SENDKEYS and same scenario but its really important to not just send the one after the other but after the completion of the whole webpage i was using a demo google form for this so i could figure out how to do this but i got stuck here this is my code
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
} //names
private void TextBox_TextChanged_1(object sender, TextChangedEventArgs e)
{
}
//size
private void size_TextChanged(object sender, TextChangedEventArgs e)
{
//string sizes = size.Text;
}
//emails
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
string emails = question1.Text;
string fnames = name.Text;
string region = country.Text;
string sizes = size.Text;
ChromeDriver driver = new ChromeDriver(#"C:\webdrivers");
ChromeDriver drv; Thread th;
string url = "https://docs.google.com/forms/d/e/1FAIpQLSeI8_vYyaJgM7SJM4Y9AWfLq-tglWZh6yt7bEXEOJr_L-hV1A/viewform";
HashSet<string> names = new HashSet<string>();
names.Add("name1", names.Add("name2"));
foreach (string name in names) ;
driver.Navigate().GoToUrl(url);
driver.FindElement(By.ClassName("quantumWizTextinputPaperinputInput")).SendKeys(emails);
driver.FindElement(By.ClassName("quantumWizTextinputPapertextareaInput")).SendKeys(fnames);
driver.FindElement(By.ClassName("quantumWizTextinputSimpleinputInput")).SendKeys(region);
driver.FindElement(By.ClassName("quantumWizTextinputSimpleinputInput")).SendKeys(sizes);
}
}
` and it need to be connected to the textbox so that i could just place the textbox name into send keys command thank you!
Not 100% sure what you're after here, but would this help you get started?
var names = new HashSet<string>()
{
"jack",
"david",
"john",
"erin",
"kevin"
};
var namesElement = driver.FindElement(By.ClassName("quantumWizTextinputPapertextareaInput"));
foreach (string name in names)
{
namesElement.SendKeys(name);
}
Am trying to solve this issue I have for work I saw that selenium is quite useful and I wanted to save time and energy doing the same task everyday so I tried making a really basic way to solve the current issue I have with C# and Selenium I need to do the following.
I'm trying to figure out how I could connect a textbox (multiple text input) to then randomly pick one and send it to a webpage. This is my code so far:
namespace emails
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
//textbox input
string quest = question1.Text;
var random = new Random();
var names = new List<string> { question1.Text};
int index = random.Next(names.Count);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
ChromeDriver driver = new ChromeDriver(#"C:\webdrivers");
ChromeDriver drv; Thread th;
string url = "https://accounts.google.com/signin/v2/identifier?continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&service=mail&sacu=1&rip=1&flowName=GlifWebSignIn&flowEntry=ServiceLogin";
driver.Navigate().GoToUrl(url);
driver.FindElement(By.ClassName("whsOnd")).SendKeys(question1.Text); Thread.Sleep(2000);
}
}
}
I tried this but as expected this would just send the whole textbox input.
Assumimg question1 contains several lines and you want to pick 1 line, ie question1 is:
SomeLineWhichIsaName
AnotherLineAnotherName
SomeMoreName
You want to split question1 on linebreaks, which gives you a list of lines (3 in this case, they seem to be names). Than your random function works.
var names = question1.Text.Split("\r\n");
int index = random.Next(names.Count);
string randomName = names[index];
driver.FindElement(By.ClassName("whsOnd")).SendKeys(randomName)
\r\n or \r or \n => just figure out what works
On our job database we have a http address that ends with "projects/Project/Entry/5893" The 5893 is the job number that will change job to job.
I have a timer set to go through each number till it gets this page End Page. So on the End Page HtmlElement does not exist so it gives me the System.NullReferenceException and there for i know the last used job number. But the Problem is that the Exception does not pop up. Does anyone know an easier way to do this. Sorry for not showing the complete webpage address it has sensitive information.
private int a = -1;
private string NJNumber = File.ReadAllText(#"...\CurrentJobNumber.txt");
//The Last Confirmed Number by me and where to start searching from.
private void NewJob_Click(object sender, EventArgs e)
{
HtmlDocument doc = nwJob.Document;
a = Convert.ToInt32(NJNumber);
JobNumberTimer.Start();
}
private void JobNumberTimer_Tick(object sender, EventArgs e)
{
HtmlDocument doc = nwJob.Document;
string aJN = a.ToString();
try
{
nwJob.Navigate("..../projects/Project/Entry/" + aJN);
HtmlElement njname = doc.GetElementById("Name");
a += 1;
}
catch(System.NullReferenceException)
{ lblJobNumber.Text = a.ToString();
JobNumberTimer.Stop();
}
}
I don't think that HtmlDocument.GetElementById will throw a NullReferenceException.
You could try and check the body of the the html doc for something on the error page.
doc.Body.InnerText.Contains("somthing to search for")
I want to get html code from website. In Browser I usually can just click on ‘View Page Source’ in context menu or something similar. But how can I automatized it? I’ve tried it with WebBrowser class but sometimes it doesn’t work. I am not web developer so I don’t really know if my approach at least make sense. I think main problem is that I sometimes get html where not all code was executed. Hence it is uncompleted. I have problem with e.g. this site: http://www.sreality.cz/en/search/for-sale/praha
My code (I’ve tried to make it small but runnable on its own):
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace WebBrowserForm
{
internal static class Program
{
[STAThread]
private static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
for (int i = 0; i < 10; i++)
{
Form1 f = new Form1();
f.ShowDialog();
}
// Now I can check Form1.List and see that some html is final and some is not
}
}
public class Form1 : Form
{
public static List<string> List = new List<string>();
private const string Url = "http://www.sreality.cz/en/search/for-sale/praha";
private System.Windows.Forms.WebBrowser webBrowser1;
public Form1()
{
this.webBrowser1 = new System.Windows.Forms.WebBrowser();
this.SuspendLayout();
this.webBrowser1.Dock = System.Windows.Forms.DockStyle.Fill;
this.webBrowser1.Name = "webBrowser1";
this.webBrowser1.TabIndex = 0;
this.ResumeLayout(false);
Load += new EventHandler(Form1_Load);
this.webBrowser1.ObjectForScripting = new MyScript();
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate(Url);
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
{
// Final html for 99% of web pages, but unfortunately not for all
string tst = webBrowser1.Document.GetElementsByTagName("HTML")[0].OuterHtml;
webBrowser1.DocumentCompleted -= new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
Application.DoEvents();
webBrowser1.Navigate("javascript: window.external.CallServerSideCode();");
Application.DoEvents();
}
}
[ComVisible(true)]
public class MyScript
{
public void CallServerSideCode()
{
HtmlDocument doc = ((Form1)Application.OpenForms[0]).webBrowser1.Document;
string renderedHtml = doc.GetElementsByTagName("HTML")[0].OuterHtml;
// here I sometimes get full html but sometimes the same as in webBrowser1_DocumentCompleted method
List.Add(renderedHtml);
((Form1)Application.OpenForms[0]).Close();
}
}
}
}
I would expect that in ‘webBrowser1_DocumentCompleted’ method I could get final html. It usually works, but with this site it doesn’t. So I’ve tried get html in my own code which should be executed in web site -> method ‘CallServerSideCode’. What is strange that sometimes I get final html (basically the same as if I do it manually via Browser) but sometimes not. I think the problem is caused because my script start before whole web site is rendered instead after. But I am not really sure since this kind of things are far from my comfort zone and I don’t really understand what I am doing. I’m just trying to apply something what I found on the internet.
So, does anyone knows what is wrong with the code? Or even more importantly how to easily get final html from the site?
Any help appreciated.
You should use WebClient class to download HTML page. No display control necessary.
You want method DownloadString
May be it will be helpful if you add calling of your external function to the end of the body and wrap it by Jquery "ondomready" function. I mean something like this:
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
{
// Final html for 99% of web pages, but unfortunately not for all
string tst = webBrowser1.Document.GetElementsByTagName("HTML")[0].OuterHtml;
webBrowser1.DocumentCompleted -= new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
HtmlElement body = webBrowser1.Document.GetElementsByTagName("body")[0];
HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
element.text = "$(function() { window.external.CallServerSideCode(); });";
body.AppendChild(scriptEl);
}
}
[ComVisible(true)]
public class MyScript
{
public void CallServerSideCode()
{
HtmlDocument doc = ((Form1)Application.OpenForms[0]).webBrowser1.Document;
string renderedHtml = doc.GetElementsByTagName("HTML")[0].OuterHtml;
// here I sometimes get full html but sometimes the same as in webBrowser1_DocumentCompleted method
List.Add(renderedHtml);
((Form1)Application.OpenForms[0]).Close();
}
}
I am trying to create a simple note-taking application for my own use. The idea is to learn C#/Mono XML editing.
I can't find a NullReference that the compiler is complaining about. No idea why. I can't see it. After few days of searching I give up... help. :)
Here is the code that is making a bug. The application runs fine until I press a button to Add new note. It just crashes. The add_activated function runs when a button is pressed and it should use AddNote function.
The code is incomplete and it certainly has some logic bugs. But I can handle those. I'm just wondering why it won't run.
MainActivity.cs:
// (...)
protected void add_activated (object sender, System.EventArgs e)
{
Gtk.TextBuffer buffer;
buffer = textview1.Buffer;
Note note = new Note(entry3.Text, buffer.Text);
AddNote (note);
}
public static void AddNote (Note note)
{
string xmlFile = "/home/tomasz/.keeper/keeper.xml";
XmlDocument xmlDoc = new XmlDocument ();
xmlDoc.Load (xmlFile);
XmlNode xmlRoot = xmlDoc.CreateElement("keeper");
if (!xmlDoc.DocumentElement.Name.Equals("keeper") )
{
xmlDoc.AppendChild (xmlRoot);
}
XmlNode xmlNote = xmlDoc.CreateElement ("note");
XmlNode xmlTitle = xmlDoc.CreateElement ("title");
XmlNode xmlText = xmlDoc.CreateElement ("text");
xmlRoot.InsertAfter (xmlRoot.LastChild, xmlNote);
xmlTitle.InnerText = note.Title;
xmlNote.InsertAfter (xmlNote.LastChild, xmlTitle);
xmlText.InnerText = note.Text;
xmlNote.InsertAfter (xmlNote.LastChild, xmlText);
xmlDoc.Save (xmlFile);
}
protected void remove_activated (object sender, System.EventArgs e)
{
throw new System.NotImplementedException ();
}
}
}
Note.cs:
using System;
namespace Keeper
{
public class Note
{
private string title;
private string text;
public string Title {
get
{
return this.title;
}
set
{
this.title = value;
}
}
public string Text
{
get
{
return this.text;
}
set
{
this.text = value;
}
}
public Note (string title, string text)
{
this.Title = title;
this.Text = text;
}
}
}
The immediate problem is that you got the arguments to InserAfter mixed up. First one should be the node to insert and the second one should be the reference node. The root element has no children yet, so LastChild is going to be null and hence the exeption. Using null as a reference point is valid, but not as a node to add.
There are other issues but you said you are able to fix those, so there you go.