vb.net open link in new IE11 tab (instead of window) - vb.net

I need to force my application to open few links (there are few buttons - one link under one button) in IE11 (regardless if it's set as default browser or not).
I tried multiple ways, but the result is always the same like with:
Process.Start("iexplore.exe", address)
It works perfectly for Firefox, or Chrome, but for IE - it always opens new window for each link. Regardless IE is already opened or not.
Few years ago I wrote similar thing which worked with IE7 I guess...:
Imports SHDocVw
Dim internetExplorerInstances As New ShellWindows()
Dim foundIE As Boolean = False
foundIE = False
For Each ie As InternetExplorer In internetExplorerInstances
If ie.Name = "internet explorer" Then
ie.Navigate(address, &H800)
foundIE = True
Exit For
End If
Next
If Not foundIE Then
With oPro
.StartInfo.UseShellExecute = True
.StartInfo.Arguments = address
.StartInfo.FileName = "iexplore"
.Start()
End With
End If
But today, with IE 11 it doesn't work.... I mean it still opens URLs in new windows.
Do you have any ideas how to programatically force IE11 to open link in new Tab, not in new window?

Generally above code is OK except one detail - it should be:
If IE.Name = "Internet Explorer" Then... it is case sensitive.
Finally my code looks like this:
Imports SHDocVw
Const openInTab As Object = &H800
Dim internetExplorerInstances As New ShellWindows()
Public Function IsProcessOpen(ByVal name As String) As Boolean
For Each clsProcess As Process In Process.GetProcesses
If clsProcess.ProcessName.Contains(name) Then
Return True
End If
Next
Return False
End Function
(...)
And call:
If IsProcessOpen("iexplore") Then
For Each IE As InternetExplorer In internetExplorerInstances
If IE.Name = "Internet Explorer" Then
IE.Navigate2(address, openInTab)
Exit For
End If
Next
Else
Process.Start("iexplore", address)
End If
And it works :)

Related

Is there a way to open an IE window with a user-defined name. When same URL opened, already opened IE window comes to foreground

Here is my requirement which I intend to implement. There is one window application showing some icons which take me to different web sites. When I click on one of the icons, it should open an IE window and append a customized name to it.
So, before opening any website after I click on website icon, I want to check if there is already an IE window open with that customized name, if yes, bring that already opened window to the foreground. If not, open a new IE window.
I have checked various questions posted which are related to what I am looking to achieve, but am somehow not able to get it right. Below is my attempt.
For Each e In shellWins
If InStr(1, e.GetProperty("IEWindowName"), namedWindow, CompareMethod.Text) <> 0 Then
hWnd = e.HWND
myIE = e
End If
Next
If hWnd == -1
Dim p As New Process
Dim psi As New ProcessStartInfo(IEPath, webSiteURL)
p.StartInfo = psi 'Trying to open a new IE window
p.Start()
For Each ie In shellWins
If ie.hwdn = p.MainWindowHandle Then
ie.PutProperty("IEWindowName", namedWindow)
End If
Next
End if
Else
myIE.BringToForeground()
This sometime works and sometimes does not. Is there any better way to do it?
It doesn't work in which situation? Does there any error throw when it doesn't work? If there is, please tell us the detailed error information and in which line it occurs.
Besides, you could try to compare the url to check if the website is already open in IE like this:
Sub Main()
Dim shellWins As SHDocVw.ShellWindows
Dim explorer As SHDocVw.InternetExplorer
shellWins = New SHDocVw.ShellWindows
Dim SQuery As String = "https://www.example.com/"
For Each explorer In shellWins
If explorer.Application.Name = "Internet Explorer" And explorer.LocationURL.Contains(SQuery) Then
explorer.BringToForeground()
End If
Next
shellWins = Nothing
explorer = Nothing
End Sub

How to click on element within iframe?

I’m trying to do web automation on IE using Selenium library.
Elements I want to click have iframes.
However, I’m getting the “element not found” error.
I tried many different approaches.
Sub activeBexIE_Final()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim Perm_bot As New Selenium.IEDriver
Perm_bot.Get "https://nalcodirect.nalco.com/irj/servlet/prt/portal/prtroot/pcd%213aportal_content%212fcom.sap.pct%212fplatform_add_ons%212fcom.sap.ip.bi%212fiViews%212fcom.sap.ip.bi.bex?BOOKMARK=0O867HQNBT13YLB5Q21HLAL2H", timeout:=1000, Raise:=False
Perm_bot.Wait 5000
' website opening successfully
Perm_bot.FindElementById("logonuidfield").SendKeys "XYZ#ABC.com"
Perm_bot.SendKeys Perm_bot.Keys.Tab
Perm_bot.SendKeys "ABCD"
Perm_bot.SendKeys Perm_bot.Keys.Enter
' the error is element not found at .click line
Perm_bot.switchToFrame Perm_bot.FindElementByName("iframe_Roundtrip_9223372036563636042")
Perm_bot.FindElementById("BUTTON_OPEN_SAVE_btn1_acButton").Click
Perm_bot.Wait 5000
Application.DisplayAlerts = True
Application.ScreenUpdating = True
perm_dot.Quit
Set perm_dot = Nothing
End Sub
HTML:
Iframe Screenshot:
Element Screenshot:
It might because the script runs faster than the page load. You could add a wait before switch to the iframe. Besides, there might be other elements with name iframe_Roundtrip_9223372036563636042 so you could use FindElementById when searching the iframe.
I made a simple sample with code below and it can work well:
Public Sub seleniumtutorial()
Dim driver As New Selenium.IEDriver
driver.Get "https://zhouyuzy.github.io/create-test-page/iframe.html "
driver.Wait 5000
driver.SwitchToFrame driver.FindElementById("iframe_Roundtrip_9223372036563636042")
driver.FindElementById("BUTTON_OPEN_SAVE_btn1_acButton").Click
driver.Wait 5000
End Sub
The result is like below:

