我的网站有一个带有3个按钮的旋转木马,用于3个旋转木马项目。 html代码如下:
<ul class="slick-dots" style="display: block;">
<li class="slick-active" aria-hidden="false">
<button type="button" data-role="none" role="button" tabindex="0">1</button>
</li>
<li aria-hidden="true">
<button type="button" data-role="none" role="button" tabindex="0">2</button>
</li>
<li aria-hidden="true">
<button type="button" data-role="none" role="button" tabindex="0">3</button>
</li>
</ul>
加载网站时,启用第一个按钮。 单击轮播中的第二个按钮会更改html,如下所示;
<ul class="slick-dots" style="display: block;">
<li class="" aria-hidden="true">
<button type="button" data-role="none" role="button" tabindex="0">1</button>
</li>
<li class="slick-active" aria-hidden="false">
<button type="button" data-role="none" role="button" tabindex="0"
style="">2</button>
</li>
<li aria-hidden="true">
<button type="button" data-role="none" role="button" tabindex="0">3</button>
</li>
</ul>
我需要点击每个轮播按钮并捕获相应的标题标题。以下是我到目前为止所做的事情。但是,这并不一致。但是,如果我调试代码,那么所有3个标题都会被正确捕获。
int CarouselItem=2;
for (int i = 1; i<=CarouselItems.size(); i++) {
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
//*getting the page header heading
WebElement PageHeaderHeading = driver.findElement(By.xpath("//div[1]/main/section/div/div/div/div[" + i + "]/div[2]/div/div/div[2]/h1"));
System.out.println("Page header text of "+i+" now is " + PageHeaderHeading.getText());
//* clicking on the 2nd and 3rd carousel buttons
if(CarouselItem<=CarouselItems.size())
{
WebElement CarouselButtonToClick = driver.findElement(By.xpath("//li[(@aria-hidden='true') and button[contains(text(),'"+CarouselItem+"')]]"));
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
CarouselButtonToClick.click();
CarouselItem = CarouselItem + 1;
}
}
有人可以帮帮我吗?这段代码有什么不对,不能一致地给出所有3个轮播项目的标题?