Selenium 单击<li> 中的href 中的链接. [英] Selenium to click a link in href which in &lt;li&gt;

查看:41
本文介绍了Selenium 单击<li> 中的href 中的链接.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<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>创建者:</p><p>发布者:</p><p>主题:</>

<nav epub:type="toc";id=toc"><ol><li><a href="?currentPage=3">Tööraamatu kasutajale</a><li><a href=?currentPage=4">1.Arvuhulgad ja avaldised</a><li><a href="?currentPage=4">1.1.Arvuhulgad </a><li><a href="?currentPage=7">1.2.Tehted astmete ja juurtega </a><li><a href="?currentPage=11">1.3.Ratsionaalavaldise lihtsustamine </a >

解决方案

To Click() 你必须为 ElementToBeClickable() 并且您可以使用以下任一定位器策略:

  • 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']"))).点击();

  • 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']";))).点​​击();

  • XPath:

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


参考文献

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

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>

解决方案

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:

这篇关于Selenium 单击<li> 中的href 中的链接.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