What I am trying to accomplish is browsing to a page, waiting for something to load and then taking and saving a screenshot.
The code I already have is
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();
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?
We can make Selenium wait for 10 seconds. This can be done by using the Thread. sleep method. Here, the wait time (10 seconds) is passed as a parameter to the method.
pause (time in milliseconds) - Selenium IDE command Special feature: If you use pause | 0 or simply pause without any number then the execution pauses until the user clicks the RESUME button.
Click And Hold Action: dragAndDrop() method first performs click-and-hold at the location of the source element. Move Mouse Action: Then source element gets moved to the location of the target element. Button Release Action: Finally, it releases the mouse.
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")));
That wouldnt really be a selenium specific thing. You just want java to sleep for a bit after loading the page but before taking the screenshot.
Thread.sleep(4000);
put that after your driver.get statement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With