2017-04-10 71 views
0

我想点击<>横幅上的按钮,它在amazon.in中持续几秒钟后仍然旋转,但无法这样做。Selenium Webdriver - 点击横幅

我写了下面的代码,但还是没有成功

driver.get("amazon.in"); 
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); 
driver.findElement(By.xpath("//span[contains(text(),'Previous page')]")).click(); 

它不能在<按钮点击其上显示在页面顶部的标题。

+0

你检查元素出现在间歇页? – ASANT

回答

-1

使用以下代码点击下一个>和之前的<滑块的箭头。

driver.get("http://www.amazon.in/"); 
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
driver.manage().window().maximize(); 
// Forward navigation 
for(int i =0;i<3;i++) 
{ 
     driver.findElement(By.xpath("//a[@class='a-carousel-goto-nextpage']")).click(); 
     Thread.sleep(1000); 
    } 

// back navigation 
for(int j=0;j<3;j++) 
{ 
    driver.findElement(By.xpath("//a[@class='a-carousel-goto-prevpage']")).click(); 
    Thread.sleep(1000); 
} 
+1

使用'Thread.sleep()'是一种不好的做法,更不用说你将它与隐式等待混合在一起。 – JeffC

+0

当您编写自动化时,您不需要看到效果。如果您需要检测效果,您可以使用代码对其进行测试以确保点击成功等。 – JeffC

+0

您已经发布了使用不良做法在SO上生活的代码。其他人可能会看到这个代码,并认为它没有时可以复制/使用它。 – JeffC

0

当我编写可能重用的代码时,我将它放入函数中。这里有一个功能可以点击横幅上的next和prev箭头。

public static void clickNextBanner() 
{ 
    driver.findElement(By.cssSelector("a.a-carousel-goto-nextpage")).click(); 
} 

public static void clickPrevBanner() 
{ 
    driver.findElement(By.cssSelector("a.a-carousel-goto-prevpage")).click(); 
} 
+0

谢谢它适用于我! – Rats

+0

如果您发现此(或任何)答案有帮助,请立即加入。如果这回答了您的问题,请将其标记为已接受的答案。谢谢! – JeffC

0

尝试使用显式等待

WebElement previous =driver.findElement(By.xpath("//span[contains(text(),'Previous page')]")); 
    WebDriverWait wait = new WebDriverWait(driver,20); 
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[contains(text(),'Previous page')]"))); 
    previous.click();