• DEVHIDE
        • Home (current)
        • About
        • Contact
        • Cookie
        • Home (current)
        • About
        • Contact
        • Cookie
        • Disclaimer
        • Privacy
        • TOS
        Login Or Sign up

        Xpath return nav/ul/li/a/herf

        112 Views Asked by DanLev At 05 August 2019 at 11:16 2025-12-09T21:14:48.821000

        the website URL: www.tase.co.il

        the website source code:

        <nav class="collapse more_madad_nav in" id="more_madad_nav">
            <ul class="first_level">
                <li>
                    <ul class="second_level">
                        <li class="current"><a href="/he/market_data/security/1158823/major_data"> x </a></li>
                        <li><a href="/he/market_data/security/1158823/graph"> x </a></li>
                        <li><a href="/he/market_data/security/1158823/indices">x</a></li>
                        <li><a href="/he/market_data/security/1158823/statistics"> x</a></li>
                        <li><a href="/he/market_data/security/1158823/historical_data"> x </a></li>
                        <li><a href="/he/market_data/security/1158823/otc"> x </a></li>
                        <li><a href="/he/market_data/security/1158823/short_sale">x</a></li>
                    </ul>
                </li>
                <li><!----><h3> x </h3>
                    <ul class="second_level">
                        <li><a href="/he/market_data/company/2135/about?securityId=1158823">x</a></li>
                        <li><a href="/he/market_data/company/2135/securities?securityId=1158823">x</a></li>
                        <li><!----><a href="/he/market_data/company/2135/reports_maya?securityId=1158823">x</a></li>
                        <li><a href="/he/market_data/company/2135/financial_reports?securityId=1158823">x</a></li>
                        <li><a href="/he/market_data/company/2135/analysis?securityId=1158823">x</a></li>
                    </ul>
                </li>
            </ul>
        </nav>
        

        my C# code for GetHtmlDocument:

        protected HtmlDocument GetHtmlDocument(string sUrl, Encoding oEncoding, bool bSetContentType = true) {
        
            string sPostData = "GET";
            string sReferer = string.Empty;
            List < string > oCookies = new List < string > ();
            string sHtmlContent = string.Empty;
            string sRedirectUrl = string.Empty;
            return GetHtmlDocument(sUrl, sPostData, sReferer, oCookies, ContentType.html, out sRedirectUrl, out sHtmlContent, oEncoding, bSetContentType);
        }
        
        protected HtmlDocument GetHtmlDocument(string sUrl, string sPostData, string sReferer, List < string > oCookies, ContentType eContentType, out string sRedirectUrl, out string sHtmlContent, Encoding oEncoding, bool bSetContentType = true) {
        
            HttpStatusCode oHttpStatusCode = SingleHttpRequest(sUrl, sPostData, sReferer, oCookies, ContentType.html, out sRedirectUrl, out sHtmlContent, oEncoding, true, bSetContentType);
            if (oHttpStatusCode != HttpStatusCode.OK) {
                string sMsg = string.Format("SingleHttpRequest to '{0}' returned '{1}' status", sUrl, oHttpStatusCode);
                LOG.Error(sMsg);
                throw new Exception(sMsg);
            }
            HtmlDocument oHtmlDocument = new HtmlDocument();
            LOG.DebugFormat("sHtmlContent : \n{0}\n , sUrl : {1}", sHtmlContent, sUrl);
            oHtmlDocument.LoadHtml(sHtmlContent);
            return oHtmlDocument;
        }
        
        HtmlDocument oSearchHtmlDocument = GetHtmlDocument(sSearchUrl, false);
        HtmlNode o = oSearchHtmlDocument.DocumentNode.SelectSingleNode("//*[@id='more_madad_nav']//ul[@class='second_level']//li/a[contains(@href,'about')]/@href"); //this is the line!
        

        I'm trying to get the first href from the "second level"(/he/market_data/company/2135/about?securityId=1158823) Thanks for the help

        c# xpath selectsinglenode
        Original Q&A
        2

        There are 2 best solutions below

        2
        Ankur Ankur On 05 August 2019 at 11:25

        Try Query nav/ul/li[1]/ul/li[1]/a/@href

        See Testing result below enter image description here

        2
        KunduK KunduK On 05 August 2019 at 11:42

        Use the following xpath

        //*[@id='more_madad_nav']//ul[@class='second_level']//li/a[contains(@href,'about')]
        

        Demo:

        enter image description here

        UPdate To get the attribute value in C#

        string element  = driver.FindElement(By.Xpath("/*[@id='more_madad_nav']/ul[@class='first_level']//ul[@class='second_level']//li/a[contanins(@href,'about')]")).GetAttribute("href");
        

        Related Questions in C#

        • Passing arguments to main in C using Eclipse
        • kernel module does not print packet info
        • error C2016 (C requires that a struct or union has at least one member) and structs typedefs
        • Drawing with ncurses, sockets and fork
        • How to catch delay-import dll errors (missing dll or symbol) in MinGW(-w64)?
        • Configured TTL for A record(s) backing CNAME records
        • Allocating memory for pointers inside structures in functions
        • Finding articulation point of undirected graph by DFS
        • C first fgets() is being skipped while the second runs
        • C std library don't appear to be linked in object file
        • gcc static library compilation
        • How to do a case-insensitive string comparison?
        • C programming: Create and write 2D array of files as function
        • How to read a file then store to array and then print?
        • Function timeouts in C and thread

        Related Questions in XPATH

        • How can I load all the elements of a webpage with Selenium?
        • Why is the copied XPATH not working for selenium?
        • When I'm typing an Xpath or CSS selector in the console why won't matching results appear while typing? Results only appear after pressing Enter
        • Nokogiri only returning 5 results
        • XPath - how to exclude text from child node
        • xpath issue in nested div
        • Question using XPath to look for a sibling of a td with a certain name
        • How to separate XML tags in freemarker in body function
        • Wait using Path (Puppeteer)
        • why can't I retrieve the track of my Spotify playlist even i have given correct full xpath
        • How do I click the correct link based on text contained in another element using Selenium + Python?
        • PHP DOMDocument ignores first table's closing tag
        • String tokenise an xpath expression
        • Problem to get into the next page, Selenium
        • Scrapy / extracting data across multiple HTML tags

        Related Questions in SELECTSINGLENODE

        • I'm trying to find a XML node by searching for an attribute using a substring
        • Get sibling values based on a single node's value from an XML document
        • Selecting an XML Node
        • XML selectsinglenode how i filter tag?
        • MSXML SelectSingleNode returning all values
        • Parsing through innerHTML with HtmlAgilityPack
        • I'm trying to replace Xpath with jQuery
        • SelectSingleNode() with XPath C# Failure
        • Parsing XML: Colon in my element causes XPath to miss it
        • SelectSingleNode always returns null?
        • How can I get a specific attribute value?
        • The problem with xml parsing using MSXML2: one first node is repeated when ten different nodes should be listed
        • What is wrong with this file or code?
        • Powershell script using xPath .SelectSingleNode not working on extracting values from web.config file xmlns
        • htmlagilitypack selectsinglenode returning null

        Trending Questions

        • UIImageView Frame Doesn't Reflect Constraints
        • Is it possible to use adb commands to click on a view by finding its ID?
        • How to create a new web character symbol recognizable by html/javascript?
        • Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
        • Heap Gives Page Fault
        • Connect ffmpeg to Visual Studio 2008
        • Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
        • How to avoid default initialization of objects in std::vector?
        • second argument of the command line arguments in a format other than char** argv or char* argv[]
        • How to improve efficiency of algorithm which generates next lexicographic permutation?
        • Navigating to the another actvity app getting crash in android
        • How to read the particular message format in android and store in sqlite database?
        • Resetting inventory status after order is cancelled
        • Efficiently compute powers of X in SSE/AVX
        • Insert into an external database using ajax and php : POST 500 (Internal Server Error)

        Popular # Hahtags

        javascript python java c# php android html jquery c++ css ios sql mysql r reactjs

        Popular Questions

        • How do I undo the most recent local commits in Git?
        • How can I remove a specific item from an array in JavaScript?
        • How do I delete a Git branch locally and remotely?
        • Find all files containing a specific text (string) on Linux?
        • How do I revert a Git repository to a previous commit?
        • How do I create an HTML button that acts like a link?
        • How do I check out a remote Git branch?
        • How do I force "git pull" to overwrite local files?
        • How do I list all files of a directory?
        • How to check whether a string contains a substring in JavaScript?
        • How do I redirect to another webpage?
        • How can I iterate over rows in a Pandas DataFrame?
        • How do I convert a String to an int in Java?
        • Does Python have a string 'contains' substring method?
        • How do I check if a string contains a specific word?
        .

        Copyright © 2021 Jogjafile Inc.

        • Disclaimer
        • Privacy
        • TOS
        • Homegardensmart
        • Math
        • Aftereffectstemplates