CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Oct 2013
    Posts
    11

    Exclamation WPF with WatiN plug in. need to open up IE and go to google

    I need to make a WPF application in visual studio 2012 for desktop. Has to use the WatiN plug in. And I have to have a text box and button (which I have), That when you type in the text box and click the button the app will launch IE and go to google and search for whatever you typed into the text box and automatically select a link from the list of search results and go to that page. Any suggestions on how to do this? thank you!!!!!! I need answers ASAP.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: WPF with WatiN plug in. need to open up IE and go to google

    Based on the example from http://watin.org/ it seems pretty straightforward.

    Add the NuGet WatiN package to your app and put in the sample code.

    I got the following to work with a bit of effort:
    Code:
    using (var browser = new IE("http://www.google.com"))
    {
      browser.TextField(Find.ByName("q")).TypeText(_textBox.Text);
      browser.Button(Find.ByName("btnG")).Click();
    
      browser.WaitForComplete(5000);
    
      System.Windows.Forms.SendKeys.SendWait("{ENTER}"); // presses search on the second screen
    
      //browser.Button(Find.ById("gbqfb")/*.ByName("btnG")*/).Click();  // doesn't work
    
    }

    What have you tried? What doesn't work?

  3. #3
    Join Date
    Oct 2013
    Posts
    11

    Question Re: WPF with WatiN plug in. need to open up IE and go to google

    I have not tried to much. I am new to WPF and am trying to teach myself how to do it. So i need someone to point me in the right direction or a walkthrough would be nice

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: WPF with WatiN plug in. need to open up IE and go to google

    Quote Originally Posted by Pope43 View Post
    I have not tried to much. I am new to WPF and am trying to teach myself how to do it. So i need someone to point me in the right direction or a walkthrough would be nice
    I just pointed you in the right direction, didn't?

  5. #5
    Join Date
    Oct 2013
    Posts
    11

    Re: WPF with WatiN plug in. need to open up IE and go to google

    Well I would just like to know if i need to make any classes at all? or just put the functionality in my mainwindow.xaml.cs

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: WPF with WatiN plug in. need to open up IE and go to google

    Quote Originally Posted by Pope43 View Post
    Well I would just like to know if i need to make any classes at all? or just put the functionality in my mainwindow.xaml.cs
    No, the code that I posted earlier goes in the search button handler. That code uses a TextBox with a name of _textBox You'll need to use the xaml editor and drag a textbox from the toolbox to the designer surface. Then add Name="_textBox" to the generated xaml. Next drag a button onto the designer surface and double-click on it to generate a button handler. Switch to the code behind page and add the code from my snippet into the button handler.

  7. #7
    Join Date
    Oct 2013
    Posts
    11

    Re: WPF with WatiN plug in. need to open up IE and go to google

    thank you I highly appreciate it

  8. #8
    Join Date
    Oct 2013
    Posts
    11

    Re: WPF with WatiN plug in. need to open up IE and go to google

    I wrote the code out and did what you said but the name Find "does not exist in the current context"

    Error 1 The type or namespace name 'IE' could not be found (are you missing a using directive or an assembly reference?) c:\users\user\documents\visual studio 2012\Projects\Search\Search\MainWindow.xaml.cs 30 38 Search

    Error 4 The type or namespace name 'Forms' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?) c:\users\user\documents\visual studio 2012\Projects\Search\Search\MainWindow.xaml.cs 37 32 Search

  9. #9
    Join Date
    Oct 2013
    Posts
    11

    Re: WPF with WatiN plug in. need to open up IE and go to google

    An unhandled exception of type 'System.IO.FileNotFoundException' occurred in WatiN.Core.dll

    is the new error i get. I fixed the rest.

  10. #10
    Join Date
    Oct 2013
    Posts
    11

    Re: WPF with WatiN plug in. need to open up IE and go to google

    It opens up IE and goes to google and types in what was in the text box into the search box but than the app closes. It doesnt search or select a link at random.

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: WPF with WatiN plug in. need to open up IE and go to google

    Quote Originally Posted by Pope43 View Post
    It opens up IE and goes to google and types in what was in the text box into the search box but than the app closes. It doesnt search or select a link at random.
    Hate to ask the obvious, but did you write code to select links at random? Now, I wouldn't have to ask this if you posted the code you have - generally speaking, it's a good idea to post code when asking questions in a programming forum as folks trying to help can see exactly what you are working with.

    At any rate, given the nature of how the WatiN automation engine works, there might be differences in how various browser versions emit tags which could effect the reliability of the automation. I suggest you contact the folks at http://watin.org/ for more specific information on using their tool.

  12. #12
    Join Date
    Oct 2013
    Posts
    11

    Re: WPF with WatiN plug in. need to open up IE and go to google

    this is the code i have. And no I have not written any code to select a link at random and i have no idea how to go about doing that either. I assume it will be in javascript. Can you please tell me the code that I will need to make my application do that?



    using WatiN.Core;

    namespace Search
    {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
    public MainWindow()
    {
    InitializeComponent();
    }

    private void Btn_Click(object sender, RoutedEventArgs e)
    {
    using (var browser = new IE("http://www.google.com"))
    {
    browser.TextField(Find.ByName("q")).TypeText(_textBox.Text);
    browser.Button(Find.ByName("btnG")).Click();

    browser.WaitForComplete(5000);

    System.Windows.Forms.SendKeys.SendWait("{Enter}"); // presses search on the second screen

    browser.Button(Find.ById("gbqfb")/*.ByName("btnG")*/).Click(); // doesn't work



    }
    }
    }
    }

  13. #13
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: WPF with WatiN plug in. need to open up IE and go to google

    Why would the code be in javascript when the rest of the code is in c#?

    As far as solving the rest of the problem. Look on the WatiN sit for info how to capture a list of links. Then look for the Random class in msdn for how to use the random class. The put the two together.

  14. #14
    Join Date
    Oct 2013
    Posts
    11

    Re: WPF with WatiN plug in. need to open up IE and go to google

    I dont know. I have a very basic understanding of WPF. abd ok thank you if you could provide links that would be great. I am trying to keep all my code on the same page

  15. #15
    Join Date
    Oct 2013
    Posts
    11

    Re: WPF with WatiN plug in. need to open up IE and go to google

    could you explain on how to go about doing it? earlier you said that i would not need a class. so now i am a little bit confused

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured