2013-03-06 47 views
5

Tôi đang gặp một số rắc rối khi nhập văn bản vào hộp tìm kiếm khi sau tôi là thẻ ID chỉnh sửa. Tôi đã nhận được ID từ mã nguồn của trang. Tôi đã làm điều này trước với các trang web khác. Ai đó có thể giúp tôi không? Có một cách khác để làm điều này?VBA - IE GetElementByID không hoạt động

Sub FileUpload() 

Dim IEexp as Object 
IEexp.visible = True 
IEexp.Navigate ("www.example.com") 

'this is where the problem 
IEexp.Document.GetElementByID("step1_id_bean_newSupportingDoc_description").Value _ 
= "monthly update" 

End Sub 

tôi nhận được một "Lỗi Automation Các Object gọi đã bị ngắt kết nối từ các khách hàng của mình"

Source Code nơi tôi kéo ID từ:

<td class="Label">Description</td> 
    <td class="Data"><input type="text" name="bean.newSupportingDoc.description" size="60" maxlength="250" value="" id="step1_id_bean_newSupportingDoc_description" class="NoBorder"/> 
</td> 
+2

Bạn có đợi tài liệu tải trước khi cố truy cập phần tử đầu vào không? –

+0

tôi chạy nó cục bộ và tôi đợi sự kiện tải trang xảy ra và tôi gặp lỗi iwebbrowser2 – user1902540

+0

Có, tôi đã chạy nó ở chế độ ngắt vì vậy tôi đợi cho đến khi trang tải hoàn toàn và tôi vẫn gặp lỗi đó. –

Trả lời

0

Bạn có thể thử

Do Until IEexp.readyState = 4 
DoEvents 
Loop 



IEexp.Document.getElementById("username").Value = "Monthly update" 


IEexp.Document.getElementById("password").Value = FilePth 
0

Mã hoạt động. Điều gì về 'chế độ bảo vệ'? Xem bài viết này: http://www.sevenforums.com/tutorials/63141-internet-explorer-protected-mode-turn-off.html. Nếu trình duyệt IE của bạn chạy ở chế độ bảo vệ, hãy thử tắt và chạy lại mã.

' Add References: 
' - Microsoft HTML Object Library 
' - Microsoft Internet Controls 

Sub FileUpload() 

    Dim IEexp As InternetExplorer 
    Set IEexp = New InternetExplorer 
    IEexp.Visible = True 
    IEexp.navigate "www.example.com" 

    Do While IEexp.readyState <> 4: DoEvents: Loop 

    Dim inputElement As HTMLInputElement 
    Set inputElement = IEexp.Document.getElementById("step1_id_bean_newSupportingDoc_description") 

    If (Not inputElement Is Nothing) Then 
     inputElement.Value = "monthly update" 
    Else 
     MsgBox "Input element not found on web page." 
    End If 

    IEexp.Quit 
    Set IEexp = Nothing 
End Sub 
1

Nếu bạn sử dụng Set IEexp = New InternetExplorerMedium, bạn không phải thay đổi cài đặt trong Tùy chọn Internet. Nó tự động khởi tạo đối tượng IE với các thiết lập ứng dụng Integrity Medium.

Các vấn đề liên quan