繁体   English   中英

如何检查元素何时在Selenium中将其状态从启用更改为禁用

[英]How to check when an element changes its state from enable to disable in Selenium

我对物品有看法。 总共有29页,一页显示20个项目。 我正在尝试通过硒获取总项目数(包括所有页面)。

这是第一页的HTML:

<div class="pagination pagination-toolbar clearfix">


            <nav role="navigation" aria-label="Pagination">
            <ul class="pagination-list">
                    <li class="disabled">
    <span >
        <span class="icon-backward icon-first" aria-hidden="true"></span>   </span>
    </li>
    <li class="disabled">
    <span >
        <span class="icon-step-backward icon-previous" aria-hidden="true"></span>   </span>
    </li>
                                        <li class="active">
    <span aria-current="true" aria-label="Page 1">
        1   </span>
    </li>
                                        <li class="hidden-phone">
        <a aria-label="Go to page 2"   href="#" onclick="document.adminForm.limitstart.value=20; submitform();return false;">
            2       </a>
    </li>
                                        <li class="hidden-phone">
        <a aria-label="Go to page 3"   href="#" onclick="document.adminForm.limitstart.value=40; submitform();return false;">
            3       </a>
    </li>
                                        <li class="hidden-phone">
        <a aria-label="Go to page 4"   href="#" onclick="document.adminForm.limitstart.value=60; submitform();return false;">
            4       </a>
    </li>
                                        <li class="hidden-phone">
        <a aria-label="Go to page 5"   href="#" onclick="document.adminForm.limitstart.value=80; submitform();return false;">
            5       </a>
    </li>
                                        <li class="hidden-phone">
        <a aria-label="Go to page 6"   href="#" onclick="document.adminForm.limitstart.value=100; submitform();return false;">
            6       </a>
    </li>
                                        <li class="hidden-phone">
        <a aria-label="Go to page 7"   href="#" onclick="document.adminForm.limitstart.value=120; submitform();return false;">
            7       </a>
    </li>
                                        <li class="hidden-phone">
        <a aria-label="Go to page 8"   href="#" onclick="document.adminForm.limitstart.value=140; submitform();return false;">
            8       </a>
    </li>
                                        <li class="hidden-phone">
        <a aria-label="Go to page 9"   href="#" onclick="document.adminForm.limitstart.value=160; submitform();return false;">
            9       </a>
    </li>
                                        <li class="hidden-phone">
        <a aria-label="Go to page 10"   href="#" onclick="document.adminForm.limitstart.value=180; submitform();return false;">
            10      </a>
    </li>
                                    <li>
        <a aria-label="Go to next page" class="hasTooltip"  title="Next"  href="#" onclick="document.adminForm.limitstart.value=20; submitform();return false;">
            <span class="icon-step-forward icon-next" aria-hidden="true"></span>        </a>
    </li>
    <li>
        <a aria-label="Go to end page" class="hasTooltip"  title="End (Page 29 of 29)"  href="#" onclick="document.adminForm.limitstart.value=560; submitform();return false;">
            <span class="icon-forward icon-last" aria-hidden="true"></span>     </a>
    </li>
            </ul>
        </nav>

            <input type="hidden" name="limitstart" value="0" />

</div>

这是最后一页的HTML:

<div class="pagination pagination-toolbar clearfix">


            <nav role="navigation" aria-label="Pagination">
            <ul class="pagination-list">
                    <li>
        <a aria-label="Go to start page" class="hasTooltip"  title="Start (Page 1 of 29)"  href="#" onclick="document.adminForm.limitstart.value=0; submitform();return false;">
            <span class="icon-backward icon-first" aria-hidden="true"></span>       </a>
    </li>
    <li>
        <a aria-label="Go to previous page" class="hasTooltip"  title="Previous"  href="#" onclick="document.adminForm.limitstart.value=540; submitform();return false;">
            <span class="icon-step-backward icon-previous" aria-hidden="true"></span>       </a>
    </li>
                                        <li class="hidden-phone">
        <a aria-label="Go to page 20"   href="#" onclick="document.adminForm.limitstart.value=380; submitform();return false;">
            20      </a>
    </li>
                                        <li class="hidden-phone">
        <a aria-label="Go to page 21"   href="#" onclick="document.adminForm.limitstart.value=400; submitform();return false;">
            21      </a>
    </li>
                                        <li class="hidden-phone">
        <a aria-label="Go to page 22"   href="#" onclick="document.adminForm.limitstart.value=420; submitform();return false;">
            22      </a>
    </li>
                                        <li class="hidden-phone">
        <a aria-label="Go to page 23"   href="#" onclick="document.adminForm.limitstart.value=440; submitform();return false;">
            23      </a>
    </li>
                                        <li class="hidden-phone">
        <a aria-label="Go to page 24"   href="#" onclick="document.adminForm.limitstart.value=460; submitform();return false;">
            24      </a>
    </li>
                                        <li class="hidden-phone">
        <a aria-label="Go to page 25"   href="#" onclick="document.adminForm.limitstart.value=480; submitform();return false;">
            25      </a>
    </li>
                                        <li class="hidden-phone">
        <a aria-label="Go to page 26"   href="#" onclick="document.adminForm.limitstart.value=500; submitform();return false;">
            26      </a>
    </li>
                                        <li class="hidden-phone">
        <a aria-label="Go to page 27"   href="#" onclick="document.adminForm.limitstart.value=520; submitform();return false;">
            27      </a>
    </li>
                                        <li class="hidden-phone">
        <a aria-label="Go to page 28"   href="#" onclick="document.adminForm.limitstart.value=540; submitform();return false;">
            28      </a>
    </li>
                                        <li class="active">
    <span aria-current="true" aria-label="Page 29">
        29  </span>
    </li>
                                    <li class="disabled">
    <span >
        <span class="icon-step-forward icon-next" aria-hidden="true"></span>    </span>
    </li>
    <li class="disabled">
    <span >
        <span class="icon-forward icon-last" aria-hidden="true"></span> </span>
    </li>
            </ul>
        </nav>

            <input type="hidden" name="limitstart" value="560" />

