gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 07:46:01 26 4
gpt4 key购买 nike

我有项目的 View 。共有 29 页,一页显示 20 个项目。我正在尝试通过 selenium 获取项目总数(包括所有页面)。

这是第一页的 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>

我可以翻到最后一页。但是在到达最后一页时,它会继续查找不再 Activity 的要单击的元素。

这是我的 Selenium 代码:​​

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

关于java - 如何检查元素何时在 Selenium 中将其状态从启用更改为禁用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51116682/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com