How to select the html element with same class name? - c#

I tried to select the second div with class="editor-col", but the element that i get is in the second div in first div with class="editor-col".
C# code:
/*Create and initialize object*/
IWebElement dropdown_priority = driver.FindElement(By.CssSelector("div.k-edit-form-container div:nth-child(2)")); //select issue tab
HTML element:
<div class="k-edit-form-container">
<div class="editor-label"></div>
<div class="editor-label"></div>
<div class="editor-label"></div>
</div>
Apreciate your advice on how to select the second div with class="editor-col". Thanks.

U find all divs with same class and use index to get 2nd of em
By.XPath(".//div[#class='editor-col'][1]")

Related

How To Get Div inside Div htmlagilitypack

first .. sorry about my bad english
my question is how can i scrape div inside div in htmlagilitypack c#
this is test html code
<html>
<div class="all_ads">
<div class="ads__item">
<div class="test">
test 1
</div>
</div>
<div class="ads__item">
<div class="test">
test 2
</div>
</div>
<div class="ads__item">
<div class="test">
test 3
</div>
</div>
</div>
</html>
how to make a loop that get all ads then loop that control test inside ads
You can select all the nodes inside class all_ads as follow:-
var res = div.SelectNodes(".//div[#class='all_ads ads__item']");
.//div[#class='all_ads ads__item'] This will select all the nodes inside all_adswhich has class ads_item.
You have to use this path => //div[contains(#class, 'test')]
This means you need to select those div(s) that contains class with name ads__item.
and then select all those selected div(s) inner html. like
class Program
{
static void Main(string[] args)
{
string html = File.ReadAllText(#"Path to your html file");
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
var innerContent = doc.DocumentNode.SelectNodes("//div[contains(#class, 'test')]").Select(x => x.InnerHtml.Trim());
foreach (var item in innerContent)
Console.WriteLine(item);
Console.ReadLine();
}
}
Output:

How to Create new div for each loop in C#

I am using HTML with c#. How i can create new div for each loop in the while to be able to make the designee as i want
my html code:
< div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
<div class="blog-post2">
<div class="post-img"></div>
<div class="post-text">
<div class="date">
<span class="post-date">26</span>
<span class="month">Oct<br>2017</span>
</div>
<h4><a href = "#" > Title </a></h4>
<p>Detelse</p>
detelse
</div></div>
</div>
my C# code:
SqlCommand cmd_check2 = new SqlCommand();
cmd_check2.CommandText = #"SELECT
[title_news]
FROM [website_DB].[dbo].[long_news_T] where id = 1";
//cmd_check.Parameters.AddWithValue("#e_mail", txtUserName.Text);
DataTable df2 = WebTbi_V1._1.func.general.GetData(cmd_check2, Label1, "website");
foreach (DataRow row2 in df2.Rows)
{
//Console.WriteLine("- - - Row - - -");
foreach (var item_1 in row2.ItemArray)
{
my for code ????
}
test.InnerHtml = html;
}
i want create a div for my news depending on result query
i need help
With a little googling you can find many and many samples for this problem
foreach (var item_1 in row2.ItemArray)
{
HtmlGenericControl myDiv = new HtmlGenericControl("div");
myDiv.Attributes.Add("class", "col-lg-4 col-md-4 col-sm-6 col-xs-12");
/*
You need to do same thing for every div you have inside
the parent div, and simply add them to parent div and
finally add your parent div to a Panel or Page or
any other place holder you have
*/
myPanel.Controls.Add(myDiv);
}
You might then need to pay attention for the event, where you generate your controls
You can use text template for it as one of the ways.
Try https://github.com/jehugaleahsa/mustache-sharp for example. Just create HTML template and use {{#each}} tag for loop.
In the future, it will be easier for you to change the template and as for me it's easier to make markupю

Identify XPath from particular element

I'm working on a page, where page loads dynamically and the data gets added while scrolling. To identify the properties of an item, I identified the parent div, where to identify the address, I have to locate an XPath from the parent to span element.
Below is my DOM structure:
<div class = "parentdiv">
<div class = "search">
<div class="header">
<div class="data"></div>
<div class="address-data">
<div class="address" itemprop="address">
<a itemprop="url" href="/search/Los-Angeles-CA-90025">
<span itemprop="streetAddress">
Avenue
</span>
<br>
<span itemprop="Locality">Los Angeles</span>
<span itemprop="Region">CA</span>
</a>
</div>
</div>
</div>
</div>
</div>
</div>
Here I want to locate the three spans, where I' currently in parent div.
Can someone guide how to locate an element using XPath from particular div?
You can try the following XPaths,
To locate the street address:
//div[#class="parentdiv"]/div/div/a/span[#itemprop="streetAddress"]
To locate the locality/city:
//div[#class="parentdiv"]/div/div/a/span[#itemprop="Locality"]
To locate the state:
//div[#class="parentdiv"]/div/div/a/span[#itemprop="Region"]
To print the list of <span> tagged WebElements with texts like Avenue with respect to div class = "parentdiv" node you can use the following block of code :
IList<IWebElement> myList = Driver.FindElements(By.CssSelector("div.parentdiv > div.address > a[itemprop=url] > span"));
foreach (IWebElement element in myList)
{
string my_add = element.GetAttribute("innerHTML");
Console.WriteLine(my_add);
}
Your DOM might become fairly large, since it adds elements while scrolling, so using CSS selectors might be quicker.
To get all the span tags in the div, use:
div[class='address'] span
To get a specific span by using the itemprop attribute use:
div[class='address'] span[itemprop='streetAddress']
div[class='address'] span[itemprop='Locality']
div[class='address'] span[itemprop='Region']
You can store the elements in a variable like so:
var streetAddress = driver.FindElement(By.CssSelector("div[class='address'] span[itemprop='streetAddress']"));
var locality = driver.FindElement(By.CssSelector("div[class='address'] span[itemprop='Locality']"));
var region = driver.FindElement(By.CssSelector("div[class='address'] span[itemprop='Region']"));

Selenium webdriver get value from data-bind on span tag

I need to get the value from this tag
<span>
<a class="jsk-sa-dialog link-lightbox-valores" data-bind="attr: { href: '#sa-valor-' + $root.types().id }" href="#sa-valor-2">
Link to click that pop-ups a new window
</a>
</span>
After look at the "html" code in debugger, I saw the text that I want to get into HTML...
To get the text that is generated into the popup window, I wrote this code:
case "someBaseText":
{
details.Click(); // Simulate the click on <a> tag
var tx = details.FindElement(By.XPath("//div[#class='overview-material']"));
var dv = tx.FindElement(By.XPath("//div[#class='sa-valor']/h3"));
var ttText = dv.Text; // Empty :(
}
The HTML that i got from debugger:
<div class="overview-material">
<div class="valores" data-bind="foreach: $root.types">
<div class="sa-valor" data-bind="attr: { id: 'sa-valor-' + id }" id="sa-valor-2">
<h3 class="titulo">Mensalidade</h3>
<div class="texto" data-bind="html: apresentacao.valor">
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
So, I want to get the text inside of div class "texto"
Based on the XPath of dv, you are getting the <h3> element:
<h3 class="titulo">Mensalidade</h3>
therefore dv.Text should return "Mensalidade".
Try updating your XPath to the <div> with class='texto':
//div[#class='sa-valor']/div[#class='texto']
Try CssSelector:
var text = driver.FindElement(By.CssSelector("div.texto")).Text;

C# - Extract speciifc div class text using HTMLAgility

I have a code in C# where I want to extract the below value (the text "I want this text" in the HTML code below). I have reformat the HTML code to make it easily readable.
<div class="paste-copy-url" style="margin:0 0 0 0;">
<h4>My Stats:</h4>
<div class="line">
<div class="wrap-input">
<input onclick="this.select();" value="I want this text" readonly="readonly">
</div>
</div>
<h4>Website Link:</h4>
<div class="line">
<div class="wrap-input"><input onclick="this.select();" value="Some value" readonly="readonly">
</div>
</div>
</div>
The code I tried (It is giving me the text : "Website Link:"):
var myvaluetoextract = htmlDocument.DocumentNode.SelectSingleNode("//div[#class='paste-copy-url']");
What am I doing wrong? Can I use this approach to get that element (There is only 1 instance of the div class in the page)?
var input = htmlDocument.DocumentNode
.SelectSingleNode("//div[#class='paste-copy-url']//div[#class='wrap-input']/input");
var yourText = input.Attributes["value"].Value;
You can do it like this:
var myvaluetoextract = htmlDocument.DocumentNode.SelectSingleNode("//div[#class='paste-copy-url']//input");
var value = myvaluetoextract.GetAttributeValue("value", null);
//input means you search for input elements in the div's subtree, recursively. GetAttributeValue is a helper that will never fail, even if the attribute doesn't exists (in this case if will return the 2nd passed parameter - which is null here)

Categories

Resources