I was trying this but keep getting the error that gecko doesn't contain a definition for innerHTML..
GeckoElement g2element = null;
g2element = (GeckoElement)mainbrowsersrc.Document.GetElementByTagName("html");
rich1.Text = g2element.InnerHtml; // 48.066
or
rich1.Text = mainbrowsersrc.Document.GetElementsByTagName("html").innerHtml;
If you need the HTML of the entire page, then you should go with
(mainbrowsersrc.Document.DocumentElement as Gecko.DOM.GeckoHtmlHtmlElement)?.InnerHtml;
Please notice that the error that you get is because there is no method .GetElementByTagName(name); - the method is called GetElementsByTagName(name) - plural form.
This is because the tg name is not unique and the method returns a collection of elements with the same tag name - for example a collection of li (list item) elements.
Consequently, if you want to get a particular element by tag name, you should do something like:
string html = mainbrowsersrc.Document.GetElementsByTagName("html").FirstOrDefault().innerHtml;
//or
html = mainbrowsersrc.Document.GetElementsByTagName("html")[0].innerHtml;
Related
i want search spacial value in html code by webbrowser in c#. for example html code<span class="pulser " data-dollari="164.843956376000000" eq_toman="_XcUOV" pulser-change="_OiuVD" pre-dollari="164.964899983000000">$164.97</span>i need Getting the value "164.964899983000000" and another value html code.
If I understand you correctly, you want to get an element from a site and get its attribute values like 'pre-dollari'.
For c#, you can use ScrapySharp , it's a library where you can simulate a webbrowser and scrape its contents. You can use it alongside htmlAgilityPack
to effectively traverse the elements.
So for your case, it could look like this.
// get your Url
Uri url = new Uri("Yoursite.com");
// open up the browser
ScrapingBrowser browser = new ScrapingBrowser();
// navigate to your page
WebPage page = browser.NavigateToPage(url, HttpVerb.Post, "", null);
// find your element, convert to a list and take the first result [0]
HtmlNode node2 = page.Find("span", By.Class("pulser")).ToList()[0];
// and now you can get the attribute by name and put it in a variable
string attributeValue = node2.GetAttributeValue("pre-dollari", "not found");
// attributeValue = 164.964899983000000
I am trying to get a column value from a SharePoint list and populate it to an ASP text box. I used SPQuery to filter and get the value. I even mentioned view fields and increased the List View Lookup Threshold. But when I am trying to assign the text box value with the column field, I am getting this exception:
Argument exception was unhandled by user- value does not fall within the expected range.
Is there any workaround for this? Code sample:
SPQuery qrySeriesDesc = new SPQuery();
qrySeriesDesc.Query = "<Where><Eq><FieldRef Name='Series'/><Value Type='Text'>" + SeriesNumber + "</Value></Eq></Where>";
qrySeriesDesc.ViewFields = "<FieldRef Name='Series Description'/>";
SPListItemCollection itemCol = list.GetItems(qrySeriesDesc);
foreach (SPListItem item in itemCol)
{
if (item != null)
{
tboxSeriesDescription.Text = item["Series Description"].ToString();
}
}
I am getting the mentioned exception at:
tboxSeriesDescription.Text = item["Series Description"].ToString();
Try to get it from field, not from item:
SPField fieldSerDesc = item.Fields.GetFieldByInternalName("SeriesDescription"); //internal name of your fields. Usually eq StaticName.
tboxSeriesDescription.Text = item[fieldSerDesc.Id].ToString();
Or, if your field is lookup for example, you can do it like this:
SPFieldLookup fieldSerDesc = (SPFieldLookup)item.Fields.GetFieldByInternalName("SeriesDescription");
tboxSeriesDescription.Text = fieldSerDesc.GetFieldValueAsText(item[fieldSerDesc.Id]);
}
You get the error because the field do not exist or is misspelled.
Please note that if you select a column that does not exist SharePoint does not raise any error.
Try to check the field's name using a tool like Sharepoint Manager and use ALWAYS the internal name
I have an XML document which contains multiple properties and each property has a summary description node. The summary description nodes sometimes contain html tags that I'd like to be used to actually create a line break on the page, but the tags are currently being displayed as text. I'm currently using XDocument in the contr to grab the value of the summaryDescription node and then passing this to the view via a view model.
XML
<summaryDescription>
A very spacious first floor four bedroom apartment situated in a convenient location. The apartment has a very impressive accommodation comprising main entrance foyer leading to hall, formal lounge, well appointed kitchen breakfast area and TV common room, family bathroom and en-suite shower room.<br/><br/>
</summaryDescription>
Controller
var query = (from props in xml.Descendants("property")
select new PropertyResult
{
Description = props.Element("summaryDescription").Value,
PriceText = props.Element("pricetext").Value,
Image = "http://www.dezrez.com/estate-agent-software/ImageResizeHandler.do?&photoID=1&AgentID=1239&BranchID=1976&Width=500&PropertyId=",
}).ToArray();
return View(new ResultsViewModel { Property = query });
As you can see the summaryDescription xml node has two line breaks at the end but these are currently being displayed as text on the page. If anyone could help me with forcing the page to see the tags as html tags, I'd be very grateful.
Thanks in advance!
In your razor view markup, use this Extension method, on your property.
public static IHtmlString EscapeHTML(this HtmlHelper htmlHelper, string value)
{
return new HtmlString(value.ToString());
}
#Html.EscapeHTML(Model.Property)
This may be a simple question for selenium users:
I know some of the attributes that we could use while finding an element like: Name, TagName, Css etc etc..
But can we use something like "link=-----" in c# for element finding based on that attribute??
Not familiar with Selenium IDE, here I assume link=601-800 students means something like <a href='something'>601-800 students</a>.
Then you can use By.XPath to locate the link with its text, or use By.LinkText, even By.PartialLinkText.
driver.FindElement(By.XPath("//a[text()='601-800 students']"));
//driver.FindElement(By.LinkText("601-800 students"));
EDIT:
If you have some links with the same text, try identify the unique ancestors.
E.g.
var headLink = driver.FindElement(By.XPath("//*[#id='header']//a[text()='601-800 students']"));
var mainLink = driver.FindElement(By.XPath("//*[#id='main']//a[text()='601-800 students']"));
If that's not possible, get them together by FindElements (note this is not FindElement), them index them.
IList<IWebElement> links = driver.FindElements(By.XPath("//a[text()='601-800 students']"));
//IList<IWebElement> links = driver.FindElements(By.LinkText("601-800 students"));
var firstLink = links[0];
var secondLink = links[1];
foreach(IWebElement link in links) {
// stuff to do with link
}
I want to scrape a Wiki page. Specifically, this one.
My app will allow users to enter the registration number of the vehicle (for example, SBS8988Z) and it will display the related information (which is on the page itself).
For example, if the user enters SBS8988Z into a text field in my application, it should look for the line on that wiki page
SBS8988Z (SLBP 192/194*) - F&N NutriSoy Fresh Milk: Singapore's No. 1 Soya Milk! (2nd Gen)
and return SBS8988Z (SLBP 192/194*) - F&N NutriSoy Fresh Milk: Singapore's No. 1 Soya Milk! (2nd Gen).
My code so far is (copied and edited from various websites)...
WebClient getdeployment = new WebClient();
string url = "http://sgwiki.com/wiki/Scania_K230UB_(Batch_1_Euro_V)";
getdeployment.Headers["User-Agent"] = "NextBusApp/GetBusData UserAgent";
string sgwikiresult = getdeployment.DownloadString(url); // <<< EXCEPTION
MessageBox.Show(sgwikiresult); //for debugging only!
HtmlAgilityPack.HtmlDocument sgwikihtml = new HtmlAgilityPack.HtmlDocument();
sgwikihtml.Load(new StreamReader(sgwikiresult));
HtmlNode root = sgwikihtml.DocumentNode;
List<string> anchorTags = new List<string>();
foreach(HtmlNode deployment in root.SelectNodes("SBS8988Z"))
{
string att = deployment.OuterHtml;
anchorTags.Add(att);
}
However, I am getting a an ArgumentException was unhandled - Illegal Characters in path.
What is wrong with the code? Is there an easier way to do this? I'm using HtmlAgilityPack but if there is a better solution, I'd be glad to comply.
What's wrong with the code? To be blunt, everything. :P
The page is not formatted in the way you are reading it. You can't hope to get the desired contents that way.
The contents of the page (the part we're interested in) looks something like this:
<h2>
<span id="Deployments" class="mw-headline">Deployments</span>
</h2>
<p>
<!-- ... -->
<b>SBS8987B</b>
(SLBP 192/194*)
<br>
<b>SBS8988Z</b>
(SLBP 192/194*) - F&N NutriSoy Fresh Milk: Singapore's No. 1 Soya Milk! (2nd Gen)
<br>
<b>SBS8989X</b>
(SLBP SP)
<br>
<!-- ... -->
</p>
Basically we need to find the b elements that contain the registration number we are looking for. Once we find that element, get the text and put it together to form the result. Here it is in code:
static string GetVehicleInfo(string reg)
{
var url = "http://sgwiki.com/wiki/Scania_K230UB_%28Batch_1_Euro_V%29";
// HtmlWeb is a helper class to get pages from the web
var web = new HtmlAgilityPack.HtmlWeb();
// Create an HtmlDocument from the contents found at given url
var doc = web.Load(url);
// Create an XPath to find the `b` elements which contain the registration numbers
var xpath = "//h2[span/#id='Deployments']" // find the `h2` element that has a span with the id, 'Deployments' (the header)
+ "/following-sibling::p[1]" // move to the first `p` element (where the actual content is in) after the header
+ "/b"; // select the `b` elements
// Get the elements from the specified XPath
var deployments = doc.DocumentNode.SelectNodes(xpath);
// Create a LINQ query to find the requested registration number and generate a result
var query =
from b in deployments // from the list of registration numbers
where b.InnerText == reg // find the registration we're looking for
select reg + b.NextSibling.InnerText; // and create the result combining the registration number with the description (the text following the `b` element)
// The query should yield exactly one result (or we have a problem) or none (null)
var content = query.SingleOrDefault();
// Decode the content (to convert stuff like "&" to "&")
var decoded = System.Net.WebUtility.HtmlDecode(content);
return decoded;
}