I have the following HTML. I want to add class="last" attributes to the final li elements in each list. How can I do this?
<div class="gpbscol">
<ul class="listl">
<li>ACCESSORIES</li>
<li>AMPLIFIERS</li>
<li>ANALOG AUDIO PROCESSING</li>
<li>MICROPHONE PREAMPLIFIERS</li>
<li>MICROPHONES</li>
<li>SPEAKERS/MONITORS</li>
<li>STUDIO</li>
<li>DIGITAL AUDIO PROCESSING</li>
<li>CONSOLES, MIXERS</li>
<li>DAWS/PERIPHERALS</li>
</ul>
</div>
<div class="audio">
<ul class="listl">
<li>DAWS/PERIPHERALS</li>
<li>LOUDSPEAKERS — FOH</li>
<li>RECORDERS/PLAYERS</li>
<li>HEADPHONES</li>
<li>MICROPHONES - WIRELESS CONVERTERS</li>
<li>NETWORK AUDIO / CONTROL / SNAKES</li>
<li>COMPUTER AUDIO INTERFACES</li>
<li>INTERCONNECTS</li>
<li>LOUDSPEAKERS — STAGE MONITORS</li>
<li>ACOUSTIC TREATMENT</li>
<li>MI PRODUCTS</li>
</ul>
</div>
So the final element might be
<li class="last">MI PRODUCTS</li>
I would be a styling issue. If I had no option for client side codes, I would go for CSS styling. You may consider this:
ul.listl li:last-child { }
This would be easier to do with jQuery:
$(function(){
$("ul.listl li:last").addClass("last");
});
And that's all, folks :)
I agree with #JohnHartsock but if you want to do it your way you can use a variety of libraries which help you in querying html elements (DOM).
Fizzler
Sharp-Query
HTML Agility Pack
string[] g=#"<div class=""gpbscol"">
<ul class=""listl"">
<li>ACCESSORIES</li>
<li>AMPLIFIERS</li>
<li>ANALOG AUDIO PROCESSING</li>
<li>MICROPHONE PREAMPLIFIERS</li>
<li>MICROPHONES</li>
<li>SPEAKERS/MONITORS</li>
<li>STUDIO</li>
<li>DIGITAL AUDIO PROCESSING</li>
<li>CONSOLES, MIXERS</li>
<li>DAWS/PERIPHERALS</li>
</ul>
</div>
<div class=""audio"">
<ul class=""listl"">
<li>DAWS/PERIPHERALS</li>
<li>LOUDSPEAKERS — FOH</li>
<li>RECORDERS/PLAYERS</li>
<li>HEADPHONES</li>
<li>MICROPHONES - WIRELESS CONVERTERS</li>
<li>NETWORK AUDIO / CONTROL / SNAKES</li>
<li>COMPUTER AUDIO INTERFACES</li>
<li>INTERCONNECTS</li>
<li>LOUDSPEAKERS — STAGE MONITORS</li>
<li>ACOUSTIC TREATMENT</li>
<li>MI PRODUCTS</li>
</ul>
</div>".Split(new string[]{"<li>"});
g[g.length-1]=g[g.length-1].replace("<li>","<li class='last' >");
string newString=String.Join("", g);
The easiest of all, this is using HtmlAgilityPack;
TextWriter text = new StringWriter();
string set = [html here];
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(set);
HtmlNode node = doc.DocumentNode.SelectSingleNode("//li[last()]");
HtmlAttribute attr;
attr = node.SetAttributeValue("class", "last");
doc.Save(text);
return text;
anyways thanks all for helping me.
Related
I am trying to get all links that contain ids.I have tried for name and price which is working perfectly but not able to get links which is related to that stuff.
For name I am using this code but for getting links it is not working.
//For Name
var name=scorenodesdoc.DocumentNode.SelectNodes("//[contains(#id,'item')]/ul[1]/li1]/span");
//for Links
var Links= doc.DocumentNode.SelectNodes("//a[contains(#id, 'item')]/#href");
xpath for link is://*[#id="item5d86882c07"]/div[1]/div/a
//This is the code I am try to get the href link
<li id="item5d86882c07" _sp="p2045573.m1686.l8" listingid="401689029639" class="sresult lvresult clearfix li" r="1">
<div class="lvpic pic img left" iid="401689029639">
<div class="lvpicinner full-width picW">
<a href="https://www.ebay.com/itm/Microsoft-Xbox-One-X-White-Console-1TB-Forza-Special-Edition-Bundle-White/401689029639?hash=item5d86882c07:g:lgwAAOSwoZJcQY5s" class="img imgWr2">
<img src="https://i.ebayimg.com/thumbs/images/g/lgwAAOSwoZJcQY5s/s-l225.jpg" class="img" alt="Microsoft Xbox One X White Console 1TB & Forza Special Edition Bundle - White'">
</a>
</div>
</div>
</li>
Ok this is how I resolve my issue.First it gets get anchor tag information then by using getattributevalue to get value of href.
var URLnodes = doc.DocumentNode.SelectNodes("//*[contains(#id,'item')]/div[1]/div/a");
var AllURL = URLnodes.Select(node => node.GetAttributeValue("href",null));
I was testing WebBrowser, but there's no method to getElemments by class, but by tag.
I have something like this.
html:
<div class="Justnames">
<span class="name">Georgia</span>
</div>
So i'd like to get the string "Georgia", which is inside of that span.
I tried:
Var example = Nav.Document.GetElementsByTagName("span");
But it return null, and I've no idea why.
Sorry my english and thanks a lot of the help! :)
This may help:
var elementCollection = default(HtmlElementCollection);
elementCollection = webBrowser1.Document.GetElementsByTagName("span");
foreach (var element in elementCollection)
{
if (element.OuterHtml.Contains("name"))
// we reach here, we get <span class="example"
}
Or:
foreach (var element in elementCollection)
{
if (element.GetAttribute("className") == "name")
// we reach here, we get <span class="example"
}
You can do this using jquery :
var test = $(".name").text();
alert(test):
Because you tagged "C#" and ".net" i assume you have a aspx page and try to access it from server code. To access it from server side you have to add the runat="server" tag to the span:
<span class="name" runat="server">Georgia</span>
Otherwise you only can access it from client side (JavaScript)
I try to get 'Galatasaray.' from following HTML. I don't know how can I specified that
<div class="dRib1">
<h2>Bilgi</h2>
</div>
<div>Galatasaray.</div>
HtmlNodeCollection aciklamaCek = dokuman.DocumentNode.SelectNodes("//div[#class='dRib1']");
textBox8.Text = aciklamaCek[0].NextSibling.NextSibling.InnerText.Trim();
I solved my problem with this code.
Currently I'm trying to read out a text from a Website via a c# program.
To be exact the Track and the Dj from www.hardbase.fm.
This is what the page source looks like:
<div id="Moderator">
<div id="Moderator_special">
<div style="width:158px; float:left; margin:8px"></div>
<div id="onAir" style="width:420px;overflow:hidden;">
<strong>
<a href="/member/46069" target="_top">
<span style="color:#4AA6E5">BIOCORE</span>
</a>
<span style="color:#26628B"> mit "This Is BIOCORE" (Hardstyle)</span>
</strong>
</div>
</div>
</div>
The text I want to read out is "BIOCORE" and "mit "This Is BIOCORE" (Hardstyle)"
(the text seen when running the snippet).
If have tried the following:
System.Net.WebClient wc = new System.Net.WebClient();
byte[] raw = wc.DownloadData("http://www.hardbase.fm/");
first = webData.IndexOf("#4AA6E5\">") + "#4AA6E5\">".Length;
last = webData.LastIndexOf("</span></a><span style=\"color:#26628B\">");
hb_dj = webData.Substring(first, last - first);
But this doesn't always works because sometimes the source code of the page changes a bit. Like the color or so. And then the search wont work.
So the question is: Is there a better method to do this?
You should try the HTML Agility Pack
HtmlWeb page = new HtmlWeb();
HtmlDocument document = page.Load("http://www.hardbase.fm/");
var nodes = document.DocumentNode.SelectNodes("//[#id='onAir']");
var nodes2 = nodes.Select(c1 => c1.SelectNodes("span")).ToList();
var span1=nodes2[0];
var span2 nodes2[1]
I am facing problem in retrieving Subject title of a mail from Unread mails using Selenium webdriver-C#.
Here's the HTML code :
<div class="ae4 UI UJ" gh="tl">
<div class="Cp">
<div>
<table id=":8e" class="F cf zt" cellpadding="0">
<colgroup>
<tbody>
<tr id=":8d" class="zA zE">
<td class="PF xY"></td>
<td id=":8c" class="oZ-x3 xY" style="">
<td class="apU xY">
<td class="WA xY">
<td class="yX xY ">
<td id=":87" class="xY " role="link" tabindex="0">
<div class="xS">
<div class="xT">
<div id=":86" class="yi">
<div class="y6">
**<span id=":85">
<b>hi</b>
</span>**
<span class="y2">
</div>
</div>
</div>
</td>
<td class="yf xY "> </td>
<td class="xW xY ">
</tr>
I am able to print 'emailSenderName' in console but unable to print 'text' (subject line i.e. "hi" in this case) as it is between span tags. Here's my code.
//Try to Retrieve mail Senders name and Subject
IWebElement tbl_UM = d1.FindElement(By.ClassName("Cp")).FindElement(By.ClassName("F"));
IList<IWebElement> tr_ListUM = tbl_UM.FindElements(By.ClassName("zE"));
Console.WriteLine("NUMBER OF ROWS IN THIS TABLE = " + tr_ListUM.Count());
foreach (IWebElement trElement in tr_ListUM)
{
IList<IWebElement> td_ListUM = trElement.FindElements(By.TagName("td"));
Console.WriteLine("NUMBER OF COLUMNS=" + td_ListUM.Count());
string emailSenderName = td_ListUM[4].FindElement(By.ClassName("yW")).FindElement(By.ClassName("zF")).GetAttribute("name");
Console.WriteLine(emailSenderName);
string text = td_ListUM[5].FindElement(By.ClassName("y6")).FindElement(By.TagName("span")).FindElement(By.TagName("b")).Text;
Console.WriteLine(text);
}
I had also tried by directly selecting the Text from tag of 5th Column (td), which contains the subject text (in my case), but no results.
I might went wrong somewhere or may be there is some other way of doing it.
Please suggest, Thanks in advance :)
The 'getText' method available in the Java implementation of Selenium Web Driver seems to do a better job than the equivalent 'Text' property available in C#.
I found a way of achieving the same end which, although somewhat convoluted, works well:
public static string GetInnerHtml(this IWebElement element)
{
var remoteWebDriver = (RemoteWebElement)element;
var javaScriptExecutor = (IJavaScriptExecutor) remoteWebDriver.WrappedDriver;
var innerHtml = javaScriptExecutor.ExecuteScript("return arguments[0].innerHTML;", element).ToString();
return innerHtml;
}
It works by passing an IWebElement as a parameter to some JavaScript executing in the Browser, which treats it just like a normal DOM element. You can then access properties on it such as 'innerHTML'.
I've only tested this in Google Chrome but I see no reason why this shouldn't work in other browsers.
Using GetAttribute("textContent") instead of Text() did the trick for me.
Driver.FindElement(By.CssSelector("ul.list span")).GetAttribute("textContent")
Try this
findElement(By.cssSelector("div.y6>span>b")).getText();
I had the same problem. Worked on PhantomJS. The solution is to get the value using GetAttribute("textContent"):
Driver.FindElementsByXPath("SomexPath").GetAttribute("textContent");
Probably too late but could be helpful for someone.
IWebElement spanText= driver.FindElement(By.XPath("//span[contains(text(), 'TEXT TO LOOK FOR')]"));
spanText.Click();
IWebElement spanParent= driver.FindElement(By.XPath("//span[contains(text(), 'TEXT TO LOOK FOR')]/ancestor::li"));
spanParent.FindElement(By.XPath(".//a[contains(text(), 'SIBLING LINK TEXT')]")).Click();
bonus content here to look for siblings of this text
once the span element is found, look for siblings by starting from parent. I am looking for an anchor link here. The dot at the start of XPath means you start looking from the element spanParent
<li>
<span> TEXT TO LOOK FOR </span>
<a>SIBLING LINK TEXT</a>
</li>
This worked for me in Visual Studio 2017 Unit test project. I'm trying to find the search result from a typeahead control.
IWebElement searchBox = this.WebDriver.FindElement(By.Id("searchEntry"));
searchBox.SendKeys(searchPhrase);
System.Threading.Thread.Sleep(3000);
IList<IWebElement> results = this.WebDriver.FindElements(By.CssSelector(".tt-suggestion.tt-selectable"));
if (results.Count > 1)
{
searchResult = results[1].FindElement(By.TagName("span")).GetAttribute("textContent");
}