I am trying to fetch an element using HTML Agility Pack (C#) but it keeps returning null.
The HTML looks like this (sorry for the image).
I can successfully find the first node I marked with the arrow using the following xpath:
var node = htmlDoc.DocumentNode.SelectSingleNode("//div[#id='listing-container']");
But it says this node only has one "text" child while in the HTML it clearly has other div I need to access (as in the image).
How can I access this node with class = c-listing?
Does not seem to be in a frame or anything like that.
Thank for you help, I am not experienced in this field.
Cheers
Related
I have a few elements inside this kind of tag as below:
enter image description here
How do I locate these kinds of elements
I tried to locate the foresaid element, which the Selenium C# is not working
You can use xpath to locate the p element:
//div[#data-testid='patientLink']/p
Later you can call the getText() of the element located by the above selector.
give the p tag you are looking for an id. Then lookup the element using
var driver = //whatever your driver is;
driver.FindElement(By.Id("element-id-goes-here"));
I am supposed to click the link (highlighted blue, refer to image above), but LinkText does not work. I also tried using CSSSelector and XPath, but both:
_driver.FindElement(By.CSSSelector("HH3"]")).Click();
_driver.FindElement(By.XPath("div[text()="HH3"]")).Click();
doesn't work. Help?
Your tried locator looks incorrect, you should try using xpath with text to locate desire element as below :-
_driver.FindElement(By.XPath(".//div[text()='HH3']")).Click();
Try _driver.FindElement(By.XPath("//div[contains(text()='HH3')]")).Click();
I am new to Selenium and looking forward to learn more, I am using Selenium WebDriver with C#.
This is how I initiate a WebElement:
CarouselSliderNextButton = DriverInitializer.driver
.FindElement(By.XPath("//a[#class='buttons next']"));
But if the element doesn't exist for some reason; hidden for instance, then it doesn't work. I know that I can check if the element exists before I can initiate, but would love to hear from experts if I am doing this the right way.
I am not claiming to be an expert, but this is what I think:
You do not "initiate" a web element
You find a web element
First of all, you need to find the element you want to locate from html file. You may use Google or Firefox in developer mode to find it. I recommend you to install "Firebug" for Firefox, it is very useful.
There are multiple reasons why an element you can "see" from HTML file but you can not locate:
1: this element is on an iframe, this case requires you to locate this iframe first then locate the element
2: this element is not visible yet, for example, a drop down arrow button will only appear if you hover your mouse over a certain area first.
But you are heading to the right direction.
If you're trying to intialise the WebElement, I use:
WebElement element = driver.findElement(By.tagName("div"));
as most DOMs have div tags.
Then after trying to find an element that exists (and isn't a div tag), check:
if (element.getTagName().equals("div")){
System.out.println("Element not found...");
}
var instantEstimateDiv: WebElement? = null => Kotlin
WebElement? instantEstimateDiv= null; => JAVA
I'm trying to get to this element: //*[#id="table-matches"]/table on this page: http://www.oddsportal.com/matches/soccer/20140221/
I want to get the table that contains matches. Table starts under Kick off time tab. The element I'm looking for is 'table class=" table-main"' and it is inside the element 'div id="table-matches" style="display: block;"'
I tried getting this document with HtmlAgilityPack in C# and I can find 'div' element, but it says that it doesn't have any child nodes (there should be a table child node). If I try to get the table, the result is null. Here is the code:
var webGet = new HtmlWeb();
var document = webGet.Load("http://www.oddsportal.com/matches/soccer/20140221/");
var div = document.DocumentNode.SelectNodes("//div[#id='table-matches']");
var table = document.DocumentNode.SelectNodes("//*[#id='table-matches']/table");
var table2 = document.DocumentNode.SelectNodes("//table");
So, div variable contains the div element (but it has no child nodes), table variable is null, even table2 variable contains 4 elements, but none of them are desired table.
I figured there is a problem with HtmlAgilityPack and tried to get the whole web page with Python. So I got the whole HTML document in a text file and searched the text file and I can find div element but it is empty. There is no table element inside. Why is that? Why can I see table element in chrome or internet explorer, but when I download html there is no such element?
Here is the python code:
url = urllib.urlopen("http://www.oddsportal.com/matches/")
document = url.read()
htmlOddsPortal = open("htmlOddsPortal.txt", "w")
htmlOddsPortal.write(document)
Here is the element in the final text document:
<div id="table-matches"></div> <!-- END PAGE BODY -->
Table is loaded with JavaScript (probably with AJAX) so you won't get it with webGet.Load(). You only get HTML that server returns in response.
You can check this if you (in Chrome) open Console (F12), click on Settings and check Disable JavaScript, then refresh page. You will see blank content.
I had same problem, but I worked in java, and I have used HTMLUnit to solve this. Probably there is similar tool for C#, or you can check if HtmlAgilityPack is able to do asynchronous call or something like WebBrowser component.
How can I iterate over HTML nodes of a web page and get the CSS Text of each node in it? I need something like what Firebug is doing, if you click on a Node, it gives you complete list of all CSS Texts associated with that Node (even inherited styles).
My main problem is not actually iterating over HTML nodes. I am doing it with Html Agility Pack library. I just need to get complete CSS for each node.
p.s. I am sorry, I should have explained that I want to do this in C# (not javascript)
I found the following code snippet useful for all element in the page and 'CurrentStyle' property of them shows their computed style:
HTMLDocument doc = (HTMLDocument)axWebBrowser1.Document;
var body = (HTMLBody)doc.body;//current style
var childs = (IHTMLDOMChildrenCollection)body.childNodes;
var currentelementType = (HTMLBody)childs.item(0);
var width = currentelementType.currentStyle.width;
Note that according to my prev post axWebBrowser1 is a WebBrowser control.
If you want the current styles for an element, look into getComputedStyle(), but if you want the inheritance too then you may have to implement the style cascade. Firebug does quite a lot of work behind the scenes to generate what you see!
You can get the CSS text from the style attribute like this:
node.getAttribute('style')
Or if you want style you can iterate through the keys and values in
node.style
If you want to grab the entire computed style of the element and not just the CSS applied in the style attribute, read this article on computed and cascaded styles.
You can use WebBrowser control in C# to access the htm document object and cast its body tag as following:
HTMLDocument doc = (HTMLDocument)axWebBrowser1.Document;
var body = (HTMLBody)doc.body;
But before that you should add com refrence: MSHTML to you project.
here you could access body.currentStyle that show you all its styles that might be css or inline styles.
You can try for (property in objName) operator as seen here.
I'm not sure if you can simply get "all" CSS properties using JavaScript to be honest, you could look into the [DOMNode].currentStyle, [DOMNode].style and document.defaultView.getComputedStyle thingamajiggy's. They should contain the 'current' style they had. What you could then do is have an array of all CSS properties you want to test and simply loop them through a function of your own that gets the CSS property for everything using forementioned methods (depending on which browser). I usually attempt the DOMNode.style[property] first as this is "inline" javascript and always rules over everything, then I sniff if the browser uses the .currentStyle method or .getComputedStyle and use the correct one.
It's not perfect and you might need to clean up some things (height: auto; to the actual current height, some browsers might return RGB colours instead of HEX) etc.
So, yes, I don't know of anything prefab that you can use in Javascript.