How get text content geckofx c# - 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.

Related

how can i get all text with gecko 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.

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

How to choose the respective attachment to download in selenium c#

Anyone know how to select the specific element that contain in the attachment to download as i can't able to select the respective attachment. the screenshot I have put in the below :
below is the html code for the specific container
<div class="attachment-wrap">
<!-- Comment Title -->
<div id="attachmentTitle-wrapTEST" class="attachmentTitle-wrap">
<h2>Attachments</h2>
</div>
<div id="attachment-containerTEST">
<!-- Attachment Box -->
<div class="comment-box">
<!-- Comment Image -->
<div class="col-xs-2">
<div class="attachmentImg">
<img src="downloadAttachment?attachmenturl=/secure/thumbnail/10104/_thumb_10104.png" />
</div>
</div>
<!-- Attachment details -->
<div class="col-xs-10">
<div class="commentContent">
<div class="topRow">
<div class="username">1177A149.PNG</div>
<div class="commentTimeStamp">25927 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 class="attachmentImg">
<img src="downloadAttachment?attachmenturl=/secure/thumbnail/10103/_thumb_10103.png" />
</div>
</div>
<!-- Attachment details -->
<div class="col-xs-10">
<div class="commentContent">
<div class="topRow">
<div class="username">4D7746B6.PNG</div>
<div class="commentTimeStamp">62766 KB</div>
</div>
<div class="bottomRow">
<div class="commentDisplay">
Download
</div>
</div>
</div>
</div>
</div>
Appreciate if you can help me with this.
You can do it in the below way to click the respective download link based on the attachment name
Code:
//Expected attachment filename needs to be specified here.
var expectedFileName = "1177A149.PNG";
//This list will hold all the available attachment section details
IList<IWebElement> attachmentList = _driver.FindElements(By.ClassName("comment-box"));
foreach(var element in attachmentList)
{
var attachmentFilename = element.FindElement(By.XPath(".//div[#class='username']")).Text;
if(attachmentFilename == expectedFileName)
{
//If the actual file name and expected file name matches, then corresponding download link will be clicked
element.FindElement(By.XPath(".//a")).Click();
break;
}
}
You an create xpath specific to png name. see here
//a[contains(#href,'downloadAttachment?filename=4D7746B6.PNG')]
It will point to download link having filename=4D7746B6.PNG

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