Results 1 to 5 of 5

Thread: How do I click a javascript button?

  1. #1

    Thread Starter
    Junior Member d.crozier7's Avatar
    Join Date
    Dec 2011
    Location
    United States
    Posts
    28

    How do I click a javascript button?

    I am working on a home project to automate a task so I can improve my time management.

    I am using a web browser control and need to automate the process of clicking a javascript button. I have tried to click the button by it's value but it is not working and I cannot click the button by it's ID also.

    This is the code of the webpage button I need my software to automatically click:
    Code:
    <a href="javascript:void(0);" onclick="viewvideo(7656,'ZYPbX7dNaPw');">Next</a>
    Note that the random looking numbers and letters within the parentheses in the "onclick" part of the code changes every time the page reloads.

    I have already searched in these forums and found a thread regarding this same issue but still not able to get it working.

    I have tried messing with this code I found on another thread, but with no luck:
    Code:
    WebBrowser1.Document.All("Submit").RaiseEvent("onclick")
    I have been researching this issue for months now on and off when I had the time and am coming to a loss now.

    Any help is very greatly appreciated.

    Thanks in advance,
    David.
    I dream in VB...
    Do
    If Sun > 0 Then
    Sun.Visible = False
    Conscious.Enabled = False
    End If
    Loop

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

    Re: How do I click a javascript button?

    As we don't have any idea of what you've tried it's kinda difficult to be too helpful. However as I've been searching unsuccessfully for a browser macro engine that would do this very thing I'm beginning to think that it is impossible by trying to invoke the click. There appears to be some disjunction between the event and the activation of the script which suggests that if it is possible it will be by a considerably more circuitous route!

  3. #3

    Thread Starter
    Junior Member d.crozier7's Avatar
    Join Date
    Dec 2011
    Location
    United States
    Posts
    28

    Re: How do I click a javascript button?

    These are the only two ways I know of using to click a button inside the web browser:
    Code:
    Dim allelements As HtmlElementCollection = WebBrowser1.Document.All
            For Each webpageelement As HtmlElement In allelements
            If webpageelement.GetAttribute("value") = "Next" Then
            webpageelement.InvokeMember("click")
            End If
            Next
    AND

    Code:
    WebBrowser1.Document.GetElementById("NAME").InvokeMember("click")
    Either one will not work.
    I dream in VB...
    Do
    If Sun > 0 Then
    Sun.Visible = False
    Conscious.Enabled = False
    End If
    Loop

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

    Re: How do I click a javascript button?

    Ok. Now I don't want you getting all excited too soon so let me say right away that I haven't tested this on an element with javascript:void() (largely cos I couldn't actually find one!) nor on every type of element but this definitely works on some pages .... and it's so bleeding obvious that I can't believe I never tried it before! Instead of

    webpageelement.InvokeMember("click")

    or

    WebBrowser1.Document.GetElementById("NAME").InvokeMember("click")

    try

    webpageelement.InvokeMember("onclick")

    or

    WebBrowser1.Document.GetElementById("NAME").InvokeMember("onclick")

  5. #5

    Thread Starter
    Junior Member d.crozier7's Avatar
    Join Date
    Dec 2011
    Location
    United States
    Posts
    28

    Re: How do I click a javascript button?

    Dunfiddlin, thank you very much for your time. I will try your two suggestions today and post back again to say my outcome.

    Thanks
    I dream in VB...
    Do
    If Sun > 0 Then
    Sun.Visible = False
    Conscious.Enabled = False
    End If
    Loop

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