I'm trying to automate a winforms webbrowser control to fill formulars on external webpages. While testing with the google-page i can't click the searchbutton.
I'm not getting an Error, it just does nothing. I've also checked if the HTMLElement is found.
I think i'm completely blind, so here is my code.
Can u tell me what i'm doing wrong?
currentPage currentPageV;
enum currentPage
{
GoogleSearch,
SearchResults
}
public Form1()
{
InitializeComponent();
webBrowser1.Navigate("www.google.de");
currentPageV = currentPage.GoogleSearch;
}
private void WebBrowser1_DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
if (currentPage.GoogleSearch == currentPageV)
{
currentPageV = currentPage.SearchResults;
webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("q")[0].SetAttribute("value", "Some Text");
webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("btnK")[0].InvokeMember("click");
}
}
Related
Am trying to submit a webform pragmatically on button1 click. In first attemp ie after launching my webbrowser from visual studio it automatically submit the form but again clicking on button1, webpage loads but submit button is not invoked. While debugging its shows that the line of code executed but no action takes place.
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Navigate("xxxx");
webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (webBrowser1.Url.OriginalString.ToString() == "xxxx")
{
if(webBrowser1.ReadyState==WebBrowserReadyState.Complete)
{
HtmlElementCollection doc = webBrowser1.Document.All;
foreach (HtmlElement ele in doc)
{
if (ele.GetAttribute("name").ToString()== "username")
{
ele.SetAttribute("value","xxxx");
}
if (ele.GetAttribute("name").ToString() == "password")
{
ele.SetAttribute("value", "xxxx");
}
if (ele.GetAttribute("classname") == "btn")
{
ele.InvokeMember("click");
}
}
}
}
textBox1.Text = webBrowser1.DocumentText;
}
You only want to setup the DocumentCompleted event for the web browser once. In your code you keep adding to the event chain each time the button is pressed which is not the behaviour you want.
You want something like (pseudo code as haven't got VS with me):
// When the form/parent loads bind the event ONCE here.
public void FormLoads()
{
webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
}
// Just navigate here and the event will still be raised
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Navigate("xxxx");
}
Its Due to your webBrowser1_DocumentCompleted Event is not working when your click on Button.
Just Create one method
Private Void Submit(); and here put your code which you want to perform on click event and call this method from two location one is at button1_click and another webBrowser1_DocumentCompleted so that your code is run at both events.
I would like to use the drop functionality of the WebBrowser control in C#. Unfortunately it doesn't work although I set the AllowWebBrowserDrop property to true.
For testing I wrote this little programm with just a textbox and a webbrowser control:
public Form1()
{
InitializeComponent();
webBrowser1.AllowWebBrowserDrop = true;
textBox1.Text = "http://www.google.com";
}
private void textBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
DoDragDrop(textBox1.Text, DragDropEffects.Link);
}
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
MessageBox.Show(e.Url.AbsoluteUri);
}
The DoDragDrop method gets executed correctly, but I never see the MessageBox appearing when dropping the string from the TextBox over the WebControl. Since the WebControl doesn't offer the usual drag & drop events I'm lost.
What do I have to do to make the url drop to the WebBrowser control work?
Use the following approach to initiate FileDrop dragging:
DataObject dObj = new DataObject();
var paths = new System.Collections.Specialized.StringCollection();
paths.Add(textBox1.Text);
dObj.SetFileDropList(paths);
textBox1.DoDragDrop(dObj, DragDropEffects.Link);
so what I need to do is to use this code:
WebBrowser browser = new WebBrowser();
browser.Navigate("www.somthing.com");
browser.Document.GetElementById("ID").InvokeMember("click");
and then i need to find the id of the button in the page and put in my code but my problem is some of the buttons don't have Id's! what should I do then? they only have type and class and some other things but no Id. i realized that some buttons might be java and maybe that's why i can't click on them by the usual way. so do you know what should I do?
You said your button element has a className ,you can get this element with like this:
public Form1()
{
InitializeComponent();
webBrowser1.Navigate("To your register account url");
}
int on = 0;
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
foreach (HtmlElement btn in webBrowser1.Document.GetElementsByTagName("button"))
{
if (btn.GetAttribute("className") == "yourclassname")
{
btn.InvokeMember("Click");
break;
}
}
}
I have a VSTO Project (WinWord addin) using c#.
The Project has only one window, it must be showed when click on some button, the problem is that it only happens when i'm debuging, if i try to use it after run the installation, it doesn't show the window. Here is my ribbon code:
public partial class MyRibbon
{
private void MyRibbon_Load(object sender, RibbonUIEventArgs e)
{
}
private void btnPublicar_Click(object sender, RibbonControlEventArgs e)
{
MyForm form = new MyForm();
//form.TopLevel = true;
form.ShowDialog();
//form.Show();
}
private void gallery1_Click(object sender, RibbonControlEventArgs e)
{
}
private void editBox1_TextChanged(object sender, RibbonControlEventArgs e)
{
}
}
The commented code are some tries. Any help tks.
After very deep debug, found my addin were throwing an non handeled exception (some XML files needed by addin are lost. They weren't where they should), the extrange thing is that Word doesn't show the problem, simply doesn´t open the form.
I'm trying to make my own webbrowser with C#,
my wpf application seems to be correct. but it's still missing something.
the webpage doesn't appear. :s
Does someone have an idea?
Here's my code in C# :
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
}
private void button1_Click(object sender, RoutedEventArgs e)
{
WebBrowser web = new WebBrowser();
web.NavigateToString (textBox1.Text);
}
Thanks for your help.
As I understand, you are instantiating a new WebBrowser control in code and you aren't adding it as a control to the actual form. You'd better add the control in design view and just do the method call in the code.
When you create the WebBrowser, try adding a third line:
WebBrowser web = new WebBrowser();
Content = web; // extra line
web.NavigateToString (textBox1.Text);
If the textbox is your address bar, it won't work. NavigateToString will interpret what's in your textbox as literal HTML.
web.NavigateToString (textBox1.Text);
should be
web.Source = new Uri(textBox1.Text, UriKind.Absolute);