WebBrowser control Anchor tag target = _Blank opening outside WebBrowser control - vb.net

I want to navigate to new tab when click on anchor. My code :
Hide Copy Code
Dim elements As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("a")
For Each pElem As HtmlElement In elements
If (pElem.GetAttribute("HREF") <> "" AndAlso pElem.GetAttribute("HREF") <> "#" Then
WebBrowser1.AllowNavigation = True
pElem.SetAttribute("target", "_Blank")
pElem.InvokeMember("click")
End If
Next
When i Perform Click it will open in IE Browser instead of Web-Browser Control. Please brief me what I am doing wrong?
FYI, Instead of Navigate using URL i want to Navigate Using click and its compulsory.
Thanks in advance.

Related

How to check the visibility of the element in Edge Browser using VBA Selenium and alternative to Obj.Wait

Unable to check the visibility of the element. Please guide
At present using Obj.Wait to wait so that Site is loaded fully. Is there any other way which can check the Edge Browser has loaded fully before execution of next code
In the instant case
Obj.Wait 580000
If Obj.IsElementPresent(By.XPath("//*[#id='downloadReport']/div")) = True Then
I have a following VBA code + Selenium which is opening a website, populate data from Excel and displays the record.
Dim Obj As New WebDriver
Sub EdgeAutoSF1CRA0()
Set Obj = New Selenium.EdgeDriver
Dim By As New Selenium.By
Obj.SetCapability "ms:edgeOptions", "{""excludeSwitches"":[""enable-automation""]}"
Obj.Start "edge", ""
Obj.Get "https://************
Obj.Window.Maximize
Obj.FindElementByName("croreAccount").SendKeys ("Search")
Obj.FindElementByXPath("//*[#id='loadSuitFiledDataSearchAction']/div[1]/div[3]/div[4]/img").Click
Obj.FindElementById("borrowerName").SendKeys (ThisWorkbook.Sheets("Sheet1").Range("C5").Value)
Obj.FindElementByXPath("//*[#id='search-button']/ul/li[1]/div/input").Click
Obj.Wait 580000
If Obj.IsElementPresent(By.XPath("//*[#id='downloadReport']/div")) = True Then
Obj.FindElementByXPath("//*[#id='downloadReport']/div").Click
Else
Obj.FindElementByXPath("//*[#id='three-icons']/ul/li[3]/a/div").Click
End If
'Call EdgeAutoSF25LA0
End Sub
If match is found, then element
XPath("//*[#id='downloadReport']/div")
is visible. The code at site is
'a href="downloadStatusReport" id="downloadReport">Download</a
If no match is found, then element
XPath("//*[#id='downloadReport']/div")
is not visible. The code at site is
'a href="downloadStatusReport" id="downloadReport" style="display: none;">Download</a

VB.NET Find element, execute onclick and manage it on new tab in webbrowser

thanks for reading, I'm stuck trying to find an element of a webbrowser document, it doesn't have any ID, neither a classname, it just has an onclick attribute and an href attribute that is "#". What I need to do is to find such element and execute the "onclick" attribute it has, also I need to manage it because usually it opens in a new window. Thanks.
Update #1
My code so far:
For Each element As HtmlElement In WebBrowser1.Document.All
If element.GetAttribute("href").ToString.Contains("#") Then
element.InvokeMember("click")
End If
Next
Update #2:
The code of the first update still not working, but now I managed to open the new window in a new form.
' This will be triggered only when link tries to open in new window.
' That means active element in web document will always be a Link.
Dim myElement As HtmlElement = WebBrowser1.Document.ActiveElement
Dim target As String = myElement.GetAttribute("href")
Dim newInstance As New Form1
newInstance.Show()
newInstance.WebBrowser1.Navigate(target)
'cancel opening IE window
e.Cancel = True

Script Errors In VB

I am trying to stop those annoying Runtime Errors in VB. I am using a tabcontrol with a webbrowser item. I am trying to supress the errors. It does not return any errors but at runtime, it doesn't work. This is my code
CType(TabControl.SelectedTab.Controls.Item(0), WebBrowser).ScriptErrorsSuppressed = True
What am I doing wrong?
Your browser control may not be Item(0) on the page (it could be in a panel etc), so as an alternative (and safer method) you can use the Controls.Find method to search the selected tab and all child controls in order to find the Browser on that tab.
Something like this:
'assumes you have a control named WebBrowser1 on the first tab page etc
Dim browserControlName As String = String.Format("WebBrowser{0}", TabControl1.SelectedIndex + 1)
Dim browser As WebBrowser = CType(TabControl1.SelectedTab.Controls.Find(browserControlName, True).First, WebBrowser)
browser.ScriptErrorsSuppressed = True

Put text into a textbox from a website in VB.NET

I want to know how to put text to a website's textbox with VB.NET.
Like when you complete a textbox and press enter.
Something like this:
Dim web as webbrowser
web.document.textbox(1).insert("text")
it would look something like this:
WebBrowser1.Document.GetElementById("login_password").SetAttribute("value", TextBox2.Text)
As giuseppe has suggested, you need to search for the element in the document and then set it's value property. for example following code logins into website by setting its userId and password textboxes and clicking the submit button.
Dim htmlDoc=WebBrowser1.Document
Dim elem_Input_UsrName As HtmlElement = htmlDoc.GetElementById("username")
Dim elem_Input_PssWrd As HtmlElement = htmlDoc.GetElementById("password")
Dim elem_Input_Submit As HtmlElement = getElementByClassName("Submit", "Input", htmlDoc)
If elem_Input_UsrName IsNot Nothing AndAlso elem_Input_PssWrd IsNot Nothing Then
elem_Input_UsrName.SetAttribute("value", "xyz#ABC.com")
elem_Input_PssWrd.SetAttribute("value", "yourpassoword")
elem_Input_Submit.InvokeMember("click")
End If
This will help 100%:
webbrowser1.document.getElementById("yourtextboxidinwebsite").innertext = textbox1.text
I get a way that if u want to receive from the website itself another way I need to receive results from the website into the text box. U can use this method.
first, make a web browser to load the page
WebBrowser1.Navigate("https://tools.keycdn.com/geo")
then this
TextBox1.Text = WebBrowser1.Document.GetElementById("geoResult").InnerText

Acessing HTML buttons having the same names and values by vb.net

I am trying to access Some HTML buttons. I am using vb.net 2008 platform to click these buttons programatically . Problem is that the all 17 buttons are without id and have the same name type and value and are in a same form . So plz guide me how to click each button indivisually . Here is the HTML code of the these buttons.
lets say that the you got a webbrowser control
And The Html Is:
"<button id=namee>Click</button><button id=namee>Click</button><button id=namee>Click</button>"
you can change the id's of the butoons + click them:
WebBrowser1.Navigate("")
Do While WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
Application.DoEvents()
Loop
WebBrowser1.Document.Body.InnerHtml = "<button id=namee>Click</button><button id=namee>Click</button><button id=namee>Click</button>"
For i = 1 To 3
WebBrowser1.Document.Body.InnerHtml = Replace(WebBrowser1.Document.Body.InnerHtml, "namee", "n" & i.ToString, , 1)
Next
WebBrowser1.Document.GetElementById("n1").InvokeMember("click")
WebBrowser1.Document.GetElementById("n2").InvokeMember("click")
WebBrowser1.Document.GetElementById("n3").InvokeMember("click")
there is another way:
Dim Elems As HtmlElementCollection
Dim WebOC As WebBrowser = WebBrowser1
Elems = WebOC.Document.GetElementsByTagName("button")
For Each elem As HtmlElement In Elems
elem.InvokeMember("click")
Next