0% found this document useful (0 votes)
33 views

seleniumwebdriver_cheatsheet

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

seleniumwebdriver_cheatsheet

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Selenium Cheat Sheet Get element text:

String elementText = element.getText();


Get attribute value:
Driver Initialization Basics
String attributeValue =
Chrome: WebDriver driver = new ChromeDriver();
element.getAttribute("attribute_name");
Firefox: WebDriver driver = new FirefoxDriver();
Edge: WebDriver driver = new EdgeDriver();
Safari: WebDriver driver = new SafariDriver(); Handling Frames

Selenium Locators Switch to frame by ID or Name:


Find element by ID: driver.switchTo().frame("frame_id_or_name");
WebElement element = Switch back to main content:
driver.findElement(By.id("element_id")); driver.switchTo().defaultContent();
Switch to frame by index:
Find element by Name: driver.switchTo().frame(0);
WebElement element = Wait for frame to be available and switch to it:
driver.findElement(By.name("element_name")); WebDriverWait wait = new
WebDriverWait(driver, 10);
Find element by Class Name: wait.until(ExpectedConditions.frameToBeAvai
WebElement element = lableAndSwitchToIt(By.id("frame_id")));
driver.findElement(By.className("element_class"));
Handling Alerts
Find element by XPath: Switch to an alert and accept it:
WebElement element = Alert alert = driver.switchTo().alert();
driver.findElement(By.xpath("//div[@id='element_id']")); alert.accept();
Dismiss an alert:
Find element by CSS Selector: alert.dismiss();
WebElement element = Get the text of an alert:
driver.findElement(By.cssSelector("#element_id")); String alertText = alert.getText();
Input text in an alert prompt:
alert.sendKeys("Text for prompt");
Selenium Operations alert.accept();

Open URL:
Handling Windows
driver.get("https://www.example.com");

Get current window handle:


Go back: String mainWindowHandle =
driver.navigate().back();
driver.getWindowHandle();
Get all window handles:
Go forward: Set<String> allWindowHandles =
driver.navigate().forward(); driver.getWindowHandles();
Switch to a window by handle:
Refresh page: driver.switchTo().window("window_handle");
driver.navigate().refresh(); Switch back to the main window:
driver.switchTo().window(mainWindowHandle);
Implicit Wait:
driver.manage().timeouts().implicitlyWait(1 Taking Screenshot
0, TimeUnit.SECONDS);
• Take a screenshot of the whole page:
Explicit Wait:
WebElement element = (new File screenshotFile =
WebDriverWait(driver, 10)) ((TakesScreenshot)
.until(ExpectedConditions.elementToBeClicka driver).getScreenshotAs(OutputType.FI
ble(By.id("element_id"))); LE);
FileUtils.copyFile(screenshotFile,
Selenium Element Interactions new File("screenshot.png"));

Type text into input field: • Take a screenshot of a specific element:


element.sendKeys("Text to type");
File elementScreenshotFile =
Click on element: element.getScreenshotAs(OutputType.FI
element.click(); LE);
FileUtils.copyFile(elementScreenshotF
Clear input field: ile, new
element.clear(); File("element_screenshot.png"));

You might also like