how can i get all text with gecko c# - c#

I use geckofx in my project c#, how can I get all the output :
Text 1
Text 2
Text 3
if the code example is like this :
<div class="A">
<div class="B">
<div class="C">
<div class="D">Text 1</div>
</div>
<div class="C">
<div class="D">Text 2</div>
</div>
<div class="C">
<div class="D">Text 3</div>
</div>
</div>
</div>

How about:
var a = this.Document.GetElementsByClassName("A").FirstOrDefault(); //this returns the parent A div (if it exists)
if (a != null)
{
var text = a.TextContent; // and that gets the full text.
}
Please bear in mind that 'GetElementsByClassName' returns a collection, because there can be many elements with the same class.

Related

.NET Reduce duplicate code for styling elements with the same set of classes

I have a .NET 6 application where I am using the same set of Bootstrap classes to style cards across multiple views.
In a single view containing multiple cards, I am placing the classes inside variables to reduce duplicate code for the view.
E.g.
{
var card = "card h-100";
var cardHeader = "card-header d-flex justify-content-between align-items-center bg-light-lighten header-title text-info pb-2 pt-2 px-3";
var cardBody = "card-body pt-2 pb-0";
var detailRow = "row mb-2";
var detailLabel = "col-4 col-md-3 col-lg-4 font-weight-bold";
var detailValue = "col";
}
<div class="#card">
<h4 class="#cardHeader">
Details
<a class="#cardHeaderBtn"></a>
</h4>
<div class="#cardBody">
<div class="#detailRow">
<div class="#detailLabel">Status</div>
<div class="#detailValue">#Model.Status</div>
</div>
<div class="#detailRow">
<div class="#detailLabel">Order No</div>
<div class="#detailValue">#Model.OrderNo</div>
</div>
<div class="#detailRow">
<div class="#detailLabel">Buyer</div>
<div class="#detailValue">#Model.BuyerBusinessName</div>
</div>
</div>
</div>
<div class="#card">
<h4 class="#cardHeader">Finance & Units</h4>
<div class="#cardBody">
<div class="#detailRow">
<div class="#detailLabel">Buying</div>
<div class="#detailValue">#Model.BuyExchangeRate</div>
</div>
<div class="#detailRow">
<div class="#detailLabel">Selling</div>
<div class="#detailValue">#Model.SellExchangeRate</div>
</div>
</div>
</div>
I'm sure there is a better way to style all my cards across different views without duplicating the variables code into them, but I am having trouble finding the right keywords to search for what that might be!
What is the best way to reduce duplicate code when it comes to styling elements with the same set of bootstrap classes?
Thank you very much in advance.

How get text content geckofx c#

i use geckofx c# to get the textcontext. why can't I get the textcontent result if the html element is like this
<div>
<div class="text1">
<div class="text2">Michael</div>
<div class="text2">Andrey</div>
</div>
</div>
output : null
whereas if the html element like this
<div class="text">
<div class="text1">
<div class="text2">Michael</div>
<div class="text2">Andrey</div>
</div">
</div>
output :
Michael, Andrey
I use geckofx code like this
GeckoNodeCollection names = geckoWebBrowser1.Document.GetElementsByClassName("text1");
foreach (GeckoNode name in names)
{
console.writeline(name.TextContent);
}
the difference that I see is in <div> and <div class="text">. I appreciate all the help you provide.

how to print the elements with text value that contains in a list selenium c#

Anyone know how to print out all the elements that contain in a list with text value in selenium c#? Try to do like the code below it print out blank value. But if i were to put writeline with elem only the value was display but it is not in text form. I would like to get value with text.
Code:
IList<IWebElement> attachmentList = driver.FindElements(By.ClassName("comment-box"));
foreach (IWebElement element in attachmentList)
{
Console.WriteLine(element.Text);
}
HTML:
<div class="comment-box">
<!-- Comment Image -->
<div class="col-xs-2">
<div id="attachmentImgSFHD-24" class="attachmentImg">
<img src="downloadAttachment?attachmenturl=/secure/thumbnail/10111/_thumb_10111.png" />
</div>
</div>
<!-- Attachment details -->
<div class="col-xs-10">
<div class="commentContent">
<div class="topRow">
<div class="username">ApplicationLink.png</div>
<div class="commentTimeStamp">31400 KB</div>
</div>
<div class="bottomRow">
<div class="commentDisplay">
Download
</div>
</div>
</div>
</div>
</div>
<div class="comment-box">
<!-- Comment Image -->
<div class="col-xs-2">
<div id="attachmentImgSFHD-24" class="attachmentImg">
<img src="downloadAttachment?attachmenturl=/secure/thumbnail/10313/_thumb_10313.png" />
</div>
</div>
<!-- Attachment details -->
<div class="col-xs-10">
<div class="commentContent">
<div class="topRow">
<div class="username">test.jpg</div>
<div class="commentTimeStamp">7423 KB</div>
</div>
<div class="bottomRow">
<div class="commentDisplay">
Download
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<br/>
The elements don't have text in the html, so element.Text is empty. Use
Console.WriteLine(element.GetAttribute("value"));
You can use the below Xpath to get the attachment details
Xpath: //div[#class='comment-box']//div[#class='commentContent']//div[#class='username']
Code:
IList<IWebElement> attachmentList = driver.FindElements(By.XPath("//div[#class='comment-box']//div[#class='commentContent']//div[#class='username']"));
foreach (IWebElement element in attachmentList)
{
Console.WriteLine(element.Text);//It will print all the attachment name like 'ApplicationLink.png,test.jpg'
}
IList<IWebElement> attachmentList = driver.FindElements(By.ClassName("comment-box"));
foreach (IWebElement element in attachmentList)
{
System.Threading.Thread.Sleep(2000);
Console.WriteLine(element.Text);
}
it works fine by putting the thread.sleep code

