Index

Selenium

Initialization

create driver
WebDriver driver = new FirefoxDriver(new FirefoxProfile());
configure firefox driver location
System.setProperty("webdriver.gecko.driver", "/path/to/geckodriver");
set window size
driver.manage().window().setSize(new Dimension(1600, 800));
open url
driver.get("https://example.com");
h2 usageexecute javascript
private static <E> E js(String js) {
        JavascriptExecutor javascript = (JavascriptExecutor) driver;
        System.out.println("running js: " + js);
        return (E) javascript.executeScript(js);
    }
wait until the document if fully loaded
WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.until(new Predicate<WebDriver>() {
            @Override
            public boolean apply(WebDriver driver) {
                JavascriptExecutor javascript = (JavascriptExecutor) driver;
                String value = javascript.executeScript("return document.readyState").toString();
                return value.equals("complete");
            }
        });
wait until a certain id exists on a web page
WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.until(new Predicate<WebDriver>() {
            @Override
            public boolean apply(WebDriver driver) {
                return driver.findElement(By.id(id)) != null;
            }
        });