getting value of attribute to string vb.net - vb.net

I want to get the value of the src attribute (https://www.google.com) to a string from a web browser HTML elements. The code of element is:
<img src="https://www.google.com" height="500" width="500">
Code I have tried is:
For Each o As HtmlElement In WebBrowser1.Document.GetElementsByTagName("img")
If o.GetAttribute("height") = "500" Then
Dim url As String = o.GetAttribute("src").ToString
Exit For
End If
Next
String url is empty every time I also tried to get url in textbox but its also empty.

problem is that is not a server side control, it is plain html. to make matters worse there are not classes or ID's to make it easier to isolate which control u want to target. Also javascript could be generating that image so that could make it harder.

Related

vb.net how to get value of <strong> using htmlagilitypack

How to get value under using hhtmlagilitypack. by the way there are many<tr><td> from this site, so would be much better if I can get the specific value under this "Social Security Number"
<tr><td><span>Social Security Number</span></td><td><strong>524-23-6748</strong></td></tr>
I tried using this code but no luck
Dim webGet As New HtmlWeb
Dim doc As HtmlDocument = webGet.Load("https://www.fakeaddressgenerator.com/World_Address/get_us_address/city/Chicago")
Dim work As HtmlNodeCollection = doc.DocumentNode.SelectNodes("/html/body/div[1]/div[3]/div[1]/div/div/div[2]/div[1]/div[2]/table/tbody/tr[6]/td[2]/strong")
TextBox1.Text = work.First().Attributes("value").Value
This one is different to the last; the xpath is right except for the tbody. It's important to remember that the document inspector you see in a browser might not reflect the actual source, and the source is what HAP parses
Easiest way I find to debug these is to remove the trailing part of the path and look what I get in the locals window:
In terms of how to know if it's attibute values or inner text etc, that comes from looking at the structure of the HTML:
Yellow highlights are attributes (things between < and > that is in the form name="value" is an attribute name/value pair), red underlines innertexts (anything between > and <)
All in your code is right apart from the TBODY, and that you're trying to select the Value of an attribute called "value" but your strong doesn't have any attributes, it only has an inner text

VBA automation can't get innerText (innerHTML, value)

So, i've gotten innertext many times. This time, it's simply not working. Tried many alternatives.
First of All
I'm using Selenium on VBA to use chromedriver.
The source website is "[http://www.fazenda.df.gov.br/area.cfm?id_area=1536]" This website operates on a Iframe. After half an hour trying to get the inner text from it, i figured that the link in its code works just as fine: "[http://publica.agnet.fazenda.df.gov.br/FichaImovel/#/FichaCadastral]". This last one seems to operate on someting call "Angular".
The menu with the innertext i required is a dropdown one. As a dummy value, you can use 50868748. The dropdown text that it generates is composed by 15 lines and one table. The info that i need is on the 15th line, "Nome do Responsável", or in the column 2 line 2 on the table.
I managed to place the code and generate the dropdown list, but i can't manage to get the inner text.
Here is my code:
Dim iptu As String
Dim driver As New ChromeDriver
With driver
.Start
iptu = 50868748 'dummy
.Get "http://publica.agnet.fazenda.df.gov.br/FichaImovel/#/FichaCadastral"
.FindElementById("inscricaoImovel").Clear
.FindElementById("inscricaoImovel").SendKeys iptu
.FindElementsById("btnCadastro")(2).Click
.Wait (300)
'until here, everything works fine
Dim label As Object
Dim name As String
Set label = .FindElementsByClass("ng-binding")(91)
'as you can see, i end up using this class(91) to get the value on the table, but i can get from the 15th line too
name = label.getAttribute("innerText") 'Error is here
'i've tried .innerText, .innerHTML and others. The error is always "object does not accept property or method" Error 438.
end with
The chrome driver is up to date. Also, the page source does not cointain the info i need.
Anyone can help me out?
To get InnerText of the element, You need to call Text method
Set label = .FindElementsByClass("ng-binding")(91)
name = label.Text
To get inner Html,
name = label.Attribute("innerHTML")
Sometimes I have found that I have to execute javascript to get what I'm after. You can try
name=driver.ExecuteScript("document.getElementsByClassName('ng-binding')[91].innerText;")

Unable to get the Query String value

Whenever I'm hitting this URL getting vToken is nothing. Please assist
Dim vHDR As String = Nothing
If Request.IsLocal Then
Dim vToken = Request.QueryString("approvalToken")
If Not vToken Is Nothing Then
Dim vUser = Helper.ValidateToken(vToken)
If Not vUser Is Nothing Then
vHDR = vUser
End If
End If
End If
URL:
http://localhost:56979/#/modules/Review?ID=20582&approvalToken=SIsJ3swuGjpPidOvWPSzuqfcTJH8IywIlVVEnkmIQU6yE93PUQ
In a URL, everything beginning with the hash sign (#) is called the Fragment Identifier. The fragment identifier section is the last section of the URL and comes after the url arguments.
So for this url from the question:
http://localhost:56979/#/modules/Review?ID=20582&approvalToken=SIsJ3swuGjpPidOvWPSzuqfcTJH8IywIlVVEnkmIQU6yE93PUQ
there is no query string. The section that looks like a query string was pre-empted to be part of a very long fragment identifier. Even the /modules/Review section that looks like part of the Path is actually part of the fragment.
I suspect somewhere in your base/index page you have a link with a blank href value, which is sometimes expressed with a single hash (#). You click that link, which puts the hash sign into the url:
http://localhost:56979/#
After clicking this link, you then redirect, click a new link, or do some other action that appends the relative url /modules/Review (which may already include the query string) to your current location. But now you have the hash sign as part of that existing location, and so you end up with the url in your question, which doesn't mean what you want it to mean.
I'd have to see a lot more code to tell you exactly how to fix this, but in the context of vb.net (asp.net), if this code runs on the server you probably want to change /modules/Review to ~/modules/Review. If this is done in javascript, I'd need to know more about your page.

Visual Basic Web Browser change form when certain webpage content detected

I am trying to make a form on VB.NET that when a certain webpage is loaded into the web browser (in this case its a .txt file with a string of numbers). It will look for those numbers and if it finds them it will close that form and open another. I have tried to do it many times and my latest attempts came out with this code. Any help would be appreciated.
Dim passcontents As String
passcontents = WebBrowser.DocumentText = "test.com/test.txt"
If WebBrowser.DocumentText = "test.com/test.txt" And passcontents Then
ActualGen.Show()
Me.Close()
Else
End If
There are a couple of basic problems with the code you've posted.
1) Are you trying to test passcontents as a boolean or a string? You've defined it as a String, but then you are trying to assign the equality of two other strings to it, as you would a Boolean.
Try setting Option Strict On at the top of your code - it gives a few more helpful pointers to check in this case.
2) The If... Then condition is checking the same data as passcontents (may) already be providing. Are you just trying to check that the correct page is loaded, or do you want to check for certain page contents as well?
You appear to be getting mixed up between the browser's document URL and the loaded document contents.

Vb.net click link by looking for String!

I am working on a vb.net program, I want to click a hyperlink on a page, the source look like this:
messages for Today, 2010-10-19
I want to check it everyday too!
I tried to click it with the following methods(Both couldn't click the link!):
Dim theElementCollection As HtmlElementCollection
Dim ctrlIdentity As String
theElementCollection = WebBrowser1.Document.GetElementsByTagName("a")
For Each curElement As HtmlElement In theElementCollection
ctrlIdentity = curElement.GetAttribute("innerText").ToString
If ctrlIdentity = Today.Date.ToString(Today.Date.ToString("dd")) Then
curElement.InvokeMember("click")
End If
Next
and I tried this code too:
If Me.WebBrowser1.Document.Links(i).InnerHtml.Contains(Today.Date.ToString("dd")) Then
Me.WebBrowser1.Document.Links(i).InvokeMember("Click")
End If
Next
Any help would be appreciated! Thanks!
I've found that the best way to click links in a WebBrowser is using javascript. Try something like this:
WebBrowser1.Navigate("javascript:function%20x(){document.getElementById('foo').click()}x()")
You'll need to rewrite your above code in javascript but that's a piece of cake. You can test your javascript by copy-pasting it directly into the browser's location bar. This is also a reliable way to fill out forms.
Caveats:
Notice how the work that I want to do is wrapped in a function. This is needed if you want the javascript to do multiple statements. Wrap in a function and then invoke the function.
You can't navigate to a URL more than around 500 characters. (The limit isn't exactly 512 but it's close.) There's no warning, either, so keep it in mind.
Make sure you wait until the page is loaded. The ReadyState = Complete and IsBusy = False.
Clicking like this doesn't always generate the usual events that you get when you click a link.
"%20" is hex for space. I don't recall if this was strictly necessary in my code. Try it both ways.