Sunday, July 5, 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.

2 comments:

  1. Hello frined,
    please I need help for script python in order to open pdf file and do: select all+ copy+ paste and write in text file.
    thank you

    ReplyDelete
    Replies
    1. Hello,
      This requirement does not require Selenium. Please post your comment in the correct blog article at https://inderpsingh.blogspot.com/2019/12/PythonTutorial14.html
      Thank you.

      Delete