Selenium Webdriver - 点击横幅
问题描述:
我想点击<
和>
横幅上的按钮,它在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();
它不能在<
按钮点击其上显示在页面顶部的标题。
答
使用以下代码点击下一个>
和之前的<
滑块的箭头。
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);
}
答
当我编写可能重用的代码时,我将它放入函数中。这里有一个功能可以点击横幅上的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();
}
答
尝试使用显式等待
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();
你检查元素出现在间歇页? – ASANT