Click on the HTML element and create a popup

I am running an Excel VBA macro that collects information from this web page . To get pricing information, you need to click on the field labeled "5 ounces. - 4 columns" in the right of the middle of the page. After you click on the window, a pop-up window will appear with the required information.

My problem is that I could not figure out how to programmatically click on this field.

Studying the page elements, it seems that the next element

<a class="os_opt_dd" href="#"><span class="os_opt_dd_s">5 oz. - 4 Count</span><input tabindex="-1" value="1144"></a>

contains a clickable element that generates a popup, but when I try to programmatically click it (as in the code below) nothing happens.

 my_url="http://www.omahasteaks.com/product/Bacon-Wrapped-Filet-Mignons-4-5-oz-01144?ITMSUF=WZB"

 Set ie = CreateObject("InternetExplorer.Application")     
   With ie    
     .Visible = True
     .navigate my_url
     .Top = 50
     .Left = 530
     .Height = 400
     .Width = 400
  End With

  ie.document.getelementsbytagname("a")(0).Click

, . -, x, , .

For x = 0 To ie.document.all.Length - 1
    ie.document.all.Item(x).Click    ' x=1101, 1103 and 1190, 1223 often seemed to generate the pop-up
Next

- , , ? .

+4
2

click, , - . , -, , , , DOM . , .

, , . , . -.

trigger - - :

jQuery 1.3, trigger() ed DOM

, , : ie.document.foo.Click - . , jquery trigger script IE.

<a> (), , os_opt_dd. 13 ( ), . , (, <div>), .

, jquery, :

// get the HTML element we want to automate
var anchor = $('a.os_opt_dd')[12];
// trigger the mousedown event on this anchor element
$(anchor).trigger('mousedown');

:

$($('a.os_opt_dd')[12]).trigger('mousedown')

, mousedown, click. , (IMO) , . . Google/search Stack Overflow, / //jQuery .., . jquery, , , - ;)

, , :

Option Explicit

Sub TestWithEarlyBinding()

    Dim strUrl As String
    Dim strJquery As String
    Dim objBrowser As InternetExplorer
    Dim objDocument As HTMLDocument

    On Error GoTo CleanUp

    'url and jquery required for automation
    strUrl = "http://www.omahasteaks.com/product/Bacon-Wrapped-Filet-Mignons-4-5-oz-01144?ITMSUF=WZB"
    strJquery = "$($('a.os_opt_dd')[12]).trigger('mousedown')"

    'create IE
    Set objBrowser = New InternetExplorer

    'browse to page and wait for load
    objBrowser.Visible = True
    objBrowser.navigate strUrl
    While objBrowser.readyState <> READYSTATE_COMPLETE 'READYSTATE_COMPLETE =4
        DoEvents
    Wend

    'force jquery event on document
    Set objDocument = objBrowser.document
    objDocument.parentWindow.execScript strJquery, "JavaScript"

CleanUp:
    If Err.Number <> 0 Then
        Debug.Print Err.Number & ": " & Err.Description
    End If
    'objBrowser.Quit
    Set objDocument = Nothing
    Set objBrowser = Nothing

End Sub

. , . :

  • Microsoft HTML
  • Microsoft Internet Controls

Update

jquery trigger , 13- , :

$('#right_sku_form').find('div#selector-PAR-00000009957').find('a.os_opt_dd').trigger('mousedown')

find() DOM.

  • <form> id right_sku_form
  • <div> id of selector-PAR-00000009957
  • <div> , class of a.os_opt_dd

jquery strJquery .

+3

. , ,

ie.document.getelementsbytagname("a")(0).Click

ie.Document.getElementsByName("a")(0).Value = "YourValue"

, , ,

Set tags = ie.Document.GetElementsByTagname("a")

    For Each t In tags            
            t.Click            
    Next
+1

Source: https://habr.com/ru/post/1650420/


All Articles