简体   繁体   English

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

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

I have a view of items. 我对物品有看法。 There are total 29 pages and in one page there are 20 items are displayed. 总共有29页,一页显示20个项目。 I'm trying to get the count of total items (including all pages) via selenium. 我正在尝试通过硒获取总项目数(包括所有页面)。

Here is the HTML for the first page : 这是第一页的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>

Here is the HTML of last page: 这是最后一页的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>

I'm able to reach till the last page. 我可以到达最后一页。 But on reaching the last page, it continues to find the element to click which is no longer active. 但是,在到达最后一页时,它将继续找到不再处于活动状态的单击元素。

Here is my Selenium Code: 这是我的硒代码:

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

The loop is getting infinite. 循环变得无限。 How can I stop once the last page is reached and the state of element changes to disabled. 到达最后一页并且元素状态更改为“禁用”后,如何停止。

Here, the Next Page button is enabled /disabled based on the below section in HTML and If the button is disabled, then class='disabled' is added in HTML. 在此,根据HTML中的以下部分启用/禁用了“下一页”按钮,如果禁用了该按钮,则将在HTML中添加class='disabled' So, we can iterate the while loop until the above disabled element found . 因此,我们可以迭代while循环,直到找到上述禁用的元素。 Please check the below solution code 请检查以下解决方案代码

First Page HTML: 第一页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>

Last Page HTML: 最后一页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" />

Solution Code: 解决方案代码:

    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");
        }           
    }

You can check if element is enabled like this: 您可以像这样检查是否enabled元素:

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

in your code it could be like this: 在您的代码中可能是这样的:

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

or you can try also this way: 或者您也可以这样尝试:

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

Here every time you locate the list of elements and if the list size becomes 0 , then you have reached the last page. 每次您找到元素列表时,如果列表size变为0 ,那么您就到达了最后一页。

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

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