Dynamically adding divs and images during runtime - vb - html

I have a table full of users which includes a username and an image. What I'm trying to do is for every user in the table, I want to create a new div with a class of "user" which contains a label for their username and their image. Here's what I have so far:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim label1 As New Label
label1.ID = "label1"
label1.Text = "Username goes here"
Controls.Add(label1)
End Sub
I found out how to add a label but I'm not sure how I'd position it, or add HTML elements with a label and image object inside them. Here's what I'm hoping the result will look like:
<div class="user">
<h2> Username </h2>
<img src="user.png">
</div>
Hopefully this could then be repeated as many times as necessary for each user.

Or you can do it like this, here an example in c#
var div = new Panel();
div.Controls.Add(new LiteralControl("<h1>hallo</h1>"));
div.Controls.Add(new Image());
Page.Form.Controls.Add(div);

You will probably want to use the asp:repeater control, you bind it the same way you bind a grid I.e
myRepeater.DataSource = results
myRepeater.DataBind
I will come back to to this an provide a full detailed explanation for this answer as I'm own my phone at the moment.

Related

Issue getting information using XPath method in Selenium VBA Excel

I am struggling getting the information of the name of a person in a website. Here is what I see when I inspect the page :
As you can see, I highlighted the line that I think is causing me some trouble. I don't know why when I select the line " User : Piquemal Bruno" (second line starting from the bottom) and I create an XPath I only have this :
/html/body/table/tbody/tr/td[2]/table/tbody/tr1/td[2]/b
I don't know why I don't have the previous information, as if it doesn't exists. Normally I should have this : /html/frameset/frame/document/html/body/table/tbody/tr/td[2]/table/tbody/tr1/td[2]/b
But something is blocking me from having the whole path.
Sub GetUserInfo()
Dim k As WebElements
Dim h As WebElement
Dim FindBy As New Selenium.By
EdgeWeb.get """"INTERNET PAGE THAT I WANT""""
Set h = EdgeWeb.FindElementByName("banner")
Set k = EdgeWeb.FindElements(FindBy.XPath("/html/frameset/frame/document/html/body/table/tbody/tr/td[2]/table/tbody/tr[1]/td[2]/b"))
Debug.Print k.Count
End Sub
Here k is empty but h sees an element. Which is normal because the banner is just before the #document tag. It really looks like the #document tag is cutting the path.
I tried many other methods like FindElementByTag or FindElementByCSS but nothing works.
Hope you can help me and thank you beforehand.

How to get text from HTML element

I have been trying off and on for the past couple of days to get the text from an HTML element into my listbox.
The URL is https://www.roblox.com/users/88510187/favorites#!/places
and I am trying to scrape the favorite places of the user, for this instance the first would be "B is for build" here is what I have most recently tried and get nothing.
Private Sub test3()
Dim divs = WebBrowser1.Document.Body.GetElementsByTagName("div")
For Each d As HtmlElement In divs
If d.GetAttribute("className") = "text-overflow item-card-name ng-binding" Then
ListBox1.Items.Add(d.OuterText)
End If
Next
End Sub
I've changed the sub around many times playing with inner and outer text and Html but cannot seem to get just plain text. What on earth am I missing?

Retrieving the text between the <div> with VBA

I am trying to get a text string from inside a div on a webpage, but I can't seem to figure out how it is stored in the element.
Set eleval = objIE.Document.getElementsByClassName("outputValue")(0)
Debug.Print (eleval.innerText)
I have tried this and variations thereof, but my string just reads as "".
I mainly need help on how is this type of data is referenced in VBA.
<div class="outputValue">"text data that I want"</div>
Here is a screenshot of the page in question, I cannot give a link since it requires a company login to reach.
With .querySelector method, make sure page if fully loaded before attempting.
Example delays can be added with Application.Wait Now + TimeSerial(h,m,s)
Set eleval = objIE.Document.querySelector("div[class="outputValue"]")
Debug.Print eleval.innerText
If it is the first of its className on the page you could also use:
Set eleval = objIE.Document.querySelector(".outputValue")
If there is more than one and it is at a later index you can use
Set eleval = objIE.Document.querySelectorAll(".outputValue")
And then access items from the nodeList returned with
Debug.Print eleval.Item(0).innerText 'or replace 0 with the appropriate index.
Dim elaval as Variant
elaval = Trim(Doc.getElementsByTagName("div")(X).innerText)
msgbox elaval
Where X is the instance of your class div

Get value from HTML element for display in Textbox

I have tried adapting a handful of solutions Ive found on here and cannot get any to work. Most recently,
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Dim Web As New HtmlAgilityPack.HtmlWeb
Dim Doc As New HtmlAgilityPack.HtmlDocument
Doc = Web.Load("http://MyWebSearch.com/s/" + TextBox1.Text)
For Each table As HtmlAgilityPack.HtmlNode In Doc.DocumentNode.SelectNodes(<div class="inline-block"></div>)
Textbox5.text(table.InnerText)
Next
End Sub
I am trying to conduct a search with a fixed address where + textbox1 contains the search item. I need to return the value from one element on the page into Textbox5 after search is conducted. I cant for the life of me get this to work. Ive tried obtaining the xpath but that failed also. What am I doing wrong?
The web page is rbx.trade/s/"username"
I am trying to return the users "Rap" and display in textbox5
You're searching for the class of an element. You probably want to search for the ID instead.
For Each table As HtmlAgilityPack.HtmlNode In Doc.DocumentNode.SelectNodes(<div id="elementID"></div>)
Textbox5.text(table.InnerText)
Next
Or if you indeed mean to be using the class, store each value in an array or append to the text box instead of storing it directly. Say there are 8 element with that class. Doing it the way you are will always store the value of the 8th skimmed element with that class name.

Form Text Boxes Fail to Update With Client Input After Their Value Is Set Server Side

Apt title
This code demonstrates my confusion:
Private Sub loadCss()
textbox.Value = method.getCSS("existingCssFile.css")
End Sub
This method, loadCss(), occurs when the page is loaded. It's purpose is to fill a textarea, which has runat="server" and id="textBox" respectfully, with all the content from a specified css file.
Protected Sub saveCss_Click(sender As Object, e As EventArgs) Handles saveCss.Click
method.saveCSS(textbox.Value, "fileToOverwrite.css")
End Sub
When an asp:Button is clicked the latter method fires. However, the string value of textbox is identical to its initial value ignorant of client additions or editions; once the textbox receives its value, it doesn't change server side.
I have attempted switching from textbox.value to textbox.InnerHTML but to no avail despite numerous combinations of the two.
I know the solution now, but can anyone explain why this happens?
Ok you need to call your method inside page_load event and have to check the postback for this page is false.
Please try following code :
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
If IsPostBack = False Then
loadCss()
End If
End Sub
In the method where you are calling loadCss(), enclose it in:
if(!IsPostBack)
{
loadCss();
}
Hope this helps!
EDIT: Whoops, I didn't see your comment Bhavesh, sorry! :/
This is the VB equivalent (I think, not an expert on VB). Put it in the method where you are calling loadCss();
If Not IsPostBack
loadCss()
End If