0

ERROR ON "var links = driver.FindElement(By.XPath("/html/body/div[2]/div[1]/div/div/div/div[1]/div/nav/ol/li[3]"));"

I am having trouble clicking a link with selenium that is in a li tag(var links), although it is clickable via hand. I have tried numerous methods, but have yet to find one that works; scouring through stackoverflows already existing problems, has also not been of help.

This is the error I get, no matter what selector type I use OpenQA.Selenium.NoSuchElementException: 'no such element: Unable to locate element

MY CODE

using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Interactions;

    namespace BookFetcher
    {
        public static class Program
        {
            public static void Main()
            {
    
    
                IWebDriver driver = new ChromeDriver(@"C:\");
                driver.Manage().Window.Maximize();
                driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
                driver.Navigate().GoToUrl("http://xn--epik-0qa.ee/minu-konto/");
                driver.FindElement(By.XPath("//*[@id='username']")).SendKeys("");
                driver.FindElement(By.XPath("//*[@id='password']")).SendKeys("");
                driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
                driver.FindElement(By.XPath("/html/body/div[2]/div[1]/div[1]/div/div/div/div/div[1]/form/p[3]/button")).Click();
                driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
                driver.FindElement(By.XPath("/html/body/header/div/div/nav[2]/ul/li[2]/a")).Click();
                driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
                var element = driver.FindElement(By.XPath("/html/body/div[3]/div[1]/section[1]/div[3]/div[94]/p[2]/a"));
                Actions actions = new Actions(driver);
                actions.MoveToElement(element);
                actions.Perform();
                driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
                element.Click();
                driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
                var links = driver.FindElement(By.XPath("/html/body/div[2]/div[1]/div/div/div/div[1]/div/nav/ol/li[3]"));;
                driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
                links.Click();
                Console.ReadLine();
            }
        }
    }

HTML SOURCE

<div class="content-area">
        <div class="epub">
            <div class="column epub-nav">
                <div class="epub-nav-inner">
                    <div class="epub-meta">
                        <p>uid: 978-9949-559-55-8</p><p>creator: </p><p>publisher: Maurus</p><p>subject: </p>                   </div>
                                <nav epub:type="toc" id="toc">
                <ol>
                                        <li>
                        <a href="?currentPage=3">Tööraamatu kasutajale</a>
                    </li>
                                        <li>
                        <a href="?currentPage=4">1. Arvuhulgad ja avaldised</a>
                    </li>
                                        <li>
                        <a href="?currentPage=4">1.1. Arvuhulgad</a>
                    </li>
                                        <li>
                        <a href="?currentPage=7">1.2. Tehted astmete ja juurtega</a>
                    </li>

I already asked for help once, however the solutions provided did not seem to fix the issue, so I thought I would give it a go again, in hopes of someone helping me.

Chris
  • 17
  • 4

1 Answers1

3

Maybe there is an iframe.

using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Interactions;

namespace BookFetcher
{
    public static class Program
    {
        public static void Main()
        {


            IWebDriver driver = new ChromeDriver(@"C:\");
            driver.Manage().Window.Maximize();
            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
            driver.Navigate().GoToUrl("http://xn--epik-0qa.ee/minu-konto/");
            driver.FindElement(By.XPath("//*[@id='username']")).SendKeys("");
            driver.FindElement(By.XPath("//*[@id='password']")).SendKeys("");
            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
            driver.FindElement(By.XPath("/html/body/div[2]/div[1]/div[1]/div/div/div/div/div[1]/form/p[3]/button")).Click();
            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
            driver.FindElement(By.XPath("/html/body/header/div/div/nav[2]/ul/li[2]/a")).Click();
            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
            var element = driver.FindElement(By.XPath("/html/body/div[3]/div[1]/section[1]/div[3]/div[94]/p[2]/a"));
            Actions actions = new Actions(driver);
            actions.MoveToElement(element);
            actions.Perform();
            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
            element.Click();
            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
            var iFrame = driver.FindElement(By.Xpath("//iframe"));
            driver.SwitchTo().Frame(iFrame);
            var links = driver.FindElement(By.XPath("//a[@href='?currentPage=4']"));
            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
            links.Click();
            Console.ReadLine();
        }
    }
}
Jortega
  • 3,616
  • 1
  • 18
  • 21
  • OpenQA.Selenium.NoSuchElementException: 'no such element: Unable to locate element: {"method":"xpath","selector":"//a[@href='?currentPage=4']"} (Session info: chrome=85.0.4183.102)' – Chris Sep 21 '20 at 21:18
  • @Chris can you add a link to the previous question you mentioned so I can see what was suggested before that also did not work. – Jortega Sep 21 '20 at 21:22
  • https://stackoverflow.com/questions/63982298/selenium-to-click-a-link-in-href-which-in-li Also thank you so much for actively helping, much love! – Chris Sep 21 '20 at 21:26
  • @Jortega Possibly OP needs `SeleniumExtras.WaitHelpers.` – undetected Selenium Sep 21 '20 at 21:37
  • @DebanjanB your answer should have worked. I am thinking there is something else going on here but without being able to get to the actual page I can only guess. – Jortega Sep 21 '20 at 21:43
  • @Jortega possibly an ` – undetected Selenium Sep 21 '20 at 21:49
  • I am not even quite sure what where this is being scraped from to be quite honest with you, some of this information posted, does not seem to be anywhere on the page i am looking at. – Chris Sep 21 '20 at 21:55
  • @Chris updated the answer to switch to an iframe since the text you are looking for was missing from the print statements. – Jortega Sep 21 '20 at 22:01
  • Severity Code Description Project File Line Suppression State Error CS1061 'IWebDriver' does not contain a definition for 'Instance' and no accessible extension method 'Instance' accepting a first argument of type 'IWebDriver' could be found (are you missing a using directive or an assembly reference?) bookfetcher C:\Users\C\source\repos\bookfetcher\bookfetcher\Program.cs 38 Active – Chris Sep 21 '20 at 22:09
  • I feel so stupid having to do this, but it seems like Instance is not apart of OpenQA.Selenium, i tried looking up maybe that i am missing a assembly refrence, but it doesnt seem to be the case, what am i missing. Once again thank you so much for the help! – Chris Sep 21 '20 at 22:09
  • @Chris updated the answer to switch a different way. `driver.SwitchTo().Frame(` – Jortega Sep 21 '20 at 22:18
  • OpenQA.Selenium.NoSuchElementException: 'no such element: Unable to locate element: {"method":"xpath","selector":"//iframe"} (Session info: chrome=85.0.4183.121)' – Chris Sep 22 '20 at 07:16
  • I almost feel bad at this, point asking. I have tried messing around with the code on my own with the directions you guys pointed me towards, but still I have yet to find something that works – Chris Sep 22 '20 at 07:17
  • https://paste.ee/p/SwfU5; i put the entire source code of the webpage in a pastebin, so if anybody still cares to help me, they can get a better view. Inded IFRAME is being used, however i dont know how i would select it to get to the real content that i want – Chris Sep 22 '20 at 07:21