I have a c# program which has a web browser my program deals with java pages i want it to click a button in a page but the button has no ID or value all i got about it is this code :
td class="submit">
<br>
<button class="fixedSizeBigButton" type="submit">
</td>
please is there any way to click that button
and for http://watin.sourceforge.net/ i need a code
thanks a lot
Your button has a class name,you can find your button using class name,see this example:
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.DocumentText = "<td class=\"submit\"><br><button class=\"fixedSizeBigButton\" type=\"submit\"></td>";
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
foreach (HtmlElement btn in webBrowser1.Document.GetElementsByTagName("button"))
{
if (btn.GetAttribute("ClassName") == "fixedSizeBigButton")
{
btn.InvokeMember("Click");
}
}
}
I found this link when I went looking around.
More looking around leads me to believe you need to look more into the entire Find class itself. I know there are other ways to locate elements.
I'm trying to locate the documentation that will let me construct some code, since I've never actually used this before :-) I'll update if/when I get something more.
Update
Looks like you aren't the only one having similar issues. You can apparently Chain find methods. WatiN - Find HTML Table Which seems rather useful, if you know more about the button's exact location. Using Find.ByClass along with something else, to try to define the button uniquely.
Related
I am using AvaloniaUI https://avaloniaui.net/docs/
I have researched their docs but it seems I can not find how can I make button which when pressed is forcing you to choose a folder.
Is it even possible and if so how, is there any example ?
I toyed with AvaloniaUI some time ago, got it working under Windows and struggled getting it working under Mac.
Nevertheless, I've seen your other question where you seem to get the dialog opened. Still, for the future:
In your XAML you place a button in a place you please:
<Button Content="Choose folder..." Margin="3" Name="FolderButton" />
Perhaps there is another way of getting it working, the following worked for me:
In your code you need to create a variable that represents your button:
private Button _folderButton;
In your constructor or in your InitializeComponent() method you find the button from XAML and assign it to your variable:
_folderButton = this.FindControl<Button>("FolderButton");
You also assign an event handler for Click event:
_folderButton.Click += FolderButtonClick;
You can immediately add the unsubscribe in your destructor:
_folderButton.Click -= FolderButtonClick;
Now you provide an event handler declaration and implementation:
public void FolderButtonClick(object sender, RoutedEventArgs e)
{
...
}
You may use http://avaloniaui.net/api/Avalonia.Controls/OpenFolderDialog/ - as you've already found out in your other question.
This even handler can be made async if you have any await operations inside.
I hope this helps.
I have a problem with a GetElementById function.
I was looking for tutorial "how to make auto login" aplication and I noticed everyone is getting submission while typing .Get... (screen 1). That's how it look in my aplication (screen 2).
When I try use full function it gets this message (screen 3).
I think there can be problem with missing library or something like that.
Screen 1Screen 2Screen 3
You'll need to add a reference to mshtml:
using mshtml;
Then in your event handler:
private void Button_Click(object sender, RoutedEventArgs e)
{
HTMLDocument document = (HTMLDocument)WebBrowser1.Document;
IHTMLElement element = document.getElementById("email");
}
I'm sorry if this seems incredibly obvious or a very much commonly asked question, but I've been searching and looking over posts for a while now and i still can't seem to get it.
I'm just getting into learning C# and I set myself a little project, making a word processor around a richtextbox control with a few extra features.
I'm currently just adding in the ability to 'Find & Replace' text, and the below code is working when used on the same form as the rich text box control.
richTextBox1.Rtf = richTextBox1.Rtf.Replace("bob", "bill");
I don't want to use a dialog box or something similar, i'm coming direct from our old friend VB6 though, so i'm not sure if they still even exist as such, so i'm making an external form that acts sort of like a dialog box, where i'd like the user to be able to enter the text to look for and replace and then press okay, and be sent back to the main form, sounds simple huh, probably is, i'm not sure what i'm missing...
private void findReplaceToolStripMenuItem_Click(object sender, EventArgs e)
{
Form3 AboutBox = new Form3();
AboutBox.ShowDialog();
}
I've tried my best at implementing a few of the answers I've read over here, in one of them i managed to be able to control form1 but only if i opened a new instance of it with form1.show(); after the code, which is kind of useless in what i'm trying to achieve.
I've set the richTextBox1.Modifiers to Public, but I'm still scratching my head over this one.
Instead of making the RichTextBox public, I'd add a property to the other form that returns the text from that control, like this:
public class SearchForm : Form
{
public string SearchTerm
{
get { return richTextBox1.Text; }
}
...
When the user closes the "search" form, you can get the search term by referencing the property:
private void findReplaceToolStripMenuItem_Click(object sender, EventArgs e)
{
string searchTerm;
using (var searchForm = new SearchForm()) // used 'using' to dispose the form
{
searchForm.ShowDialog();
searchTerm = searchForm.SearchTerm;
}
// do something with searchTerm
}
You'll find this makes maintenance more manageable too. Changing the names of controls in one form shouldn't require you to make changes in any other form that uses them.
I know this has been discussed several times around here but the default behaviour for opening links
clicked in a WebBrowser control does not work for my application.
So while this works as in it opens a link clicked in IE:
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
System.Diagnostics.Process.Start(e.Url.ToString());
e.Cancel = true;
}
I am using a dropdown list to update the html file that the webBrowser is displaying like so:
private void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
webBrowser1.Url = myURI;
}
Now the problem I'm having is that with the _Navigating method above, the webBrowser does not load any subsequent changes to the URL (thanks to the e.cancel I guess) so it only displays the first html file it loads.
If I remove the _Navigating method it updates fine but then the links open up in the same webBrowser control which is what I do not want.
How can I get it to work both ways?
I hope this can help you.
If you want to open a link in a browser, you can add this simple code:
Process.Start("http://google.com");
Remember, there is a lot of information about it. Here in stack Overflow you can take a look in this post: How to open in default browser in C#
If you want to open your link in another browser, you can use this code:
System.Diagnostics.Process.Start("firefox.exe", "http://www.google.com");
Don't forget to visit this post called: How do I open alternative webbrowser (Mozilla or Firefox) and show the specific url?
And Finally, I could recommend you this stack overtflow post called: .NET C#: WebBrowser control Navigate() does not load targeted URL
I hope this information can help you a little bit.
This is an old post but I believe I may understand what the original poster wanted to do. They wanted a page to load in the webbrowser control if the user selected it from the dropdown list but any links in the loaded page should open in the user's web browser. If this is indeed the case, the original poster need a flag on the form to determine the behavior.
The original poster simply needed a flag such as linksOpenInSystemBrowser shown below.
using System;
using System.Windows.Forms;
namespace Browser_Test
{
public partial class myForm : Form
{
private bool linksOpenInSystemBrowser = false;
public myForm()
{
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
linksOpenInSystemBrowser = false;
webBrowser1.Navigate(comboBox1.SelectedItem.ToString());
}
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
if(!linksOpenInSystemBrowser)
{
linksOpenInSystemBrowser = true;
return;
}
System.Diagnostics.Process.Start(e.Url.ToString());
e.Cancel = true;
}
}
}
I have a webbrowser. In the browser's page there is a button. Its value is
Create my account
I want to click this button using only its name. It doesn't have an name so I can't use getElementByID. I tried:
webBrowser.Document.GetElementById("Create my account").InvokeMember("Click");
but that just errored out.
There is no ID for this button
Use the page's DOM to obtain a reference to the element in question. Use development tools found in most browsers to locate where in the DOM tree the button is.
You need the attribute Equals Selector. This should be helpful
You can use chrome in options-tools-programers tools will open in the bottom of browser
then in lower left corner you will see a magnifying glass icon,click it,then you just have to click the desired element that it shows the information.
I worked on something like that and button has id="something"
hope it helps.
You can do it with like this:(see this example)
public Form1()
{
InitializeComponent();
webBrowser1.Navigate("To your register account url");
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
foreach (HtmlElement btn in webBrowser1.Document.GetElementsByTagName("button"))
{
if (btn.InnerText == "Create my account")
{
btn.InvokeMember("Click");
break;
}
}
}