Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Selenium to pause for X seconds

Tags:

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?

like image 655
user2612619 Avatar asked Nov 15 '13 19:11

user2612619


People also ask

How do I make Selenium wait 10 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.

How do I pause in Selenium?

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.

How do you hold and drag in Selenium?

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.


2 Answers

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"))); 
like image 196
user2612619 Avatar answered Oct 26 '22 23:10

user2612619


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.

like image 45
Jas Avatar answered Oct 27 '22 01:10

Jas