Practical Code Flashcards
what is the recommended way to install a browser driver in order to use it in Selenium?
- to download the Chrome driver (conveniently named chromedriver.exe)
- install (store) that file in a well-known place (directory) on the test workstation.
- add directory to the Path environmental variable
What happens when we create a WebDriver object?
- An API between a script and the web browser is created
- An instance of the web browser is created
Why is Web Driver protocol enabled to run several tests simultaneously or to control several browsers in one script?
Because WebDriver protocol is based on HTTP communication
How do we create a WebDriver object to interact with Chrome Browser?
from selenium import webdriver
driver = webdriver.Chrome()
CODE: To navigate to desired site page
driver.get(‘https://www.python.org’)
CODE: Two useful attributes that the WebDriver object contains to check if the correct page has
been opened:
- driver.current_url
- driver.title
CODE: To close the browser controlled by webdriver
driver.quit()
CODE: To close an specific tab in the controlled web browser instance
driver.close()
What is the safest way to determine which window is the currently open window?
driver.title attribute
CODE: To simulate navigating forward, backward and refresh the web browser
- driver.back()
- driver.forward()
- driver.refresh()
three ways in which a script context can be changed:
- changing browsers
- changing windows/tabs within one browser
- changing frames within one page
where the list of all open tabs (windows) is stored?
An array that can be accessed through the window_handles attribute of the WebDriver object.
CODE: The way to switch between open tabs in a browser (using Windows)
for handle in driver.window_handles:
driver.switch_to.window(handle)
CODE: One way to Open up multiple tabs in Chrome browser (using Windows):
driver.execute_script( “$(window.open(‘’)” )
What is necessary to be able to find elements in a specific frame?
change the context to that particular frame
CODE: To change context to a specific frame.
driver.switch_to.frame(‘frame_ID’)
CODE: What happens if the frame has no ID or we want to use different strategy to find it?
frame_location = driver.find_element_by_name(‘frame_name’)
driver.switch_to.frame( frame_location )
CODE: to switch back to the parent frame
driver.switch_to.parent_frame()
CODE: switch back to the whole page
driver.switch_to.default_content()
CODE: Three ways to manipulate the size of a web browser’s window using Selenium.
maximize: driver.maximize_window()
minimize: driver.minimize_window()
fullscreen: driver.fullscreen_window()
When it is useful to take screenshots of the screen?
- When the automated script detects that a failure occurred
- When the pass/fail determination can only be made by viewing the screen image
- When dealing with safety or mission-critical software which might require an audit
- When doing configuration testing on different systems
two scopes in which screenshots can be taken?
- the entire browser page
- a single element in the browser page
CODE: how to take a screenshot of a whole page and place it in a specific location?
driver.get_screenshot_as_file(“C:\path\to\screenshot.png”)
CODE: how to take screenshot of an element and saving it to a specific location?
ele = driver.find_element_by_id(‘btnLogin’)
ele.screenshot(“C:\path\to\element_screenshot.png”)
CODE: create an image as a base64 encoded string version of the snapshot of the window
img_b64 = driver.get_screenshot_as_base64()
CODE: create a binary string representation of the snapshot of the window
png_str = driver.get_screenshot_as_png()
CODE: create an image as a base64 encoded string version of the snapshot of a web element
ele = driver.find_element_by_id('loginForm') img_b64 = ele.screenshot_as_base64
CODE: create an image as a base64 encoded string version of the snapshot of a web element
ele = driver.find_element_by_id('loginForm') png_str = ele.screenshot_as_png()