【问题标题】:Selenium to click a link in href which in <li>Selenium 单击 <li> 中的 href 中的链接
【发布时间】:2020-09-21 03:19:45
【问题描述】:

我想使用 selenium 单击“?currentPage=3”,例如,在网站本身上,您单击的按钮会重定向到 href,但是当我使用 selenium 时,没有任何反应。 我尝试了很多次迭代,也查找了类似的指南,但无济于事。

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

【问题讨论】:

    标签: c# html selenium selenium-webdriver web


    【解决方案1】:

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

    参考文献

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

    【讨论】:

    • ExpectedConditions 行给我一个错误我正在使用“使用 openQA.Selenium.Support.IO;”的当前上下文中不存在名称“ExpectedConditions”和“使用 openQA.Selenium.Support.IO.ExpectedConditions;”显然不再存在或已弃用
    • @Chris 查看更新的答案,让我知道状态。
    • 首先,我要感谢您在回答后回复,我是 stackoverflow 的新手,到目前为止,获得实时指导或指向正确方向真是太神奇了,我什至离开了你如果我有更多的声誉。但是即使是现在,似乎也在发生同样的错误。我还尝试了所有 3 个版本,CSS、xpath 和 linktext
    • NoSuchElementException:没有这样的元素:无法找到元素:{"method":"link text","selector":"Tööraamatu kasutajale"}(会话信息:chrome=85.0.4183.102)跨度>
    • @Chris 试试其他两个定位器。
    猜你喜欢
    • 2021-01-07
    • 2013-04-24
    • 1970-01-01
    • 2016-12-28
    • 2015-08-18
    • 2018-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多