Results 1 to 6 of 6

Thread: Ready to give up :( ! Radio button...

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Posts
    123

    Ready to give up :( ! Radio button...

    Simple question, I hope:

    How do I "click" a radio button from a currently open browser, not WebBrowser1?

    Sample Code:

    Code:
    Dim btnClick As HtmlElement = WebWindow.WebBrowser1.Document.GetElementById("radiogroup0")
                    If btnClick IsNot Nothing Then
                        btnClick.InvokeMember("click")
                    End If
    This code does not work when I load up Chrome (which is my default) and give it focus using the code below, nor with Firefox, IE, or my proxy browser. It only works if I run this code directly through WebBrowser1 in vb.

    Code:
      'Give browser focus
                For Each app As Process In Process.GetProcessesByName("Chrome")
                Dim theHandle As IntPtr = FindWindow(Nothing, app.MainWindowTitle)
                If theHandle <> IntPtr.Zero Then
                    SetForegroundWindow(theHandle)
                End If
            Next
    My guess? I have to somehow declare the currently open browser as WebBrowser1, but I think they won't share the same controls so I don't think that will work. I know it's possible to "click" buttons on other apps with VB, so I'm 100% sure this is possible, but how?

    BACKGROUND: I'm trying to get this working with a proxy browser and can't change the default browser vb uses to it if that's your first thought, sorry. I've spent the last 16+ hours of my free time trying to find an answer to this and am quite literally at my wits end; I've read through two different Visual Basic Step by Step and Teach Yourself Visual Basic in 24-hours, and so many pages off Google, I honestly don't know where to begin, but ironically I didn't find anything even remotely close to the exact issue I'm having, and hence I have no clue how to proceed. A little "hint" would be nice if you don't want to give me the code.

    EDIT: The error code I'm receiving is, "NullReferenceException was unhandled"
    Last edited by Taem; Apr 30th, 2013 at 09:32 PM. Reason: Forgot to give error code

  2. #2
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: Ready to give up :( ! Radio button...

    If you can make this to a asp.net app it would be a better way try to handle browsers.
    Yes it's doable.I was doing something like this back in 2008 i think and had a conversation with JMC, i think. Unfortunately i don't have a clue where is this code (probably on an older PC somewhere) else i wouldn't mind giving some code.I rember that i had to browse down the lower "windows" of the given handler so i could get on the object i was looking for. All these where with API calls back then.I don't now if they changed something in newer VS versions.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Ready to give up :( ! Radio button...

    The only way to 'click' anything in an external application, unless you have an API for that application is to put the cursor over it and create a click. This obviously requires that the relevant window is active and that you know where to position the cursor.

    vb.net Code:
    1. ' Declarations
    2. Private Declare Auto Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As UInt32, ByVal dx As UInt32, ByVal dy As UInt32, ByVal cButtons As UInt32, ByVal dwExtraInfo As IntPtr)
    3.     Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down
    4.     Const MOUSEEVENTF_LEFTUP = &H4 ' left button up

    vb.net Code:
    1. ' Procedure
    2. Cursor.Position = New Point(x,y)
    3.         mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, CType(0, IntPtr))
    4.         mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, CType(0, IntPtr))
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  4. #4
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: Ready to give up :( ! Radio button...

    Pretty sure there was another way as I've said with native win API's but i won't defend that as it's been years and i may be wrong. in any way this is the least favorite to do. I bet there will be another way if the OP explain in details.Why are you trying to click something external.What is the goal you want to achieve? Maybe your goal can be done internally with another way.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Ready to give up :( ! Radio button...

    if the OP explain in details
    One can only dream of the day when people ask questions in a way that gives one the necessary information to answer them. Sigh!

    this is the least favorite to do
    No arguments from me. More often than not, however, it is the only answer. One cannot assume that every HTML element has a 'click' member - very often even those that logic dictates 'must' have, don't. As for this example without any of the HTML before us, who knows (well, actually I think it's a fair bet that it doesn't but ... )?
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  6. #6
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,187

    Re: Ready to give up :( ! Radio button...

    Do you absolutely need to use chrome? I would think you could use mshtml (adding reference to mshtml in the COM tab) with an existing internet explorer window like so (pseudo-code):

    Code:
    dim htmldoc as mshtml.htmldocument
    
    for each IE in browsers 'where IE is the internetexplorer object and browsers is the collection of open ie windows
    
    if IE.url = "www.theWebPagetoPull.com" then 'if the url or indicator matches...
    
    htmldoc = IE.document 'set htmldoc to this page so we can begin to parse it
    
    
    'now set your coding here to check the radio button
    ' could look at http://www.vbforums.com/showthread.php?440657-Radio-Box-Selection-Mshtml-Help
    
    exit for
    
    End If
    
    
    
    next
    Last edited by jayinthe813; May 1st, 2013 at 11:25 PM.

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