2017-01-17 5 views
0

this site 드롭 다운 메뉴에서 국가 및 언어를 선택할 수 있지만 "새 신청서 완성"버튼을 클릭하면 선택할 수 있습니다. 들판은 비어 있다고합니다.vba를 사용하여 웹 페이지의 드롭 다운 목록에서 값 선택

도움을 주시면 감사하겠습니다.

Sub Test() 

strURL = "https://visa.kdmid.ru/PetitionChoice.aspx" 

    With ie 
    .Visible = True 
    .navigate strURL 

    While .Busy 
     DoEvents 
    Wend 

    Set html = .document 

    'Country where you will apply for visa. 
    Set ctY = html.getElementById("ctl00$phBody$Country") 
    For i = 1 To ctY.Options.Length 
     If ctY.Options(i).Text = "NETHERLANDS" Then 
      ctY.selectedIndex = i 
      Exit For 
     End If 
    Next i 

    'Select Language 
    Set lnG = html.getElementById("ctl00$phBody$ddlLanguage") 
    For i = 1 To lnG.Options.Length 
     If lnG.Options(i).Text = "ENGLISH" Then 
      lnG.selectedIndex = i 
      Exit For 
     End If 
    Next i 

    'Click I have read instructions check box 
    html.getElementById("ctl00$phBody$cbConfirm").Click 


    'Click apply button 
    Set btnGo = html.forms(0).all("ctl00$phBody$btnNewApplication") 
    btnGo.Click 

    End With 

    End Sub 

답변

1

그래서 당신이 올바른 궤도에 있지만 당신이 사이트의 HTML 보면 당신은 첫 번째, 'ctl00_phBody_Country'을 가지고 선택 - 국가 두 요소는 실제로있다, 그러나 이것은 실제로 단지입니다 드롭 다운되며 실제 선택된 값은 'ctl00_phBody_cddCountry_ClientState'에 저장됩니다 ... 언어 섹션은 비슷한 구조를가집니다. 마지막으로 허용되는 값은 드롭 다운에 표시되는 국가 이름뿐 아니라 실제로 드롭 다운의 국가 코드와 국가 이름의 조합입니다.

Public Sub Test() 
Dim IE As InternetExplorer 
Dim HTMLDoc As HTMLDocument 

Dim countryStr As String 
Dim countryObj As HTMLObjectElement 
Dim countryCodes As IHTMLElementCollection 
Dim codeCounter As Long 
Dim languageStr As String 
Dim languageObj As HTMLObjectElement 
Dim languageCodes As IHTMLElementCollection 

countryStr = "Netherlands" 
languageStr = "English" 

Set IE = New InternetExplorer 

With IE 
    .Visible = False 
    .Navigate "https://visa.kdmid.ru/PetitionChoice.aspx?AspxAutoDetectCookieSupport=1" 
    While .Busy Or .ReadyState <> READYSTATE_COMPLETE: Wend 
    Set HTMLDoc = IE.document 
End With 

Set countryObj = HTMLDoc.getElementById("ctl00_phBody_cddCountry_ClientState") 
Set countryCodes = HTMLDoc.getElementById("ctl00_phBody_Country").getElementsByTagName("option") 
For codeCounter = 0 To countryCodes.Length - 1 
    If countryCodes(codeCounter).innerText = UCase(countryStr) Then 
     countryObj.Value = countryCodes(codeCounter).Value & ":::" & countryCodes(codeCounter).innerText & ":::" 
     While IE.Busy Or IE.ReadyState <> READYSTATE_COMPLETE: Wend 
     Exit For 
    End If 
Next 

Set languageObj = HTMLDoc.getElementById("ctl00_phBody_cddLanguage_ClientState") 
Set languageCodes = HTMLDoc.getElementById("ctl00_phBody_ddlLanguage").getElementsByTagName("option") 
For codeCounter = 0 To languageCodes.Length - 1 
    If languageCodes(codeCounter).innerText = UCase(languageStr) Then 
     languageObj.Value = languageCodes(codeCounter).Value & ":::" & languageCodes(codeCounter).innerText & ":::" 
     While IE.Busy Or IE.ReadyState <> READYSTATE_COMPLETE: Wend 
     Exit For 
    End If 
Next 

HTMLDoc.getElementById("ctl00$phBody$cbConfirm").Click 
While IE.Busy Or IE.ReadyState <> READYSTATE_COMPLETE: Wend 
HTMLDoc.getElementById("ctl00_phBody_btnNewApplication").Click  'Launch Form 

IE.Quit 
Set IE = Nothing 
End Sub 
+0

감사합니다. 그러나이 작업을 실행하면 필드가 비어있는 동일한 오류가 발생합니다. – newguy

+0

흠, 다시 확인하기 위해 다시 한 번 다시 실행했는데 드롭 다운은 비어있는 것처럼 보였지만 다음 화면으로 넘어갈 때 어떤 오류도 발생하지 않습니다 ... 아마도 누락 된 참조가 있습니까? 오직 내가 생각할 수있는 것 - 'Microsoft HTML Object Library'와 'Microsoft Internet Controls'가 모두 추가되었는지 확인하십시오. – TheSilkCode

+0

나는 그것이 나의 마지막에 작동하지 않는 라이브러리를 참조했다. 당신이 아마 당신의 파일 버전을 나에게 보낼 수 있을까? 왜냐하면 나는 여전히 작동하지 않기 때문에 답변에서 귀하의 코드를 가져 갔기 때문입니다. – newguy

관련 문제