0. Introduction
As we said in a previous entry in this blog, we are considering automatically use Selenium for the automatic use of web pages.
The only drawback is to manually select the certificate for authentication as it seems rather difficult for Selenium.
As Anuja Jain says in her blog there are mainly 3 ways to wait for a condition to get fulfilled.
1. First wait condition: Implicit wait
Anuja explains: "Implicit Wait means informing selenium web driver to wait for specific amount of time and if the web element is not visible after waiting for that specific point then throw "No such Element found Exception".1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | package test; import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; public class ImplicitWait { public static void main(String[] args) throws Exception { //1. Get WebDriver String chromeDriverPath = "/home/eduard/Selenium/chromedriver" ; System.setProperty("webdriver.chrome.driver", chromeDriverPath); ChromeOptions options = new ChromeOptions(); //options.addArguments("--headless", "--disable-gpu", "--window-size=1920,1200","--ignore-certificate-errors", "--silent"); WebDriver driver = new ChromeDriver(options); driver.get("https://dantesquehtmlunit.blogspot.com"); driver.manage().timeouts().implicitlyWait( 2000, TimeUnit.MILLISECONDS); driver.quit(); } } |
2. Second wait condition: Implicit wait
In this case, Selenium is waiting until the condition is fulfilled within a max time waiting before throwing an "ElementNotVisibleException" (if the element is not found)1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | package test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; public class ExplicitWait { public static void main(String[] args) throws Exception { //1. Get WebDriver String chromeDriverPath = "/home/eduard/Selenium/chromedriver" ; System.setProperty("webdriver.chrome.driver", chromeDriverPath); ChromeOptions options = new ChromeOptions(); //options.addArguments("--headless", "--disable-gpu", "--window-size=1920,1200","--ignore-certificate-errors", "--silent"); WebDriver driver = new ChromeDriver(options); //Define a wait object with a max time for waiting WebDriverWait wait = new WebDriverWait(driver,20); driver.get("https://dantesquehtmlunit.blogspot.com"); WebElement title; //Wait untill title is available title= wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("title"))); System.out.println("condition fulfilled!"); Thread.sleep(2000); driver.quit(); } } |
A new object is created for managing the intelligent wait.
3. Third wait condition: Fluent wait. Valid for waiting to manually select Certificate for login.
By means of the fluent wait, two times are settled:- First delay time
- repeat time cycle.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | package test; import java.time.Duration; import java.util.function.Function; import org.openqa.selenium.By; import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.support.ui.FluentWait; import org.openqa.selenium.support.ui.Wait; public class SeleniumWaitForCertSelection { private static WebElement getWebElement(WebDriver driver, Wait<WebDriver> wait, String xpathExpression) { WebElement myElement= wait.until(new Function<WebDriver, WebElement>() { public WebElement apply(WebDriver driver) { return driver.findElement(By.xpath(xpathExpression)); } }); return myElement; } public static void main(String[] args) { //1. Get WebDriver String chromeDriverPath = "/home/eduard/Selenium/chromedriver" ; System.setProperty("webdriver.chrome.driver", chromeDriverPath); ChromeOptions options = new ChromeOptions(); //options.addArguments("--headless", "--disable-gpu", "--window-size=1920,1200","--ignore-certificate-errors", "--silent"); WebDriver driver = new ChromeDriver(options); Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) .withTimeout(Duration.ofSeconds(2)) .pollingEvery(Duration.ofSeconds(1)) .ignoring(NoSuchElementException.class); //1. Get the page driver.get("https://enguera.sedelectronica.es/carpetaelectronica.4"); //2. fluent Wait and Look for the anchor element and click (//a=anchor) //driver.findElement(By.xpath("//a[contains (@id, 'id2')]")).click(); getWebElement(driver,wait,"//a[contains (@id, 'id2')]").click(); // wait and get the second element getWebElement(driver,wait,"//a[contains (@id, 'id3')]").click(); //Wait and the the nth element and meanwhile you can interact manually with the browser to get the dertificate getWebElement(driver,wait,"//a[contains (@id, 'idn')]").click(); System.out.println("condition fulfilled!"); Thread.sleep(2000); driver.quit(); } } |
Lines 16-25 a method for waiting and finding the desired element is created with yellow background
Lines 36-39 a fluent wait object is defined to manage delays
Line 46-48 explicit call to the method to find the desired element and click it,
Lines 53-54 You can interact manually with the browser until the desired element is visible. In this case, we can select manually a certificate before entering the next page