[英]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.