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""
# English language URL:
# Japanese language URL:
# French language URL:

# Define a Python list of languages.
languages = ["en", "ja", "fr"]
driver = webdriver.Firefox(executable_path=exec_path)
# 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.
    # 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.
    language_URL = r"https://" + languages[i] + ""
    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.

No comments:

Post a Comment