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

Sunday, June 21, 2020

Selenium Python Tutorial 7 | Python selenium Loop through Links | Selenium Python Link Click

Moving on to the next tutorial in the Selenium Python Tutorials for Beginners. This Selenium Python beginner tutorial explains selenium python link click, selenium link text and how to loop through links with explicit wait. First, view the Selenium Python Link Text Link Click Tutorial. Then read on. 
Here is my full Selenium Python example. I have put explanations in the comments within the code (lines or phrases starting with #).

# Selenium WebDriver Python coding
# Import webdriver library to use the Selenium WebDriver commands.
from selenium import webdriver
# Import the By library to find the links on the web page.
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

exec_path = r"E:\Training\SeleniumPython\Downloads\geckodriver-v0.26.0-win32\geckodriver.exe"
URL = "https://inderpsingh.blogspot.com/"
wait_time_out = 15

driver = webdriver.Firefox(executable_path=exec_path)
# Navigate to the URL.
driver.get(URL)
wait_variable = W(driver, wait_time_out)
# We need to find the link before Python Selenium click link .
# Define the Python list called links.
links = wait_variable.until(E.visibility_of_any_elements_located((By.TAG_NAME, "a")))
print ("The total number of links is", len(links))
# Use for each loop to python selenium loop through links. It takes time to loop through links.
for link in links:
    print (link.text) # Selenium link text
# Selenium link click on the "Selenium Python Tutorials" link using WebDriverWait Python
wait_variable.until(E.element_to_be_clickable((By.LINK_TEXT, "Selenium Python Tutorials"))).click()
# Go back to the home page.
driver.back()
# Click the same link but with the partial link text to Selenium link click Python.
wait_variable.until(E.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Selenium Python"))).click()

Want to learn more? Please view my Selenium Python Link Click and Link Text Tutorial. Thank you.

Sunday, June 14, 2020

Selenium Python Tutorial 6 | iFrame Frame and Alert

This is the next tutorial in the Selenium Python Tutorials for Beginners. This Selenium Python beginner tutorial explains how to handle selenium python frames and selenium python alert handling. A frame or iFrame is a smaller web page within the main web page. Selenium WebDriver cannot directly find a web element that is inside a frame. It raises the unable to find element exception. Therefore, you need to switch to the frame and then find the web element. An alert is a message box that pops up on the web page. Selenium WebDriver can dismiss or accept the alert. First, view the Selenium Python iFrame Alert Tutorial. Then read on.
Here is my full Selenium Python example. I have put explanations in the comments within the code (lines or phrases starting with #).

# Selenium WebDriver Python coding
from selenium import webdriver
from selenium.webdriver.common.by import By
# Import two libraries needed for WebDriverWait Python
from selenium.webdriver.support.ui import WebDriverWait as W
from selenium.webdriver.support import expected_conditions as E
import time

exec_path = r"E:\Training\SeleniumPython\Downloads\geckodriver-v0.26.0-win32\geckodriver.exe"
URL = "https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_alert"
# Selenium Python iframe locator on the web page
frame_id_locator = "iframeResult"
button_css_locator = "body > button:nth-child(2)"
wait_time_out = 5

driver = webdriver.Firefox(executable_path=exec_path)
driver.get(URL)

# Selenium WebDriver code to find all the frames on the web page:
frames = driver.find_elements_by_tag_name("iFrame")
print("There are",len(frames),"frames on this web page.")
for f in frames:
    print("Frame Id:", f.get_attribute('id'), ",Frame Name:", f.get_attribute('name'), ",Frame Source:", f.get_attribute('src'))

wait_variable = W(driver, wait_time_out)
# Selenium Python iframe switch after waiting for the frame to be available
wait_variable.until(E.frame_to_be_available_and_switch_to_it(frame_id_locator))
wait_variable.until(E.presence_of_element_located((By.CSS_SELECTOR, button_css_locator))).click()
# Wait for the alert to appear.
wait_variable.until(E.alert_is_present())
time.sleep(2)
# Accept the Python Selenium alert.
driver.switch_to.alert.accept()
time.sleep(2)
# From selenium python iframe switch back to the main web page.
driver.switch_to.default_content()

Want to learn more and see Selenium WebDriver in action? Then, please view my Selenium Python Frames Alerts Tutorial. Thank you.

Sunday, June 7, 2020

Selenium Python Tutorial 5 | Open Window or Tab | Python Selenium Switch To Window

This is the fifth tutorial in the Selenium Python Tutorials for Beginners. This Selenium Python beginner tutorial explains how to open another window or tab in the browser with Selenium WebDriver. First, view the Selenium Python Open Window or Tab Tutorial. Then read on.
Here is my Selenium Python example. I have put explanations in the comments within the code (lines or phrases starting with #).

# Selenium WebDriver Python coding
from selenium import webdriver
# Give the location of the browser driver (the executable path). In Python, r means a raw string.
exec_path = r"E:\Training\SeleniumPython\Downloads\geckodriver-v0.26.0-win32\geckodriver.exe"
URL = r"https://www.wikipedia.org/"
# English language URL: https://en.wikipedia.org/
# Japanese language URL: https://ja.wikipedia.org/
# French language URL: https://fr.wikipedia.org/

# Define a Python list of languages.
languages = ["en", "ja", "fr"]
driver = webdriver.Firefox(executable_path=exec_path)
driver.get(URL)
# Print the Selenium WebDriver window handle of the original browser window.
print ("Window handle of the current window is", driver.current_window_handle)

for i in range(len(languages)):
    # Call Selenium WebDriver execute script method with Javascript to open another tab.
    driver.execute_script("window.open()")
    # Call Selenium WebDriver switch to method.
    # Selenium WebDriver window handles index 0 is the original tab.
    # In the for loop, use indexes starting from 1.
    driver.switch_to.window(driver.window_handles[i+1])
    language_URL = r"https://" + languages[i] + ".wikipedia.org/"
    driver.get(language_URL)
    print (language_URL, driver.window_handles[i+1], driver.title, driver.current_url)

Want to learn more? Want to see the above code working? Then, please view my Selenium Python Open Window or Tab Tutorial. Thank you.