</div>

我可以到达最后一页。 但是,在到达最后一页时,它将继续找到不再处于活动状态的单击元素。

这是我的硒代码:

WebElement pag = driver.findElement(By.xpath("//li[not(contains(@class, 'disabled'))]/a/span[(contains(@class, 'icon-next'))]"));
        while(pag!= null)
        {
            ((JavascriptExecutor)driver).executeScript("window.scrollTo(0, document.body.scrollHeight)");
            driver.findElement(By.className("icon-next")).click();
            List <WebElement> itemlist = driver.findElements(By.xpath("//div[@class='tjlms-tbl']/table/tbody/tr"));
            total = total + itemlist.size();
        }

循环变得无限。 到达最后一页并且元素状态更改为“禁用”后,如何停止。

在此,根据HTML中的以下部分启用/禁用了“下一页”按钮,如果禁用了该按钮,则将在HTML中添加class='disabled' 因此,我们可以迭代while循环,直到找到上述禁用的元素。 请检查以下解决方案代码

第一页HTML:

    <li class="hidden-phone">
        <a aria-label="Go to page 10"   href="#" onclick="document.adminForm.limitstart.value=180; submitform();return false;">
    10      </a>
    </li>
    <li>
        <a aria-label="Go to next page" class="hasTooltip"  title="Next"  href="#" onclick="document.adminForm.limitstart.value=20; submitform();return false;">
            <span class="icon-step-forward icon-next" aria-hidden="true"></span>
        </a>
    </li>
    <li>
        <a aria-label="Go to end page" class="hasTooltip"  title="End (Page 29 of 29)"  href="#" onclick="document.adminForm.limitstart.value=560; submitform();return false;">
            <span class="icon-forward icon-last" aria-hidden="true"></span>
        </a>
    </li>
</ul>

最后一页HTML:

        <li class="active">
            <span aria-current="true" aria-label="Page 29">
    29  </span>
        </li>
        <li class="disabled">
            <span >
                <span class="icon-step-forward icon-next" aria-hidden="true"></span>
            </span>
        </li>
        <li class="disabled">
            <span >
                <span class="icon-forward icon-last" aria-hidden="true"></span>
            </span>
        </li>
    </ul>
</nav>
<input type="hidden" name="limitstart" value="560" />

解决方案代码:

    List <WebElement> fistPagelist = driver.findElements(By.xpath("//div[@class='tjlms-tbl']/table/tbody/tr"));
    total = fistPagelist .size();
boolean hasNextPage=true;

    while(hasNextPage)
    {
        ((JavascriptExecutor)driver).executeScript("window.scrollTo(0, document.body.scrollHeight)");
        List <WebElement> disabledButtonList = driver.findElements(By.xpath("//li[@class='disabled']//span[@class='icon-forward icon-last']"));

        //If the page is lastpage, then only above disabledButtonList webElement will be found and it's size will be 1. Otherwise, it's size will be 0.

        if(disabledButtonList.size()==0){
            driver.findElement(By.className("icon-next")).click();
            List <WebElement> itemlist = driver.findElements(By.xpath("//div[@class='tjlms-tbl']/table/tbody/tr"));
            total = total + itemlist.size();
            //Inorder to iterate the while loop, hasNextPage flag value is set it as true
            hasNextPage=true;           
        }
        else{
             List <WebElement> itemlist = driver.findElements(By.xpath("//div[@class='tjlms-tbl']/table/tbody/tr"));
             total = total + itemlist.size();
            //If the LastPage found, then we need to stop the while loop and hence hasNextPage flag value is set it as false;
            hasNextPage=false;  
            System.out.println("You have reached the last page");
        }           
    }

您可以像这样检查是否enabled元素:

if (element.isEnabled()) {
   // do stuff
}else {
   break;
}

在您的代码中可能是这样的:

WebElement pag = driver.findElement(By.xpath("//li[not(contains(@class, 'disabled'))]/a/span[(contains(@class, 'icon-next'))]"));
while(pag!= null)
{
  ((JavascriptExecutor)driver).executeScript("window.scrollTo(0, document.body.scrollHeight)");
  WebElement next = driver.findElement(By.className("icon-next"));
  if (next.isEnabled()){
    next.click(); // click only if enabled
    List <WebElement> itemlist = driver.findElements(By.xpath("//div[@class='tjlms-tbl']/table/tbody/tr"));
    total = total + itemlist.size();
  }else { // break the loop if not enabled
    System.out.println("You have reached the last page");
    break;
  }
}

或者您也可以这样尝试:

List<WebElement> pag = driver.findElements(By.xpath("//li[not(contains(@class, 'disabled'))]/a/span[(contains(@class, 'icon-next'))]"));
while(pag.size() > 0)
{
  pag = driver.findElements(By.xpath("//li[not(contains(@class, 'disabled'))]/a/span[(contains(@class, 'icon-next'))]"));
  ((JavascriptExecutor)driver).executeScript("window.scrollTo(0, document.body.scrollHeight)");
  driver.findElement(By.className("icon-next")).click();
  List <WebElement> itemlist = driver.findElements(By.xpath("//div[@class='tjlms-tbl']/table/tbody/tr"));
  total = total + itemlist.size();
}

每次您找到元素列表时,如果列表size变为0 ,那么您就到达了最后一页。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM