硒单击href中<li>中的链接

时间:2020-09-20 18:24:19

标签: c# html selenium selenium-webdriver web

例如,我要使用硒单击“?currentPage = 3”,在网站本身上,单击的按钮将重定向到href,但是,当我使用硒时,什么也没有发生。 我尝试了很多次迭代,还查找了类似的指南,但无济于事。

<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>

1 个答案:

答案 0 :(得分:2)

对于元素上的Click(),您必须为ElementToBeClickable()引入 WebDriverWait ,并且可以使用以下任一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();
    

更新

可能您需要SeleniumExtras.WaitHelpers,并且可以使用以下任一解决方案:

  • 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();
    

参考文献

您可以在以下位置找到几个相关的详细讨论: