0

I have a code for web scraping. It operates pretty well but after some operations I can't figure how to proceed as there is no unique parameters to connect VBA commands to.

Here is my current code:

Sub ChechAutomate()

    Dim ie As New InternetExplorer, url As String, ws As Worksheet

    Set ws = ThisWorkbook.Sheets("Other Data")

    url = "https://infra.com"

    With ie
        .Visible = True
        .Navigate2 url

        While .Busy Or .ReadyState < 4: DoEvents: Wend

        With .Document

        If .querySelectorAll("#login-bis-id-btn").Length > 0 Then

            .querySelector("[name=userName]").Value = "username"
            .querySelector("[name=password]").Value = "password"
            .querySelector("[type=submit]").Click

        Else

            Application.Wait (Now + TimeValue("00:00:01"))

            .querySelector("[id=companySearchKeyData]").Value = ws.Range("T24").Value
            .querySelector("[type=submit]").Click

            Application.Wait (Now + TimeValue("00:00:01"))

            .querySelector("[name=creditType] [value='17']").Selected = True

            Application.Wait (Now + TimeValue("00:00:01"))

            .querySelector("[id=legalForms] [value='EN']").Selected = True

        End If

        End With

    End With

End Sub

1) After upper code is ready I need to click this button:

enter image description here

I have tried .querySelector("[name=#]").Click. Text "New decision" is changing within the language of the page. Also there are other type="button" buttons.

2) After (1) has been done, I need to input value to this field:

enter image description here

I have tried to use .querySelector("[name=questions[0].answer]").Value = "1000" it gives an error.

3) After (1) and (2) I need to click a button:

enter image description here

10101
  • 2,232
  • 3
  • 26
  • 66
  • What do you mean _Text "New decision" is changing within the language of the page_ ? Also, please use the snippet tool via [edit] to insert html not post images so we can use for testing, – QHarr May 28 '19 at 11:13
  • If I choose another language on a web page, for example German then "New decision" text will be "Neue Entscheidung". Of course one option is list all the possible variants (there will be 3) but I was wondering if there is some common solution to click it in every language version. I would be glad to share source code to make it easier but there is too much private information. – 10101 May 28 '19 at 11:45
  • 1
    But sharing as images is not that helpful I'm afraid. Can you obfuscate the private info? I am about to post a short term answer we can work on. Also, do this language change other parts of the problem? For example, the attribute values for the last button (part 3 of your question)? – QHarr May 28 '19 at 11:47
  • I understand that image is not so much help... `value="Order"` is changing according to language settings – 10101 May 28 '19 at 11:51
  • The same solutions as per your problem 1 – QHarr May 28 '19 at 11:51

1 Answers1

0

Caveat:

We really need to see more html. Without seeing the entire html (obfuscate private info) with problems like 1 and 3 I can only give general approaches to consider.


Problem 1)

For now, you have three other options at least:

1) grab the collection/nodelist and index in to get correct button/loop all items checking for unique innerText/attribute if available

Set col = ie.document.getElementsByTagName("input")  'collection. For Each over
Set nodeList = ie.document.querySelectorAll("input") 'nodelist. For i = 0 to nodeList.Length -1

Indexing:

col(0)  'example index
nodeList.item(0)  'example index

2) Find a relationship between this element and another that captures this element. Or some combination of attributes to identify. See answer here for some further detail.

For example, perhaps adding parent div class

ie.document.querySelector("div.content [type=button]")

3) Pass a list with Or syntax of the language variants

ie.document.querySelector("[value='New decision'], [value='Neue Entscheidung'],[value='Other language variant']")

Problem 2)

You need to use single quotes inside of double quotes

ie.document.querySelector("[name='questions[0].answer']").value = 1000

Problem 3)

As per problem 1.


Footnotes:

Remember querySelector returns first match for pattern, querySelectorAll returns all matches.

QHarr
  • 83,427
  • 12
  • 54
  • 101
  • 1
    I figured out how to dive deep starting from div from your answer. Also your solution with double quotes helped. Thank you again master! – 10101 May 28 '19 at 12:16