0

Hello dear VBA collegues :)

Sub login()
'test 
    Const URL$ = "https://kwm.kromi.de/cgi-bin/kwm?HTML=frontend/login.htm"
    Dim UserName As String, Password As String, LoginData As Worksheet
    Set LoginData = ThisWorkbook.Worksheets("Sheet1")
    UserName = LoginData.Cells(1, "B").Value
    Password = LoginData.Cells(2, "B").Value
    Dim IE As Object
    Set IE = CreateObject("InternetExplorer.Application")
    With IE
        .Visible = False
        .Navigate URL
        ieBusy IE
        .Visible = True
       Dim oLogin As Object, oPassword As Object
        Set oLogin = .document.getElementsByName("VS_LOGIN")(0)
        Set oPassword = .document.getElementsByName("VS_PASSWORD")(0)
        oLogin.Value = UserName
        oPassword.Value = Password
        .document.forms(0).submit
        ieBusy IE
       Stop
       '.document.getElementsByTagName("a")(2).href
       '.document.getElementsByClassName("link3").Click
     .Navigate2 ""
     ieBusy IE
     Stop
    End With
'''
End Sub
Sub ieBusy(IE As Object)
    Do While IE.Busy Or IE.readyState < 4
        DoEvents
    Loop
End Sub

And the first task is work, macro log in to website. I need to go deeper and click something but structure of web is too much for my small head I am looking some examples on website but nothing work. I showed code of website below. I need to click button "statystyka".

/html/body/div[1]/div[1]/a[2] - Xpath adress [link picture]https://ibb.co/2Pgx2tn

May you give me some help please :)

edit: I tried use something like this: '.document.getElementsByTagName("a")(2).href but this not good way on thinking

MaciekPol
  • 3
  • 3
  • What does `nothing work` means? Did you click it but nothing happen or is there some error? – Raymond Wu Sep 16 '21 at 08:58
  • It looks like the link actually runs a Javascript so google on `ExecScript` to find out how to execute Javascript using IE. – Raymond Wu Sep 16 '21 at 09:02
  • @Raymond Wu ,For my is issue is how to navigate this structure of document it is connected with html structure and dependence of html (doc -> head ->div etc). I don't know how to choose in coretly way line (I supouse that people with expirience with html and vba had knowglege how to do this): `Statystyka ` I tried something like this `'.document.getElementsByTagName("a")(2).href` – MaciekPol Sep 16 '21 at 09:23
  • I created a simple example to test QHarr's answer, and it works well. However, we cannot access the page you provided, so I am afraid that you cannot reproduce your problem. – Xudong Peng Sep 20 '21 at 10:20
  • @Raymond Wu I am waiting for QHarr reply, maybe he have some idea or other way to try. – MaciekPol Sep 20 '21 at 13:07

2 Answers2

1

You need to move into the appropriate frame, add a wait as I am using .Navigate to the frame src, then you can target by a substring of the onclick attribute:

ie.navigate ie.document.querySelector("[name=Navigator]").src
ieBusy ie
ie.document.querySelector("[onclick*=statistic]").click
QHarr
  • 83,427
  • 12
  • 54
  • 101
  • Hello thank You @QHarr i tried Your code `ie.navigate ie.document.querySelector("[name=Navigator]").src`
    after this line navigate to site with menubar
    `ie.document.querySelector("[onclick*=statistic]").click`
    after this line web log out to login site . Do You have an idea?
    – MaciekPol Sep 17 '21 at 05:33
  • The click logs you out? – QHarr Sep 17 '21 at 06:04
  • Thank You dear colluege for Your answer I try to show You on the picture, normaly when You manualy click you recived something like this (https://ibb.co/WykqLnW) using VBA it is look like this (https://ibb.co/bLfsNpK) and resul is log out (https://ibb.co/mtPc1KS) – MaciekPol Sep 17 '21 at 09:24
  • is there so different way of choose this button? Do You know any possibility? – MaciekPol Sep 21 '21 at 13:18
  • Try placing `ie.document.querySelector("[onclick*=statistic]").click` with `ie.document.parentWindow.ExecScript Replace$(ie.document.querySelector("[onclick*=statistic]").getAttribute("onclick"), " return false;", "")` - Also, are there any other steps that should be happening in between? – QHarr Sep 21 '21 at 22:48
  • the problem is that after line `ie.navigate ie.document.querySelector("[name=Navigator]").src` even if You click Manualy by hand "statistic" - log out. Is there other possibility to choose "Navigator" or directly click to "statistic". Thank You for Your help. – MaciekPol Sep 22 '21 at 13:13
  • you can try to walk down the contentDocument via the frame, https://stackoverflow.com/questions/44902558/accessing-object-in-iframe-using-vba – QHarr Sep 22 '21 at 16:25
  • Is there an option to click directly node CSS selector: "div.menubar-navi:nth-child(1) > a:nth-child(4)#text". I have checked that if you choose directly "https://kwm.kromi.de/cgi-bin/kwm?HTML=frontend/statistic/stat_current.htm&ID=7794CCAAE6459102319F3D5F7A291D33" but this id is always diffrent. May You check one more time. Do You know some way ? – MaciekPol Oct 03 '21 at 17:00
  • which id? And how does the id vary? – QHarr Oct 03 '21 at 23:32
  • see picture in main post behind XPATH, that Yoo have a tag href one click witk link https://kwm.kromi.de/cgi-bin/kwm?HTML=frontend/statistic/stat_current.htm&ID=7794CCAAE6459102319F3D5F7A291D33 but it olways generate new statistick online and ID=7794CCAAE6459102319F3D5F7A291D33 change every log in so You couldn`t navigate directly by this link, is there way to debug.print this changed href and navigate using this directly. – MaciekPol Oct 04 '21 at 08:11
0

If You want navigate by tag You need to set frame as is below

Dim doc As HTMLDocument
Dim doc2 As HTMLDocument
Dim lnk As HTMLLinkElement

Set doc = IE.document
 Set doc2 = doc.frames("Navigator").document
 Set lnk = doc2.getElementsByTagName("A")(1)
 lnk.Click

@QHarr @Raymond Wu :) Thank You for trying help, maybe that will be solution in others

MaciekPol
  • 3
  • 3