I'm trying to teach myself C# the program I'm working on has a browser attached to it. I have been trying to figure out how to make it click a button by the tag name but I cant seem to get it to. I have searched this site and found many helpful topics but none yet to help solve this one.
<a class="some_class" style="letter-spacing: -1px" href="/someurl" data-executing="0">displaybuttontext</a>
That is my button I would like to make the program click. But cant get it to. Here are a few ways I have tried:
private void test1_CheckedChanged_1(object sender, EventArgs e)
{
web.Document.GetElementsByTagName("class").GetAttribute("href").invokemember("click");
}
That method didn't it threw up a error with GetAttribute, I then tried:
moco.Document.GetElementById("a").InvokeMember("click");
This doesn't show an error but also doesn't click the button (displaybuttontext). Could someone be kind enough to show a example and explain it. Keep in mind that I'm new to this.
Let's say you have this on the html page:
<a id="link-id" href="linktosomewhere.html">click here</a>
You would want to call:
Document.GetElementById("link-id").InvokeMember("click");
This is because the link element has the attribute id="link-id".
You can see in this article that the parameter you pass to GetElementById needs to be the ID of the element you want to retrieve: http://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument.getelementbyid(v=vs.110).aspx
Related
I need to host a web page inside a WPF application, so I added a WPF WebBrowser control as follows...
<WebBrowser Name="WebBrowser" />
In the C#, I did the following...
WebBrowser.Navigate("http://tableau.visionrt.com/#/signin");
WebBrowser.LoadCompleted += PageLoadedLogin;
...and had the following method, in which I wanted to fill in the two inputs and submit the form...
private void PageLoadedLogin(object sender, NavigationEventArgs e) {
IHTMLDocument3 doc = (IHTMLDocument3)WebBrowser.Document;
HTMLInputElement u = (HTMLInputElement)doc.getElementById("username");
HTMLInputElement p = (HTMLInputElement)doc.getElementById("password");
}
However, both u and p are null, meaning I can't access the elements. I can see the elements if I view the source in a a browser, but I can't access them in code.
I tried the following, just in case I'd got the names wrong...
IHTMLElementCollection inputs = doc.getElementsByTagName("input");
...but that didn't find anything.
Now, looking at the actual HTML that the URL returns, it looks like the <input> and <button> elements are generated in JavaScript, in which case they may not have been created when the LoadCompleted event fires.
So, I tried adding a call to Thread.Sleep in a quick-and-dirty way of making sure the JavaScript had had time to create the elements, but it didn't make any difference.
I tried using the HtmlAgilityPack as suggested in this post, but that gave the same lack of results as above.
Anyone any ideas why I can't access generated elements? All of this works fine on a page with static HTML.
So, I've come across an issue where my favorite radio station plays a song I don't know while I'm driving. They don't have one of those pages that shows a list of songs that they've played; however, they do have a "Now Playing" section on their site that shows what's currently playing and by who. So, I am trying to write a small program that will poll the site ever 2 minutes to retrieve the name of the song and the artist. Using Chrome dev tools, I can see the song title and artist in the source. But when I view the page source, it doesn't show up. They are using a javascript to run display that info. I've tried the following:
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Navigate(#"http://www.thebuzz.com/main.html");
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
}
private void webBrowser1_DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
do
{
// Do nothing while we wait for the page to load
}
while (webBrowser1.ReadyState == WebBrowserReadyState.Loading);
var test = webBrowser1.DocumentText;
textBox1.Text = test.ToString();
}
Essentially, I'm loading it into a WebBrowser and trying to get the source this way. But I'm still not getting the part after the javascript is run. Is there a way to actually retrieve the rendered HTML after the fact?
EDIT
Also, is there a way in the WebBrowser to allow scripts to run? I get popups asking me if I want to allow them to run. I don't want to suppress them, I need them to run.
As Jay Tomten said in the comments, you're trying to fix the result of your problem, not the cause. The cause of the problem is that they're using Javascript to update that part of the page. Instead of working around that by letting the Javascript do its update and then reading what it wrote, ask yourself where the Javascript is getting the info from and whether you can go to the same place. Open up something that lets you see web traffic - Fiddler, or Chrome's dev console, for example. Watch for POST calls. One of them will likely be an AJAX request in which the Javascript on the page is getting the current song. Note the URL, inspect the call to see what parameters it sends and what data it gets back. You can use Postman or something like it to assemble a POST request and work out how the Javascript on that site is getting its data, and then write a little code to make your own call to that URL and parse what comes back.
i am trying to build a timer in my asp.net website.
so, when you click on a button, lets say hes id = "Button1." it will make the button disable(with enable = false command) and after few\4secconds it will enable=true again.
i have tired to look around how to to so, but all guilds online are very old.. and nothing is realyl helpfull.
This is the full code name(just for the exple):
protected void Button1_Click(object sender, EventArgs e)
{
}
Thanks alot! please help me :).
Have a look at the following answer it should be what you are after:
Disable an asp.net dynamic button click event during postback and enable it afterwards
you cant do that in the server side. youll have to do it in client with setTimeout Js function with 4000 ms.
you have to understand that when you see the page - ther server doesnt know the page anymore. and he cant post back by himself.
Okay lets say the page contains a link called 'About Us'. Instead of me doing:
webBrowser.Navigate ("www.page.com/aboutus");
...how can I tell my program to click any link that contains the text About Us?
I know this might seem like Im doing more work then necessary I have too but trust me I need this bit of code.
Any help would be appreciated, thanks :)
You need to find the HtmlElement object for the <a> tag, then call InvokeMember("click").
If the element has an ID, you can get it by calling Document.GetElementById; otherwise, you can look through GetElementsByTagName and find the element you're looking for.
I am currently building a test harness for the company I work at. I have experience both with C# and WatiN and have never encountered the issue I am now having.
Below, is a snippet of the markup for the page giving me the issue:
<div id="toggle1" class="NavLayout toggle">
<span onClick="toggleMenu(1, false);">
<span id="toggletext1">Quote Processing</span>
</span>
</div>
As you can see, I have a div, 2 spans and an image. I am using WatiN to try and click the image, that will then expand the menu, exposing yet another layer that I will need to click something else on. The problem I am having is in getting the 'Click' to happen. From what I can see in the snippet, it seems to me I need to be able to click the event, but cannot 'find' it with the code.
Any help out there to be had?
I have also had issues with clicking on certain elements.
I've run into issues where I could only click on an element if it was highlighted by mousing over the element.
Since I cannot see your code snippet, I can't tell if there is any javascript that deals with mouseover associated with the image, but if there is, you can try the following:
img.FireEvent("onmouseover");
img.FireEvent("onmousedown");
img.FireEvent("onMouseup");
You might also might want to try img.FireEvent("onclick") as well.
These are all guesses, since I can't see your code. It's also possible that rather than clicking on the image element itself, that you may want to try clicking on the parent object.
EDIT:
Ok, now that I can see your code, it appears that you should fire an onClick event against the span with the 'onclick' code in it.
I don't see an image listed in your code snippet, but this code should call the parent of the lowest level span.
Watin.Core.Span span = browserinstance
.Span(Find.By("innertext", "Quote Processing"));
span.Parent.FireEvent("onclick");
The DOM content that you intended to post is not visible. You might want to edit your post and check if it is visible.
In order to click on images
Watin.Core.Image img = browserinstance.Image(Find By Constraint);
if (img!=null and img.Exists)
img.ClickNoWait();
OR
img.FireEvent("onclick");