I am creating a windows form in VB.NET with web browser control, but not able to click on the below code.
<input type="Submit" class="btn btnSearch bold include_WidthButton" value="Search">
I can click when it is get element by ID, but not by classname. Please help me so much of googling didn't help me.
This is what I tried:
For Each Element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("btn btnSearch bold include_WidthButton")
If Element.OuterHtml.Contains("btn btnSearch bold include_WidthButton") Then
Element.InvokeMember("click")
End If
Exit For
There is no native function to get a collection of elements by class using the webbrowser document. However you can create your own collection using a function and looping through all elements using the document.all property.
Function ElementsByClass(document As HtmlDocument, classname As String)
Dim coll As New Collection
For Each elem As HtmlElement In document.All
If elem.GetAttribute("className").ToLower.Split(" ").Contains(classname.ToLower) Then
coll.Add(elem)
End If
Next
Return coll
End Function
Use would be like this:
Private Sub UpdatBtn_Click(sender As System.Object, e As System.EventArgs) Handles UpdatBtn.Click
For Each elem As HtmlElement In ElementsByClass(WebBrowser1.Document, "form")
elem.SetAttribute("className", elem.GetAttribute("className") & " form-control")
Next
End Sub
I see you are trying to base your collection off the entire className instead of an individual class so you would need to slightly modify.
This is my solution:
Public Sub clickButton(ByVal web As WebBrowser, ByVal tagname As String, ByVal attr As String, ByVal contains As String, ByVal sleep As Integer)
Try
If (web.Document IsNot Nothing) Then
With web.Document
For Each Elem As HtmlElement In .GetElementsByTagName(tagname)
If (Elem.GetAttribute(attr).Contains(contains)) Then
Elem.InvokeMember("Click")
Thread.Sleep(sleep)
Return
End If
Next
End With
End If
Catch ex As Exception
Show_Error(MODULE_NAME, "clickButton")
End Try
End Sub
use:
clickButton(WebBrowser1, "button", "classname", "btn btnSearch bold include_WidthButton", 2000)
Related
I'm trying to switch from the normal Webbrowser control (IE) at vb.net to CefSharp (ChromiunWebBrowser).
In the WebBrowser Control (IE), for example, I would have implemented a button click like this
Public Sub SetWebbrowserTagIDClick(ByVal TagName As String, ByVal TagID As String, ByVal Attribute As String, ByVal AttributeValue As String)
Dim ElementListe As HtmlElementCollection = Nothing
ElementListe = Mainform.WebBrowser1.Document.GetElementById(TagID).GetElementsByTagName(TagName)
For Each Element As HtmlElement In ElementListe
If InStr(Element.GetAttribute(Attribute).ToString, AttributeValue) > 0 Then
Element.InvokeMember("Click")
Exit For
End If
Next
End Sub
Is there a way to implement it in cefSharp in the same way or do I always have to go through Javascript?
In the web browser it didn't matter if I only addressed a class name. In cefSharp, isn't it difficult?
Warm greetings from Germany
On a VB.NET project with Metroframework Modern UI 1.4.0.0 installed, I use this part of code into a Module to loop over all MetroTextBoxes which placed into a MetroTabControl and give Cursor.Hand to their embedded Clear button. How can I do the same thing including MetroTextBoxes which could be out of MetroTabControl, into my form or into an other container for example? In other words, I would like to loop over every MetroTextBox into a form, even if it is nested.
Public Sub TxtBoxes_Cursors(sender, e)
Dim _FormSender = DirectCast(sender, MetroFramework.Forms.MetroForm)
For Each _MetroTabControl As Control In _FormSender.Controls.OfType(Of MetroFramework.Controls.MetroTabControl)()
For Each _TabPage As Control In _MetroTabControl.Controls.OfType(Of MetroFramework.Controls.MetroTabPage)()
For Each _Textbox As Control In _TabPage.Controls.OfType(Of MetroFramework.Controls.MetroTextBox)()
If _Textbox.HasChildren Then
For Each _ClearButton As Control In _Textbox.Controls
If _ClearButton.Name = "lnkClear" Then
_ClearButton.Cursor = System.Windows.Forms.Cursors.Hand
End If
Next
End If
Next
Next
Next
End Sub
This is the exact answer to my question according to the accepted answer of this relevant question which user A Friend pointed on comments. First I have to place this Function into my Module:
Public Function FindControlRecursive(ByVal list As List(Of Control), ByVal parent As Control, ByVal ctrlType As System.Type) As List(Of Control)
If parent Is Nothing Then Return list
If parent.GetType Is ctrlType Then
list.Add(parent)
End If
For Each child As Control In parent.Controls
FindControlRecursive(list, child, ctrlType)
Next
Return list
End Function
And then edit my Public Sub like this:
Public Sub TxtBoxes_Cursors(sender, e)
Dim _FormSender = DirectCast(sender, MetroFramework.Forms.MetroForm)
Dim _MetroTextBoxes As New List(Of Control)
For Each _Textbox As MetroFramework.Controls.MetroTextBox In FindControlRecursive(_MetroTextBoxes, _FormSender, GetType(MetroFramework.Controls.MetroTextBox))
If _Textbox.HasChildren Then
For Each _ClearButton As Control In _Textbox.Controls
If _ClearButton.Name = "lnkClear" Then
_ClearButton.Cursor = System.Windows.Forms.Cursors.Hand
End If
Next
End If
Next
End Sub
I'm working on a project where I have a WebBrowser control which needs to have a custom user-agent set, then go to Google and fill out the search box, click the search button, then click a link from the search results. Unfortunately I can't use HTTPWebRequest, it has to be done with the WebBrowser control.
Before I added the code to change the user-agent, everything worked fine. Here's the code that I have:
Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("urlmon.dll", CharSet:=CharSet.Ansi)> _
Private Shared Function UrlMkSetSessionOption(dwOption As Integer, pBuffer As String, dwBufferLength As Integer, dwReserved As Integer) As Integer
End Function
Const URLMON_OPTION_USERAGENT As Integer = &H10000001
Public Sub ChangeUserAgent(Agent As String)
UrlMkSetSessionOption(URLMON_OPTION_USERAGENT, Agent, Agent.Length, 0)
End Sub
Private Sub btnGo_Click(sender As Object, e As EventArgs) Handles btnGo.Click
ChangeUserAgent("Fake User-Agent")
wb.Navigate("http://www.google.com", "_self", Nothing, "User-Agent: Fake User-Agent")
End Sub
Private Sub wb_DocumentCompleted(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs) Handles wb.DocumentCompleted
Dim Source As String = wb.Document.Body.OuterHtml
Dim Uri As String = wb.Document.Url.AbsoluteUri
If Uri = "http://www.google.com/" Then
wb.Document.GetElementById("lst-ib").SetAttribute("value", "browser info")
wb.Document.All("btnK").InvokeMember("click")
End If
If Uri.Contains("http://www.google.com/search?") Then
Dim TheDocument = wb.Document.All
For Each curElement As HtmlElement In TheDocument
Dim ctrlIdentity = curElement.GetAttribute("innerText").ToString
If ctrlIdentity = "BROWSER-INFO" Then
curElement.InvokeMember("click")
End If
Next
End If
End Sub
End Class
The problem lies in the following code:
wb.Document.GetElementById("lst-ib").SetAttribute("value", "browser info")
wb.Document.All("btnK").InvokeMember("click")
I thought the problem might be that the page not being fully loaded (frame issue) but I put the offending code in a timer to test, and got the same error. Any help would be much appreciated.
Do you realize .All("btnK") returns a collection? So, you are doing .InvokeMember("click") on a Collection :). You cannot do that, you can only do .InvokeMember("click") on an element for obvious reasons!
Try this:
wb.Document.All("btnK").Item(0).InvokeMember("click")
The .Item(0) returns the first element in the collection returned by .All("btnK"), and since there will only probably be one item returned, since there is only one on the page, you want to do the InvokeMember on the first item, being .Item(0).
May I ask what it is you are developing?
Since you're a new user, please up-vote and/or accept if this answered your question.
I need to get the text between two span tags on a web page using visual basic.
<span>Some Text</span>
I know there must be a way but I can't seem to find it.
This is for a website i do not own.
Give your span an ID and runat="server" attribute e.g.
<span id="xMySpan" runat="server">Some Text</span>
Then you will be able to retrieve it in server-side code, e.g.
Dim sVar As String = xMySpan.InnerHtml
Are you extracting this from the entire HTML document or just the quoted text above?
If its just the above (and you've already filtered out the other HTML) then you can use a conbination of LEFT() and RIGHT() to snip off the ends, or use REPLACE() to get rid of the two tags.
What about assigning an ID to the span? If you do, then this works:
TextBox1.Text = _
WebBrowser1.Document.GetElementById("spanID").GetAttribute("innerText")
Using this format:
<span id="spanID">...</span>
EDIT: To filter by content:
$("span").filter(function(){
return $(this).html() == "a";
})
Will work with this:
<span>a</span>
I made this script, hope it will be helpful
I have:
Textbox to get the youtube url [urlVideo]
Button to load the page [btn_loadViews]
A webBrowser Control [webBrowser1]
and a label to show the text [lb_views]
I'm not validating anything, so This is just an example of how do i get text from websites.
If there's another way to do it, i would like to know it too. =)
Private Sub btn_loadViews_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_loadViews.Click
WebBrowser1.Navigate(urlVideo.Text)
WaitForPageLoad()
getViews()
End Sub
Private Sub getViews()
Try
Dim version = FileVersionInfo.GetVersionInfo("c:\windows\system32\ieframe.dll")
'Depending on the navigator version, google's server sends diffetent pages, so
'Here Detect ie version
If version.ProductVersion < "8" Then
lb_views.Text = WebBrowser1.Document.GetElementById("vc").FirstChild.InnerText
Else
lb_views.Text = WebBrowser1.Document.GetElementById("watch7-views-info").FirstChild.InnerText
End If
Catch ex As Exception
MsgBox(ex.ToString)
Application.Exit()
End Try
End Sub
Private Property pageready As Boolean = False
Private Sub WaitForPageLoad()
AddHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
While Not pageready
Application.DoEvents()
End While
pageready = False
End Sub
Private Sub PageWaiter(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
pageready = True
RemoveHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
End If
End Sub
Dim WithEvents hDoc As HTMLDocument
Set hDoc = WebBrowser1.Document
Dim strValue As String
strValue = hDoc.getElementsByName("so").Item(0).Value
I am opening a website in a WebBrowser control using VB.NET 2008. On the fourth page of the website, I want to focus the control by triggering the tab key programmatically. I am using the following code:
If adtxt.Text = "http://aojsl.com/dfassfeed2.php" Then
System.Windows.Forms.SendKeys.Send("{TAB}")
End If
However, my code is unable to trigger the tab key. Does anyone know how to make this work?
Method 1
Private Sub Form_Load()
WebBrowser1.Navigate "http://www.google.com/"
Do
Thread.Sleep(100)
Loop While webBrowser1.IsBusy = True
End Sub
Private Sub Command1_Click()
WebBrowser1.Document.All("q").focus 'Set focus to the search text field
End Sub
Private Sub Command2_Click()
WebBrowser1.Document.All("btnI").focus 'Set focus to the google "I Am feeling lucky button"
End Sub
Method 2
I converted it to VB.Net from this MSDN thread: Focus issues with System.Windows.Controls.WebBrowser
You will need to change the ActiveElement in webBrowser.Document.ActiveElement.Focus() to the textbox or button.
Public Partial Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
Dim host As New WindowsFormsHost()
im webBrowser As New WebBrowser()
host.Child = webBrowser
elementHost1.Child = host
webBrowser.Navigate(New Uri("http://www.google.com"))
Me.Activated += Function() Do
Console.WriteLine(Me.ActiveControl)
If webBrowser.Document <> Nothing Then
If Me.ActiveControl = elementHost1 AndAlso webBrowser.Document.ActiveElement <> Nothing Then
webBrowser.Document.ActiveElement.Focus()
End If
End If
End Function
End Sub
End Class
Method 3
Another way might be to do it in the HTML, eg:
OnLoad="document.myform2.mybutton.focus();">
lets say that the html of youre page is:
<button id="btn">Ok</button><input id="txt">
you can set focus in this way:
If adtxt.Text = "http://aojsl.com/dfassfeed2.php" Then
webbrowser1.document.getelementbyid("btn").focus()
webbrowser1.document.getelementbyid("txt").focus()
End If
Another way:
use the GetElementsByTagName(TagName)
lets say that your html is:
<button>no</button>
<button>no</button>
<button onclick='alert(1);'>--focus me!--</button>
<button>no</button>
Dim Elems As HtmlElementCollection
Dim WebOC As WebBrowser = WebBrowser1
Elems = WebOC.Document.GetElementsByTagName("button")
For Each elem As HtmlElement In Elems
If elem.InnerHtml = "--focus me!--" Then
elem.Focus()
elem.InvokeMember("click")
End If
Next
another one:
Dim num As Integer = 1
Dim elms As HtmlElementCollection
Dim wb As WebBrowser = WebBrowser1
elms = wb.Document.GetElementsByTagName("button")
For Each elem As HtmlElement In elms
If elem.Id = "" Then
elem.Id = "button" & num.ToString
num = num + 1
End If
Next
WebBrowser1.Document.GetElementById("button3").Focus()
To focus the select element by using the focus function in vb.net. For exsample,
Me.WebBrowser1.Document.All.Item("password").Focus()
This will put the focus on the element called password!
Use Me.WebBrowser1.Document.All.Item("YOURelement") to find the correct element and then add .Focus() to focus on the one you want! :D
Do this Me.WebBrowser1.Document.All.Item(textbox1.text).Focus()
make a Textbox and then if you want to Spambot it easy Detects everytime your Typ you Write and Send