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 below. 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.

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 below. 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.

June 07, 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 below. 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.