July 12, 2020

Selenium Python Tutorial 10 | Selenium python Screenshot | selenium python screenshot_as_png

This is the next tutorial in the Selenium Python Tutorials for Beginners. This Selenium Python beginner tutorial explains Selenium Python screenshot or Selenium Python snapshot, meaning the image of the screen in a specific state. We may need to take a Selenium Python screenshot on failure of test case for bug reporting or on success for proof of testing. First, view the Selenium Python Screenshot Tutorial. Then read on.

Here is my Selenium Python example with a Python user defined function to take a WebDriver screenshot Python automatically. Then, I will use Python Imaging Library to modify the screenshot. The explanations are in the comments within the code (lines starting with #).

# Selenium WebDriver Python coding
# This Python file, Utilities.py, contains the WebDriver screenshot Python functions, screenshot and modify_screenshot.
import time
from PIL import Image as I

def screenshot(d):
    """Take the Selenium Python screenshot with the current date and time"""
    # Put the folder path with your computer's folder. The following folder path is an example only.
    # Escape each backslash with another backslash.
    folder = "E:\\Training\\SeleniumPython\\Screenshots\\"
    time_string = time.asctime().replace(":", " ")
    # Take Selenium Python screenshot_as_png
    file_name = folder + time_string + ".png"
    # Save Python Selenium screenshot with Selenium WebDriver save_screenshot method.
    d.save_screenshot(file_name)
   # Another Save Python Selenium screenshot with Selenium WebDriver method is get_screenshot_as_file (commented out).
    #d.get_screenshot_as_file(file_name)
    modify_screenshot(file_name)

def modify_screenshot(f):
    picture = I.open(f)
    # Modify the screenshot. Resize screenshot to another screen resolution.
    picture = picture.resize((1280, 654))
    # Modify the screenshot. Rotate screenshot by 90 degrees.
    picture = picture.transpose(I.ROTATE_90)
    # Modify the screenshot to save Selenium Python screenshot_as_jpg.
    picture = picture.convert("RGB")
    # Python strings are immutable. Create a new string for the filename.
    f_new = f.replace(".png", ".jpg")
    picture.save(f_new)

Here is my Selenium Python script, Screenshots.py that calls the Python Selenium screenshot function. Note that the screenshot user defined function calls the modify_screenshot function on it's last line.

# Selenium WebDriver Python coding
from selenium import webdriver
import Utilities as U
exec_path = r"E:\Training\SeleniumPython\Downloads\geckodriver-v0.26.0-win32\geckodriver.exe"
URL = "https://www.youtube.com/playlist?list=PLc3SzDYhhiGUPPWt_rIVszepL1nMTbDaW"

driver = webdriver.Firefox(executable_path=exec_path)
driver.get(URL)
U.screenshot(driver)
# Close the browser with the Selenium WebDriver quit method.
driver.quit()

Want to learn how the above user defined functions work in action? Or how to implement the screenshot function in an existing Selenium Python test automation script? Then, please view my Selenium Python Screenshot Tutorial. Thank you.

July 06, 2020

Selenium Python Tutorial 9 | python selenium copy paste | Keyboard Actions in selenium python | Keys and PyAutoGUI

Moving on to the next tutorial in the Selenium Python Tutorials for Beginners. This Selenium Python beginner tutorial explains keyboard actions in Selenium Python using the Keys and PyAutoGUI libraries. The keyboard actions are send keys, edit text, copy and paste text and set focus using the keyboard. First, view the Selenium Python Copy Paste Tutorial. Then read on. 
 
Here is my first Selenium Python example with explicit wait. I have put explanations in the comments within the code (lines starting with #).

# Selenium WebDriver Python coding
# Type something in the Distance textbox and copy it to the speed textbox.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as W
from selenium.webdriver.support import expected_conditions as E
from selenium.webdriver import ActionChains as A
from selenium.webdriver.common.keys import Keys as K

exec_path = r"E:\Training\SeleniumPython\Downloads\geckodriver-v0.26.0-win32\geckodriver.exe"
URL = "https://inderpsingh.blogspot.com/2014/08/demowebapp_24.html"
distance_id_locator = "distance"
speed_id_locator = "speed"
wait_time_out = 15
driver = webdriver.Firefox(executable_path=exec_path)
# Define variable for WebDriverWait Python.
wait_variable = W(driver, wait_time_out)
# Navigate to the URL given above.
driver.get(URL)
# Define the ActionChains object named a (to do keyboard actions).
a = A(driver)
distance_element = wait_variable.until(E.presence_of_element_located((By.ID, distance_id_locator)))
speed_element = wait_variable.until(E.presence_of_element_located((By.ID, speed_id_locator)))
# Type some text in the Distance text box using send keys.
distance_element.send_keys("123456")
# Python Selenium select all text using Ctrl+A
a.key_down(K.CONTROL).send_keys("a").perform()
# Python Selenium copy text to clipboard using Ctrl+C
a.key_down(K.CONTROL).send_keys("c").perform()
# Set focus to the Speed text box.
a.click_and_hold(speed_element).perform()
# Python Selenium paste text using Ctrl+V i.e. Python Selenium copy paste
a.key_down(K.CONTROL).send_keys("v").perform()

Here is my second Selenium Python example using PyAutoGUI, which I find having a number of features for keyboard actions and easy to use. You install the PyAutoGUI package with the pip command in the Command Prompt. Then install the PyAutoGUI package in the project under File menu>Settings>Project Interpreter. I explained such tasks here.

# Selenium WebDriver Python coding
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as W
from selenium.webdriver.support import expected_conditions as E
import pyautogui as P

exec_path = r"E:\Training\SeleniumPython\Downloads\geckodriver-v0.26.0-win32\geckodriver.exe"
URL = "https://inderpsingh.blogspot.com/2014/08/demowebapp_24.html"
distance_id_locator = "distance"
wait_time_out = 15
driver = webdriver.Firefox(executable_path=exec_path)
wait_variable = W(driver, wait_time_out)
driver.get(URL)
distance_element = wait_variable.until(E.presence_of_element_located((By.ID, distance_id_locator)))
# Call send keys method to send an empty string to set focus to the Distance text box.
distance_element.send_keys("")
# Call PyAutoGUI write method to type text in the Distance text box.
P.write("123456.78")
P.sleep(1)
# Call PyAutoGUI press method to press backspace key 3 times i.e. remove ".78".
P.press("backspace", 3)
# Call PyAutoGUI hotkey method to press keyboard shortcut Ctrl+A i.e. Python Selenium select all.
P.hotkey("ctrl", "a")
# Call PyAutoGUI hotkey method to press keyboard shortcut Ctrl+C i.e. Python Selenium copy text from web page.
P.hotkey("ctrl", "c")
P.sleep(1)
# Call PyAutoGUI press method to set focus to Speed text box.
P.press("tab")
# Call PyAutoGUI hotkey method to press keyboard shortcut Ctrl+V i.e. Python Selenium paste text from clipboard.
P.hotkey("ctrl", "v")

Want to learn more? Want to see the above Python scripts in action? Then, please view my Selenium Python Keyboard Actions Tutorial. Thank you.

June 28, 2020

Selenium Python Tutorial 8 | Python Selenium Mouse Click | Mouse Action Chains | PyAutoGUI Mouse

This is the next tutorial in the Selenium Python Tutorials for Beginners. This Selenium Python beginner tutorial explains Python Selenium mouse actions. First view the Selenium Python Mouse Click Tutorial below. Then read on.
 Here is my first Selenium Python example. I have put explanations in the comments within the code (lines starting with #).
# Selenium WebDriver Python coding
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as W
from selenium.webdriver.support import expected_conditions as E
from selenium.webdriver import ActionChains as A

exec_path = r"E:\Training\SeleniumPython\Downloads\geckodriver-v0.26.0-win32\geckodriver.exe"
URL1 = "https://inderpsingh.blogspot.com/2014/08/demowebapp_24.html"
URL2 = "https://crossbrowsertesting.github.io/drag-and-drop"
heading_css_locator = ".post-body > div:nth-child(1) > div:nth-child(1) > form:nth-child(1) > h3:nth-child(1)"
distance_id_locator = "distance"
draggable_id_locator = "draggable"
droppable_id_locator = "droppable"
wait_time_out =15

# Define the Selenium WebDriver variable.
driver = webdriver.Firefox(executable_path=exec_path)
# Define the variable for Explicit Wait.
wait_variable = W(driver, wait_time_out)
# Navigate to URL1.
driver.get(URL1)
# Find the heading web element, on which to perform the mouse double click.
heading_element = wait_variable.until(E.presence_of_element_located((By.CSS_SELECTOR, heading_css_locator)))
distance_element = wait_variable.until(E.presence_of_element_located((By.ID, distance_id_locator)))
# Define the variable of type ActionChains for Mouse Action Chains.
a = A(driver)
# Selenium Python Mouse Double Click on heading web element
a.double_click(heading_element)
# Call one of the multiple Selenium Python Mouse Move methods.
# Note: Selenium Python Mouse move methods implement Selenium Python Mouse Hover too.
a.move_to_element_with_offset(distance_element, 0, 0)
# Selenium Python Mouse Click
a.click_and_hold(distance_element)
a.release()
# Type 1000 at the mouse pointer location i.e. the beginning of the distance web element.
a.send_keys("1000")
# Call ActionChains perform method to run all the methods queued thus far.
a.perform()

# Navigate to URL2.
driver.get(URL2)
draggable_element = wait_variable.until(E.presence_of_element_located((By.ID, draggable_id_locator)))
droppable_element = wait_variable.until(E.presence_of_element_located((By.ID, droppable_id_locator)))
# Define another variable of type ActionChains. 
b = A(driver)
# Python Selenium Mouse Drag and Drop
b.drag_and_drop(draggable_element, droppable_element)
# Right click the Mouse on the draggable web element.
b.context_click(draggable_element)
b.perform()

A reliable way to operate the mouse with Selenium WebDriver is to use the PyAutoGUI library. You install the PyAutoGUI package with the pip command. Then install the PyAutoGUI package in the project. I explained such tasks here. Here is my second Selenium Python example for Selenium Python Mouse Actions using PyAutoGUI.

# Selenium WebDriver Python coding
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as W
from selenium.webdriver.support import expected_conditions as E
import pyautogui as P
import time

exec_path = r"E:\Training\SeleniumPython\Downloads\geckodriver-v0.26.0-win32\geckodriver.exe"
URL = "https://inderpsingh.blogspot.com/2014/08/demowebapp_24.html"
distance_id_locator = "distance"
speed_id_locator = "speed"
wait_time_out = 15
driver = webdriver.Firefox(executable_path=exec_path)
wait_variable = W(driver, wait_time_out)
driver.get(URL)
driver.execute_script("window.scrollBy(0,120)","")
distance_element = wait_variable.until(E.presence_of_element_located((By.ID, distance_id_locator)))
distance_element.send_keys("1000")
speed_element = wait_variable.until(E.presence_of_element_located((By.ID, speed_id_locator)))
speed_element.send_keys("50")
# When this script is running, move the mouse pointer to the Calculate Travel Days button.
time.sleep(5)
x, y = P.position()
# Print the x and y coordinates of the Calculate Travel Days button.
print ("X is ", str(x), "Y is ", str(y))
# Give the x and y coordinates printed by the print statement above e.g. 222, 573. The 3rd argument (optional) is the time the mouse pointer should take to move to the x and y coordinates.
# Note: After locating the x, y coordinates by running this Python script once, you may comment out the above lines from time.sleep(5) to print ("X is ", str(x), "Y is ", str(y))
# PyAutoGUI mouse operations
P.moveTo(222, 573, 3)
# Python Selenium Mouse Click
P.leftClick()

Want to learn more details? Want to see the above Selenium Python examples working? Then, please view my Selenium Python Mouse Move and Mouse Click Tutorial. Thank you.