Chris
Chris

Reputation: 17

Selenium to click a link in href which in <li>

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>

Upvotes: 2

Views: 660

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193078

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:

Upvotes: 2

Related Questions