Selenium Cheat Sheet
Posted on
//Webdriver Methods driver.get("https://example.com"); driver.close(); //Close the current window driver.get("http://www.example.com"); driver.getTitle(); driver.getCurrentUrl(); driver.getPageSource(); driver.quit(); //Navigation Methods driver.navigate().to("http://www.example2.com"); // Load a new web page in the current browser window driver.navigate().back(); // Move back a single "item" in the browser's history. driver.navigate().forward(); // Move a single "item" forward in the browser's history. driver.navigate().refresh(); // Refresh the current page //Managing the current window driver.manage().window().fullscreen(); //Fullscreen the current window if it is not already fullscreen driver.manage().window().maximize(); // Maximizes the current window if it is not already maximized driver.manage().window().minimize(); // Minimizes the current window if it is not already minimized //managing driver timeouts //Specifies the amount of time the driver should wait when searching for an element if it is not immediately present. driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10)); //Sets the amount of time to wait for a page load to complete before throwing an error. driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10)); //Sets the amount of time to wait for an asynchronous script to finish execution before throwing an error. driver.manage().timeouts().scriptTimeout(Duration.ofSeconds(10)); // Switch Methods driver.switchTo().alert(); // Switches to the active modal dialog driver.switchTo().window("window2"); // Switch focus to window2 driver.switchTo().defaultContent(); // Switch to the main document when a page contains iframes driver.switchTo().parentFrame(); // Switch focus to the parent context. driver.switchTo().activeElement(); // Switches focus to the active element //Alert Methods Alert alert = driver.switchTo().alert(); alert.accept(); // To accept the alert dialog alert.dismiss(); // To dismiss the alert dialog alert.getText(); // To get alert dialog text alert.sendKeys("sample"); // To enter text in prompt dialog //Element Methods WebElement element = driver.findElement(By.id("some_id")); element.sendKeys("some text"); // To type into an element, which may set its value element.getText(); // Get the visible text of this element element.isDisplayed(); // Check if this element displayed element.isEnabled(); // Check if this element enabled element.getTagName(); // Get the tag name of this element element.click(); // Click this element element.submit(); // It will submit the form element.getAttribute("value"); // Get the value of the given attribute element.getCssValue("float"); // Get the value of a given CSS property element.findElement(By.id("someId")); // Find the first WebElement within element element.findElements(By.className("items")); // Find WebElements within element element.clear(); // It will reset element value //Select Methods Select select = new Select(element); // Select the option at the given index. select.selectByIndex(1); // Select all options that have a value matching the argument. select.selectByValue("some value"); // Select all options that display text matching the argument. select.selectByVisibleText("Visible text"); // Clear all selected entries. select.deselectAll(); // Deselect the option at the given index. select.deselectByIndex(1); // Deselect all options that have a value matching the argument. select.deselectByValue("some value"); // Deselect all options that display text matching the argument. select.deselectByVisibleText("visible text"); // Returns all selected options belonging to this select tag select.getAllSelectedOptions(); // Returns the first selected option in this select tag select.getFirstSelectedOption(); // Returns all options belonging to this select tag select.getOptions(); // Check whether this select element support selecting multiple options select.isMultiple(); //Javascript Executor Methods JavascriptExecutor je = (JavascriptExecutor) driver; // Executes JavaScript in the context of the currently selected frame or window. je.executeScript("window.scrollBy(10,500)"); //Execute an asynchronous piece of JavaScript in the context of the currently selected frame or window. je.executeAsyncScript("window.setTimeout(arguments[arguments.length - 1], 2000);"); // For emulating complex user gestures, use this class rather than using the Keyboard or Mouse directly Actions actions = new Actions(driver); actions.dragAndDrop(sourceElement, destinationElement); // Drang and drop actions.click(); // Clicks at the current mouse location. actions.clickAndHold(); // Clicks (without releasing) at the current mouse location actions.contextClick(); // Performs a context-click at the current mouse location. actions.doubleClick(); // Performs a double-click at the current mouse location. actions.keyDown(Keys.ENTER); // Performs a modifier key press. actions.moveToElement(destinationElement); // Moves the mouse to the middle of the element. actions.release(); // Releases the depressed left mouse button at the current mouse location. actions.sendKeys(); // Sends keys to the active element. // Find Elements By By.className("class_name"); // Find element by class name By.id("element_id"); // Find element by id By.cssSelector("#css_selector"); // Find element by css selector By.linkText("link_text"); // Find element link text By.name("name"); // Find element name By.tagName("tag_name"); // Find element tag name By.xpath("//xpath"); // Find element by xpath By.partialLinkText("partial_link_text"); // Find element by partial link text