java无法使用selenium逐个单击图像旋转木马中的内容
我无法单击下面的旋转木马中显示的图像,如下面的快照所示。我尝试了很多,但是失败了,尽管我可以通过点击右箭头导航按钮导航到其他图像。图像转盘位于特色车辆标题文本(在网站中)下方,该文本具有左右导航按钮 网站链接:https://ryder.com/used-trucks
下面是我尝试过的方法之一,代码如下。请你尽快帮我
public void click_Image_Carousel_To_Open_ProductDetailsPage() throws InterruptedException
{
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView();", driver.findElement(By.xpath("//h2[contains(text(),'FEATURED VEHICLES')]")));
WebElement ele1= null;
String image_link = null;
List<WebElement> image_Carousel_Links_list = driver.findElements(By.xpath("(//div[@class='photo']/a)"));
System.out.println("Size :"+image_Carousel_Links_list.size());
WebElement image_Carousel_Next_Btn = driver.findElement(By.xpath("//BUTTON[@class='slick-next slick-arrow'][text()='Next']"));
for(int i=1;i<image_Carousel_Links_list.size();i++)
{
System.out.println(+i+")Image links :"+driver.findElement(By.xpath("(//div[@class='photo']/a)["+i+"]")).getAttribute("href"));
System.out.println(" Element : (//div[@class='photo']/a)["+i+"]");
ele1=driver.findElement(By.xpath("(//div[@class='photo']/a)["+i+"]"));
//this for loop to rediscover the elements to avoid stale element exception
for(int k=0;k<500;k++)
{
image_Carousel_Links_list = driver.findElements(By.xpath("(//div[@class='photo']/a)"));
if(driver.findElement(By.xpath("(//div[@class='photo']/a)["+i+"]")).isDisplayed())
{
ele1=driver.findElement(By.xpath("(//div[@class='photo']/a)["+i+"]"));
break;
}
click_ImageCarousel_NextButton(image_Carousel_Next_Btn);
Thread.sleep(300);
}
if(ele1.isDisplayed())
{
ele1.click();
System.out.println(i+") Clicked on the Image present in Image Carousel :" +ele1.getAttribute("href"));
Thread.sleep(3000);
driver.navigate().back();
Thread.sleep(4000);
}
else
{
System.out.println(" Image Not found in Image Carousel");
}
}
}
private void click_ImageCarousel_NextButton(WebElement image_Carousel_Next_Btn) {
image_Carousel_Next_Btn.click();
}
共 (0) 个答案