2

I want to use selenium to click on the "?currentPage=3" for example, on the website itself the button you click redirects to the href, however when I use selenium, nothing happens. I have tried so many iterations and also looked up similar guides, yet to no avail.

<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: </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>
                                        <li>
                        <a href="?currentPage=11">1.3. Ratsionaalavaldise lihtsustamine</a>
                    </li>
Chris
  • 17
  • 4

1 Answers1

2

To Click() on the element you have to induce WebDriverWait for the ElementToBeClickable() and you can use either of the following Locator Strategies:

  • LinkText:

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.LinkText("Tööraamatu kasutajale"))).Click();
    
  • CssSelector:

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("li > a[href='?currentPage=3']"))).Click();
    
  • XPath:

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//li/a[@href='?currentPage=3' and text()='Tööraamatu kasutajale']"))).Click();
    

Update

Possibly you need SeleniumExtras.WaitHelpers and you can use either of the following solutions:

  • LinkText:

    new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.LinkText("Tööraamatu kasutajale"))).Click();
    
  • CssSelector:

    new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.CssSelector("li > a[href='?currentPage=3']"))).Click();
    
  • XPath:

    new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.XPath("//li/a[@href='?currentPage=3' and text()='Tööraamatu kasutajale']"))).Click();
    

References

You can find a couple of relevant detailed discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • the ExpectedConditions line is giving me an error The name 'ExpectedConditions' does not exist in the current context I am using "using openQA.Selenium.Support.IO;" and "using openQA.Selenium.Support.IO.ExpectedConditions;" apparently does not exist anymore or is deprecated – Chris Sep 20 '20 at 18:59
  • @Chris Checkout the updated answer and ;et me know the status. – undetected Selenium Sep 20 '20 at 19:20
  • Firstly, i want to thank you for replying even after a answer, i am new to stackoverflow and so far it is so amazing to get in realtime guidance or points in the right direction, i even leave you a like if i had more reputation. however even now, the same error seems to be happening. I also tried all 3 versions, CSS, xpath and linktext – Chris Sep 20 '20 at 19:30
  • NoSuchElementException: no such element: Unable to locate element: {"method":"link text","selector":"Tööraamatu kasutajale"} (Session info: chrome=85.0.4183.102) – Chris Sep 20 '20 at 19:30
  • @Chris Try the other two locators. – undetected Selenium Sep 20 '20 at 19:32
  • yeah, my bad i edited it a little bit late, i already did, none of them seem to be working – Chris Sep 20 '20 at 19:36
  • Each one essentially gives the same error of Unable to locate element – Chris Sep 20 '20 at 19:37
  • @Chris To address **NoSuchElementException** check [this](https://stackoverflow.com/questions/47993443/selenium-selenium-common-exceptions-nosuchelementexception-when-using-chrome/47995294#47995294) and [this](https://stackoverflow.com/questions/48471321/nosuchelementexception-selenium-unable-to-locate-element/48472940#48472940) discussion. – undetected Selenium Sep 21 '20 at 21:48