Bear in mind that I know only a bit about HTML:
There is a site I'm trying to interact with using a WebBrowser. The site has a textarea element as follows:
<textarea name="ctl00$ContentPlaceHolderMain$txtCallDesc" rows="2" cols="20" id="ctl00_ContentPlaceHolderMain_txtCallDesc" tabindex="205" style="width: 100%; height: 80px; font-size: 8pt"></textarea>
From what I've read, the generated ID of the textarea signifies that it's placed inside another form of some sort, and I'm not sure if this is where I'm running into my problem.
Once the page has loaded, I have something like the following in a button:
Dim theCol As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("textarea")
For Each curElement As HtmlElement In theCol
ListBox1.Items.Add(curElement.TagName)
Next
Nothing populates in the list. I've also tried using the ID of the text box gathered by the 'inspect element' feature of Chrome:
Dim value As HtmlElement = WebBrowser1.Document.GetElementById("ctl00_ContentPlaceHolderMain_txtCallDesc")
MsgBox(value.GetAttribute("value"))
No matter what I do, I can't seem to get the program to recognize that there ARE textarea elements in the document. The source for the page is far to long to spam everyone with here, but is there anything I'm missing that I should be looking out for? Perhaps needing to get another element first, then search that for elements within it?
Edit:
The element I'm trying to get seems to be within an iFrame, but it looks like it's from the same domain so the same origin policy shouldn't come into play, should it?
<iframe id="mainFrame" width="100%" height="100%" frameborder="0" class="mainFrame" name="Main" src="/Calls/OpenCalls.aspx">
Using the code shown in Get Iframe HTML:
For i = 0 To WebBrowser1.Document.Window.Frames.Count - 1
Dim frameDoc = WebBrowser1.Document.Window.Frames(i)
Dim theCol = frameDoc.Document.GetElementsByTagName("textarea")
For Each curElement As HtmlElement In theCol
ListBox1.Items.Add(String.Format("TagName: {0} Id:{1}", curElement.TagName, curElement.Id))
Next
Next
The essential part being the use of WebBrowser1.Document.Window.Frames.
You can't reference elements inside an iframe directly since they are inside another document. So first get a reference to the document element inside the iframe and then you can query it the same way.
Dim frameDoc = WebBrowser1.Document.GetElementById("mainFrame").DomElement.contentWindow.Document
And the rest you already know...
Dim theCol = frameDoc.GetElementsByTagName("textarea")
For Each curElement In theCol
ListBox1.Items.Add(curElement.TagName)
Next
Related
I'm attempting to use VBA to scrape the link to a .gif file from this HTML fragment:
<div class="row">
<div class="col-md-12">
<div id='imageDiv' style='width:99%'>
<img style='width:99% !important; border:5px solid silver;' src="http://www.[rest of link].gif" alt="" />
</div>
My code below :
parent_url = "http://www.[webpage url]"
objIE.navigate parent_url
While objIE.Busy Or objIE.readyState <> 4
DoEvents
Wend
For Each ele In objIE.document.getElementsByTagName("imageDiv")
If InStr(ele.Style, "width") > 0 Then
ws1.Cells(2, 2) = ele.innerText: Exit For
End If
Next
objIE.Quit
This doesn't write anything to the spreadsheet even when I try a number of different element types.
Any pointers about what I'm doing wrong here?
TIA
Firstly, as pointed out by Tim Williams, imageDiv is an id and not a tag, so it can be reached via .getElementById() method, which returns a unique HTML element, in contrast to .getElementsByTagName() which returns a collection of HTML elements.
A tag in HTML, in its simplest form looks like <TagName>Inner Text</>.
So in your case, the tag name you are looking for is img and the id you're looking for is imageDiv.
So, if you want to get the element whose id is imageDiv and then get its img elements, and more specifically its first img element you would have to do it like so:
Dim img As HTMLImg
Set img = objIE.document.getElementById("imageDiv").getElementsByTagName("img")(0)
Secondly, the innerText is not what you are looking for. What you need is the src.
This can be reached like so:
Debug.Print img.src
To take advantage of the .src property, we store the element in a HTMLImg variable.
The code above will print the following to your immediate window:
http://www.[rest%20of%20link].gif/
References Used: Microsoft HTML Object Library
Try:
For Each ele In objIE.document.getElementById("imageDiv").getElementsByTagName("img")
In addition to the answers given it is more efficient and faster to use a css selector which returns a single node
Debug.Print objIE.document.querySelector("#imageDiv img").src
i will explain everything
so it will be at least useful. i searched many times without any good point i failed, hope some help,
please don't vote "unclear question".
i trying to click on specific
Dianna
Example:
<div class="Center_Home"><ul class="board"><li Address-Of-Center="Dianna" class="tile"></li></ul></div>
Here is my code that grabs div and all Required Classes elements.
Dim elementList As HtmlElementCollection
Dim curElement As HtmlElement
elementList = WebBrowser1.Document.GetElementsByTagName("div")
For Each curElement In elementList
If curElement.GetAttribute("classname").Equals("ul") Then
curElement.InvokeMember("click")
End If
If curElement.GetAttribute("classname").Contains("Dianna") Then
curElement.InvokeMember("click")
End If
Next
Addition info : i dont know if it important
> layout board : ul.board
> board inside : li.tile
Also i found this similar page it may help too :
https://docs.telerik.com/teststudio/testing-framework/write-tests-in-code/intermediate-topics-wtc/element-identification-wtc/finding-page-elements
Big Thanks ....I try VB.NET for a while
and Glade i Found This Great Forum. i asked Several question's And all Answered.
className refers only to the class="..." attribute, not all attributes on the element. You need to iterate the li elements instead and specifically check the Address-Of-Center attribute.
'curElement is declared by the loop, no need to make a variable of it.
Dim elementList As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("li")
For Each curElement As HtmlElement In elementList
If curElement.GetAttribute("Address-Of-Center").Equals("Dianna") Then
curElement.InvokeMember("click")
Exit For 'You must stop looping when you've found the element.
End If
Next
I'm creating an automation that will go through almost 110 pages with VBA. These pages have identical layout. I would need to go from one page to another automatically by "clicking" next button. At the very end of every page, there is a "button" (list anchor) that says "Next page". Problem is that the source code does not contain ID which would make it easy to refer with:
getElementById("id").Click
I open browser. That works fine. and I've tried something like this but it doesn't work:
Dim ieDoc As Object
Dim links As Object
Dim link As Object
Set ieDoc = ieApp.Document
Set links = ieDoc.Anchors
For Each link In links
If link.innerHTML = "innerHTML" Then
link.Click
Exit For
End If
Next link
I have tried almost everything I could find from stackoverflow but nothing worked for my needs.
THis is the source code of the "Next button" that I'm trying to click:
<li class="pager-next"><a title="Next page" href="/fi/tyosuhde- edut/kayttokohdehakupage=1&service_type=lunch&keywords=&city=&service=&service_areas=&payment_method=&municipality=&service_coupon_code=&items_per_page=50">seuraava ›</a></li>
I quess the problem is that the ClassName is in "li" and not in "a"?
Could some help me??
EDIT
Found a workaround!!:
Set pages = doc.getElementsByTagName("a")
For Each page In pages
If (page.getAttribute("title") = "Siirry seuraavalle sivulle") Then
page.Click
End If
Next page
You will need to keep reseting the html document with each refresh.
After a refresh try
ieApp.document.querySelector("a[title=""Next page""]").Click
CSS Selector
More info about CSS Selectors: CSS selectors
EDIT:
In your case the actual HTML selector is
appIE.doc.querySelector("a[title = ""Siirry seuraavalle sivulle""]").Click
Note there is no space after the "a" and you will need to leave enough time between clicks to allow the new page to load.
I am making a program that finds the source URL of a video from the website, www.clicktoview.org. I can download the captcha it requires, and display it, but I can't solve it with the user's input because the text box on the website doesn't have a value="" attribute.
Here is the relevant part of the HTML code:
<input type="text" id="recaptcha_response_field" name="recaptcha_response_field">
With this hindrance, is there any way I can input the user's captcha interpretation to the text field?
My code would be
WebBrowser1.Document.GetElementById("recaptcha_response_field").SetAttribute("value", TextBox2.Text)
but there isn't a value attribute.
N.B. The website is http://clicktoview.org/jbs2xyb89uai
Thanks for any help!
There isn't anything wrong with your code.
WebBrowser1.Document.GetElementById("recaptcha_response_field").SetAttribute("value",
TextBox2.Text)
should do what you want.
Even if a htmlElement doens't have a written value="" field you could still set it.
Have you checked to see that the GetElementById("recaptcha_response_field") returns a valid htmlElement?
Dim htmlElement As HtmlElement = WebBrowser1.Document.GetElementById("recaptcha_response_field")
If htmlElement IsNot Nothing Then
htmlElement.SetAttribute("value",TextBox2.Text)
End If
how can I parse this html code:
163 Punti<
I want to parse "163 Punti". I've tried to search on google but I didn't found nothing..
Someone could help me? Thanx
The element doesn't have an ID. So you can't get the element by using GetElementById method (which is the surest way to identify an element). However you can use other methods.
Dim allLinks As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("A")
For Each link As HtmlElement In allLinks
If link.GetAttribute("href") = "http://member.20dollars2surf.com/points.php" Then
Dim linkText As String = link.InnerHtml
MessageBox.Show(linkText)
End If
Next
The above code will work properly only if there is only one link on the page with that URL. Otherwise you will need to further customize this code.