让Selenium暂停X秒 [英] Getting Selenium to pause for X seconds
问题描述
我想要完成的是浏览页面,等待加载某些内容,然后拍摄并保存屏幕截图。
What I am trying to accomplish is browsing to a page, waiting for something to load and then taking and saving a screenshot.
我已经拥有的代码是
WebDriver driver = new FirefoxDriver();
driver.get("http://www.site.com");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
try {
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("/home/Desktop/image.png"));
} catch (Exception e) {
e.printStackTrace();
}
driver.close();
我需要等待的原因,即使加载页面是因为它会被加载但是在网站上的内容我想在几秒钟后拍下照片。由于某种原因页面没有等待,是否有另一种方法可以让驱动程序/页面等待X秒?
The reason I need to wait, even if the page is loaded is because it'll be loaded but on the site the content I'd like to take a picture of loads after a few seconds. For some reason the page is not waiting, is there another method that I can use to get the driver/page to wait for X amount of seconds?
推荐答案
您可以找到在初始页面加载后加载的元素,然后让Selenium等到找到该元素。
You can locate an element that loads after the initial page loads and then make Selenium wait until that element is found.
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("ID")));
这篇关于让Selenium暂停X秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!