Windows Form run website without address bar

I've seen a lot of examples using JavaScript to run a popup window to remove address bar. My issue is I want to run a full website [that I've created in ASP.net] without the address bar visible from a Windows Form application.
I've tried:
Dim objIE As Object 'InternetExplorer 'or as object - if you want to keep it lite and don't add the reference
objIE = CreateObject("InternetExplorer.Application")
With objIE
.Visible = True
objIE.AddressBar = False
objIE.MenuBar = False
objIE.ToolBar = False
.Navigate2(Address)
End With
objIE.Navigate = Address
but I get an error at CreateObject of Cannot create ActiveX component.
So my current working code to run the website is:
Dim process As New System.Diagnostics.Process()
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden
Dim sInfo As New ProcessStartInfo(Address)
Process.Start(sInfo)
However, the address bar is visible.
Any suggestions?
That code should work fine, as I just tested it myself. I would say start a fresh VB file (console application/windows form, whichever you'd like) and try to run just these:
Dim objIE as Object
objIE = CreateObject("InternetExplorer.Application")
With objIE
.Visible = True
End With
If it does not run from there, then your best bet is to try to "Reset" your internet explorer, which can be done through the Settings/Options. I've done this in the past and it allowed to work. The fact that you cannot create the object is the concerning part, as that's being written correctly and when I run that on my PC it works.
Give those two a shot (try a fresh file, and reset internet explorer). And let me know if that works.

VBA IE click button that requires argument?

I'm using Excel and programming in VBA.
I've located a button that I want to click. However, it seems this button requires an argument.
Using the Console in Chrome, I typed this:
document.getElementById("button1").click
That line resolved function click() { [native code] }
so instead I tried this:
document.getElementById("button1").click ('100', '2016-03-02')
And it worked. So, how do I run the same thing from VBA?
I have tried the following:
Sub ClickTheButton()
Dim IE As SHDocVw.InternetExplorer
Dim Doc As MSHTML.HTMLDocument
Set IE = New SHDocVw.InternetExplorer
With IE
.Visible = True
.Navigate "(webpage here)"
Do Until Not .Busy And .ReadyState = 4
DoEvents
Loop
Set Doc = .Document
Doc.GetElementByID("button1").Click ' nothing happens when running this
Doc.GetElementByID("button1").Click "('100', '2016-03-02')" ' not working (can't even attempt to run this)
Doc.GetElementByID("button1").Click ("'100', '2016-03-02'") ' not working (can't even attempt to run this)
End With
End Sub
I can't run the procedure, because of the code after .Click returning Wrong number of arguments or invalid property assignments.
Any ideas?
Answer found here.
The click button ran a function. Instead of clicking the button I could instead just run the function like this:
Dim CurrentWindow As HTMLWindowProxy
Set CurrentWindow = IE.Document.parentWindow
Call CurrentWindow.execScript("xajax_DoCalReservation('100', '2016-03-02')")

Reusing browser/tab in VB.net

I am using Process.Start() to open a URL and it's great for a one-time use but if opening multiple URLs it creates either a new instance of the default browser or uses a new tab. I need to use the original tab.
Suggestions?
For Internet Explorer, you will need to reference the shdocvw.dll COM component located, by default, at c:\windows\system32\shdocvw.dll. This COM component contains a ShellWindows object that you can use to determine if there is a running instance of Internet Explorer or not, like this:
Dim internetExplorerInstances As New ShellWindows()
Dim foundIE As Boolean = False
For Each ie As InternetExplorer In internetExplorerInstances
If ie.Name = "Windows Internet Explorer" Then
ie.Navigate(ur, &H800)
foundIE = True
Exit For
End If
Next
If Not foundIE Then
' Either do nothing or use Process.Start with a new browser instance
End If
For the other browsers, you are, unfortunately,out of luck, programmatically.