I am writing an windows application in VB.Net to automate the internet explorer using Visual Studio 2005.

There is a website with list of orders. I need to accept the orders automatically whenever they arrive.

There are 2 pages in a websiteand they will refresh at regular intervals. In the first page there is a list of elements in a grid with Accept and Decline buttons. I have to click on Accept button. Then next page loads with some text contents and a Check box and a Submit button. I need to check check box and click on the button automatically.

I am able to click the accept button on first page and the page redirects to second page. But the second page uses modal window to load the page contents with check box and submit button.


So I am not able to get the html source of that modal window. Whenever I try to get the source I am getting the source of the page in the background.


Any help in finding the source code of a modal window is much appreciable.

If it is not achievable in VB.Net, please let me know whether it is possible with any other language or tools.

Dani AI

Generated

The page you described is almost certainly showing a separate browser document when the modal opens (as hinted with the JS example). That means the modal is not part of the background page DOM — it is a different Internet Explorer instance/frame — so reading Document on the original IE object will keep returning the background page. Two practical ways to get the modal DOM from VB.Net are: 1) enumerate browser instances and find the modal, or 2) retrieve the document from the modal window handle via the accessibility API.

Try this workflow first — it is the simplest and often works:

  • Add COM references: Microsoft Internet Controls (SHDocVw) and Microsoft HTML Object Library (MSHTML).
  • Enumerate SHDocVw.ShellWindows, inspect each InternetExplorer.Document (or LocationURL/LocationName) and look for the modal page or the expected elements. When found, manipulate the DOM normally (set checkbox, click button). Make sure the automation runs on an STA thread and wait for document.readyState = "complete" before interacting.

Example (conceptual):

' Add refs: SHDocVw and MSHTML
Dim shells As New SHDocVw.ShellWindows()
For Each ie As SHDocVw.InternetExplorer In shells
    Dim doc = TryCast(ie.Document, mshtml.IHTMLDocument2)
    If doc IsNot Nothing AndAlso ie.LocationURL.Contains("partOfDialogUrl") Then
        Dim chk = doc.getElementById("checkboxId")
        If chk IsNot Nothing Then chk.setAttribute("checked", True)
        Dim btn = doc.getElementById("submitId")
        If btn IsNot Nothing Then btn.click()
        Exit For
    End If
Next

If enumeration does not find the modal (some modal dialogs are hosted differently), fallback to the Windows accessibility method: call AccessibleObjectFromWindow (oleacc.dll, OBJID_NATIVEOM) with the modal HWND to get an IDispatch you can cast to an MSHTML document. P/Invoke that API, convert the returned object to mshtml.IHTMLDocument2, then inspect it. See Microsoft docs for AccessibleObjectFromWindow for details.

Notes and troubleshooting:

  • Confirm whether the “modal” is a true separate window (showModalDialog/window.open) or a DOM overlay (jQuery/HTML overlay). Overlays live in the same document and are accessed via the original Document.
  • Use Spy++/UI Spy to find the modal HWND when needed.
  • Ensure correct COM threading (STA) and release COM objects (Marshal.ReleaseComObject) to avoid leaks.
  • If long-term automation is required or IE quirks become a problem, consider using a WebDriver (Selenium) or UI Automation approach instead.

Reference: AccessibleObjectFromWindow (oleacc) documentation — https://learn.microsoft.com/en-us/windows/win32/api/oleacc/nf-oleacc-accessibleobjectfromwindow

Recommended Answers

All 2 Replies

Javascript code to open modal window in html page.

<script type="text/javascript">
function doopen1()
{
   window.showModalDialog ("win1.php",null,"dialogLeft:400,dialogTop:100px;dialogHeight:100px;dialogWidth:300px;");
 }
</script>

My requirement is totally different. I am accessing a website which uses modal window to show page contents. I want to get the html source of that modal window using my VB.Net application.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.