Output csharp into a html class

Trying to acheive outputing a variable from csharp into the class of a html element.
Currently i'm doing this:
#foreach (var cocktail in Model)
{
<li>
<div class="drink">
<div class="drink-sprite " ></div>
</div>
<div class="details">
<div class="name">#cocktail.CocktailName</div>
<div class="price">£#cocktail.Price.ToString("0.00")</div>
<div class="description">#cocktail.Description</div>
</div>
</li>
}
essentially in the div class="drink-sprite" I want to put the drink type in the glass so an image is shown based on the class types. so drink-sprite martini - which is dervied from cocktail.type.
Anythoughts?
#foreach (var cocktail in Model)
{
<li>
<div class="drink">
<div class="drink-sprite #cocktail.Type"></div>
</div>
<div class="details">
<div class="name">#cocktail.CocktailName</div>
<div class="price">£#cocktail.Price.ToString("0.00")</div>
<div class="description">#cocktail.Description</div>
</div>
</li>
}
Why don't you just print the variable directly in the class declaration?
Like so:
<div class="drink-sprite #cocktail.Type" ></div>
This should output the cocktail.Type directly into the HTML as you'd need.
If cocktail.Type is null or "" (empty), then this will still work, and won't affect your current implementation.

How to fetch the record from database and load it to labels?

My labels: The labels are mentioned here followed by the code.
<div id="valueIntroduction" class="labelarea" runat="server"> </div>
<div class="line"></div>
<div id="valueBacklog notice" class="labelarea"> </div>
<div class="line"></div>
<div id="valueReasonably complete" class="labelarea"> </div>
<div class="line"></div>
<div id="valueStatement of purpose" class="labelarea"> </div>
<div class="line"></div>
<div id="valueResume" class="labelarea"> </div>
<div class="line"></div>
<div id="valueTranscripts" class="labelarea"> </div>
<div class="line"></div>
<div id="valueGRE Official test scores" class="labelarea"> </div>
<div class="line"></div>
<div id="valueTOEFL or IELTS Official test scores" class="labelarea"> </div>
<div class="line"></div>
<div id="valueLetters of Recommendation3" class="labelarea"> </div>
<div class="line"></div>
<div id="valueLetters of Recommendation2" class="labelarea"> </div>
<div class="line"></div>
<div id="valueLetters of Recommendation1" class="labelarea"> </div>
<div class="line"></div>
<div id="valueSignature" class="labelarea"> </div>
<div class="line"></div>
<div id="valueSubject" class="labelarea"> </div>
<div class="line"></div>
This code is assigning the last record to the first label. How do I assign the correct record to correct label?
if (Department.Items[0].Selected)
{
firstPanel.Visible = true;
myLegend.InnerText = "Informatics";
NewComm.CommandText = "getTextHeaderINFO";
NewComm.CommandType = CommandType.StoredProcedure;
NewComm.Connection = NewConn;
NewComm.CommandTimeout = 3000;
SqlDataReader results = NewComm.ExecuteReader();
while (results.Read())
{
Response.Write(results["TEXT_CONTENT"].ToString());
valueIntroduction.InnerHtml = results["TEXT_CONTENT"].ToString();
}
}
Well, as it is written now you are setting the same label's InnerHtml in every iteration of the while loop.
I would think you would be doing something more like this:
label1.InnerHTML = results["text_content"];
results.read()
label2.InnerHtml = results["text_content"];
etc.
In short, you'll need to assign the results iteratively. It may be safer to read them into a List or a string array (in your loop) so you can verify count, but essentially you have to set each label individually. I can't image how you could expect it to work setting a single one over and over again.
Now, what you SHOULD be doing, when you intend to work with HTML elements programmatically is very different from what I see here.
What you're doing here requires you to lookup HTML elements by ID, assign to their InnerHtml, and is honestly very error-prone. It's also not a good use of C# or the .NET environment.
Use .NET labels instead. Define them like this:
<asp:Label runat="server" id="valueIntroduction" CssClass="labelarea" />
Then on the code behind you can access them much easier:
valueIntroduction.Text = "This text will show up in the valueIntroduction label";
Why do you call results.Read() in a loop? You should just find the one single result row in the results and put this one into the valueIntroduction.InnerHtml property.

Categories

Resources