How do I get web browser element's css style? - c#

I have a page like the below in my web browser that I want to get the style atributte value. I tried:
HtmlElement ele = webBrowser1.Document.GetElementById("foo");
MessageBox.Show(ele.GetAttribute("style"));
but it output:
System.__ComObject
Why does it output a System.__ComObject type and how do I handle it?
HTML page:
<div id="foo" style="display:block;">
a
</div>

ele.Style
Will help.
ele.GetAttribute("Style")
won't work because returns string, so it can't say more than that is an object, while ele.Style returns CssStyleCollection.

var e = document.getElementById('foo');
var css = window.getComputedStyle(e,null).getPropertyValue("display");
alert(css);

Related

Problems with using .InnerHtml in ASP.NET c #

I have a div in the aspx page which i gave the attribute runat = "server" and id = "content".
When starting the page I need to create some cards with data taken from a database. But it gives me error when I try from code behind in c # using content.InnerHtml to add html code.
aspx page
<div class="page-wrapper">
<div class="page-content" runat="server" id="contenuto">
</div>
</div>
code behind
contenuto.InnerHtml += "<div class='row'>";
it gives me this error: "Could not get the internal content of content because it is not in literal format."
image of the Error
"contenuto" not containing any server control inside it, you required to change your code like below -
var sb = new StringBuilder();
String innertext =sb.Append("Text what you required or tag");
contenuto.InnerHtml = innertext ;

Write someting in a <input> tag in html with selenium c# doesn't work

I wanted to fill in a input from website with html.
THis how the part of the website (https://account.protonmail.com/signup?language=de) looks like:
<div class="flex-item-fluid">
<input autocomplete="username" autocapitalize="off" autocorrect="off" spellcheck="false" aria-invalid="false" id="username" aria-describedby="id-1" class="w100 inputform-field" value="">
</div>
This my c# selenium part:
var elem = driver.FindElement(By.XPath("//*[#id='username']")).SendKeys("test");
// I also tried this:
var elem = driver.FindElement(By.XPath("//input[#id='username']")).SendKeys("test");
I hope somebody can help me.
The username field is in iframe
use the below xpath to switch to iframe first then you can continue with the rest of the code:
//iframe[#src]
something like this :
IWebElement iframe = driver.FindElement(By.Xpath("//iframe[#src]"));
driver.SwitchTo().Frame(iframe);
Also, I see multiple elements for username field. Always remember we should give more precedence to css_selector over xpath
Username field can be locate with this css:
body.color-norm.bg-norm input[id='username']
so in code something like this :
var elem = driver.FindElement(By.CssSelector("body.color-norm.bg-norm input[id='username']")).SendKeys("test");
var pass = driver.FindElement(By.CssSelector("input[id='password']")).SendKeys("password here");
The element you are looking resides in an iframe so first switch the frame.
xPath for iframe:
//iframe[contains(#src,'Name=username')]
Then find the element using below xPath,
//input[#id='username']
Completed code:
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[contains(#src,'Name=username')]")));
var elem = driver.FindElement(By.XPath("//*[#id='username']")).SendKeys("test");
you can try this
var elem = driver.FindElement(By.CssSelector(".w100.inputform-field")).SendKeys("")
Here we search by class name, not by input name. Good Luck.
if it worked for you. USEFUL

How could I get the text is inside of a span which has no ID but a class

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)

get value from web page using Html Agility Pack

I am trying to get the value of the "Pool Hashrate" using the HTML Agility Pack. Right when I hit my string hash, I get Object reference not set to an instance of an object. Can somebody tell me what I am doing wrong?
string url = http://p2pool.org/ltcstats.php?address
protected void Page_Load(string address)
{
string url = address;
HtmlWeb web = new HtmlWeb();
HtmlDocument doc = web.Load(Url);
string hash = doc.DocumentNode.SelectNodes("/html/body/div/center/div/table/tbody/tr[1]")[0].InnerText;
}
Assuming you're trying to access that url, of course it should fail. That url doesn't return a full document, but just a fragment of html. There is no html tag, there is no body tag, just the div. Your xpath query returns nothing and thus the null reference exception. You need to query the right thing.
When I access that url, it returns this:
<div>
<center>
<div style="margin-right: 20px;">
<h3>Personal LTC Stats</h3>
<table class='zebra-striped'>
<tr><td>Pool Hashrate: </td><td>66.896 Mh/s</td></tr>
<tr><td>Your Hashrate: </td><td>0 Mh/s</td></tr>
<tr><td>Estimated Payout: </td><td> LTC</td></tr>
</table>
</div>
</center>
</div>
Given this, if you wanted to get the Pool Hashrate, you'd use a query more like this:
/div/center/div/table/tr[1]/td[2]
In the end you need to do this:
var url = "http://p2pool.org/ltcstats.php?address";
var web = new HtmlWeb();
var doc = web.Load(url);
var xpath = "/div/center/div/table/tr[1]/td[2]";
var poolHashrate = doc.DocumentNode.SelectSingleNode(xpath);
if (poolHashrate != null)
{
var hash = poolHashrate.InnerText;
// do stuff with hash
}
The problem is that xpath is not finding the specified node. You can specify an id to the table or the tr in order to have a smaller xpath
Also, based on your code I assume that you're looking for a single node only, so you may want to use something like this
doc.DocumentNode.SelectSingleNode("xpath");
Another good option is using Fizzler

C# Html Agility Pack ( SelectSingleNode )

I'm trying to parse this field, but can't get it to work. Current attempt:
var name = doc.DocumentNode.SelectSingleNode("//*[#id='my_name']").InnerHtml;
<h1 class="bla" id="my_name">namehere</h1>
Error: Object reference not set to an instance of an object.
Appreciate any help.
#John - I can assure that the HTML is correctly loaded. I am trying to read my facebook name for learning purposes. Here is a screenshot from the Firebug plugin. The version i am using is 1.4.0.
http://i54.tinypic.com/kn3wo.jpg
I guess the problem is that profile_name is a child node or something, that's why I'm not able to read it?
The reason your code doesn't work is because there is JavaScript on the page that is actually writing out the <h1 id='profile_name'> tag, so if you're requesting the page from a User Agent (or via AJAX) that doesn't execute JavaScript then you won't find the element.
I was able to get my own name using the following selector:
string name =
doc.DocumentNode.SelectSingleNode("//a[#id='navAccountName']").InnerText;
Try this:
var name = doc.DocumentNode.SelectSingleNode("//#id='my_name'").InnerHtml;
HtmlAgilityPack.HtmlNode name = doc.DocumentNode.SelectSingleNode("//h1[#id='my_name']").InnerText;
public async Task<List<string>> GetAllTagLinkContent(string content)
{
string html = string.Format("<html><head></head><body>{0}</body></html>", content);
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
var nodes = doc.DocumentNode.SelectNodes("//[#id='my_name']");
return nodes.ToList().ConvertAll(r => r.InnerText).Select(j => j).ToList();
}
It's ok with ("//a[#href]"); You can try it as above.Hope helpful

Categories