• TechQA.

        Xpath return nav/ul/li/a/herf

        112 views Asked by DanLev At 2019-08-05T11:16:04+00:00 05 August 2019 at 11:16 2025-12-09T21:14:48+00:00

        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 answers

        2
        Ankur Ankur On 2019-08-05T11:25:52+00:00 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 2019-08-05T11:42:54+00:00 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#

        • How to call a C language function from x86 assembly code?
        • What does: "char *argv[]" mean?
        • User input sanitization program, which takes a specific amount of arguments and passes the execution to a bash script
        • How to crop a BMP image in half using C
        • How can I get the difference in minutes between two dates and hours?
        • Why will this code compile although it defines two variables with the same name?
        • Compiling eBPF program in Docker fails due to missing '__u64' type
        • Why can't I use the file pointer after the first read attempt fails?
        • #include Header files in C with definition too
        • OpenCV2 on CLion
        • What is causing the store latency in this program?
        • How to refer to the filepath of test data in test sourcecode?
        • 9 Digit Addresses in Hexadecimal System in MacOS
        • My server TCP doesn't receive messages from the client in C
        • Printing the characters obtained from the array s using printf?

        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

        • PowerShell, how to select an xml node
        • HtmlAgilityPack SelectSingleNode() to the third instance of an h3 heading
        • SelectSingleNode with each node having different namespace
        • Looping a node collection gives me unique nodes but selecting nodes inside from these give me the results of the first loop item
        • SelectSingleNode vs dot reference
        • SelectSingleNode relative to root node on passed XML
        • Hadoop Nodemanager not working in windows
        • add child node to parent xml with specfic innertext vb.net
        • SelectSingleNode and InnerText in XML file
        • Select sibling node value based on multiple sibling conditions
        • The problem with xml parsing using MSXML2: one first node is repeated when ten different nodes should be listed
        • Select Single Node of XML with Powershell
        • Powershell script using xPath .SelectSingleNode not working on extracting values from web.config file xmlns
        • When I use SelectSingleNode Method with my XmlElement It Still Reads the Whole Document and Not Inside My Element
        • Obtain the value of one XML attribute using a different attribute as a key in Powershell

        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?

        Popular Tags

        javascript python java c# php android html jquery c++ css ios sql mysql r reactjs node.js arrays c asp.net json

        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)
        • Privacy
        • Terms
        • Cookies
        • Homegardensmart
        • Math
        • Aftereffectstemplates