This is the first tutorial in the Selenium Python Tutorials for Beginners. This Selenium Python beginner tutorial gives an introduction to Python and Selenium WebDriver, pip install Selenium and multiple selenium python example. First, view the Selenium Python tutorial 1. Then read below.
You can do Python browser automation with Selenium WebDriver. Python programming language is also used for web development, game development, machine learning and other applications. The Selenium library Python is open source and free. Selenium tests web applications in multiple browsers.
In order to work with selenium using python, we need to install Python 3, which we can install from the Python official website. Next, how to Python install Selenium? We can use pip, which is the Python package installer. In the command prompt, the pip install Selenium command is below. This command gets the Selenium Python download or the Selenium package from the Selenium official website.
pip install -U selenium
For Python Selenium automation, we can create a new project in the PyCharm IDE. Click File> New Project. Then give the name of the project. In order to configure Selenium WebDriver in this project, select the project. Click File > Settings > Project > Project interpreter. It shows the packages that are available. Click + button, which is the Install button. It shows all the available packages. In the search text box, type selenium. Select selenium and click Install Package button. It gives the message, Package 'selenium' installed successfully. Click close button. Then it shows selenium in the packages list also. Click OK button. Next, right-click the Project > New > Python file. Give a name and click OK button. We need to download the Python WebDriver libraries for the browsers. We can go to the Selenium website and click on the Downloads tab. In the Selenium Client & WebDriver Language Bindings section, find Python and click the Download link. Here are the drivers for the browsers like Firefox (GeckoDriver), Chrome (ChromeDriver) and Edge (EdgeDriver) which we can download. Now, we are ready for selenium using python. Here is my first Selenium Python example:
# Selenium WebDriver Python coding
# Import WebDriver library from the Selenium package
from selenium import webdriver
# Import By to find elements on the web page
from selenium.webdriver.common.by import By
# Give the location of the browser driver. r means a raw Python string.
exec_path = r"E:\Training\SeleniumPython\Downloads\geckodriver-v0.26.0-win32\geckodriver.exe"
URL = r"https://www.wikipedia.org/"
english_link_locator = "js-link-box-en"
search_locator = "searchInput"
search_text = "Software"
# Define the Selenium WebDriver variable with the executable path.
driver = webdriver.Firefox(executable_path=exec_path)
# Navigate to the URL.
driver.get(URL)
driver.maximize_window()
# Find the English link.
english_link_element = driver.find_element(By.ID, english_link_locator)
english_link_element.click()
# Find the Search text box.
input_box_element = driver.find_element(By.ID, search_locator)
input_box_element.send_keys(search_text)
input_box_element.submit()
# The quit method closes the browser windows.
# driver.quit()
In order to work with selenium using python, we need to install Python 3, which we can install from the Python official website. Next, how to Python install Selenium? We can use pip, which is the Python package installer. In the command prompt, the pip install Selenium command is below. This command gets the Selenium Python download or the Selenium package from the Selenium official website.
pip install -U selenium
For Python Selenium automation, we can create a new project in the PyCharm IDE. Click File> New Project. Then give the name of the project. In order to configure Selenium WebDriver in this project, select the project. Click File > Settings > Project > Project interpreter. It shows the packages that are available. Click + button, which is the Install button. It shows all the available packages. In the search text box, type selenium. Select selenium and click Install Package button. It gives the message, Package 'selenium' installed successfully. Click close button. Then it shows selenium in the packages list also. Click OK button. Next, right-click the Project > New > Python file. Give a name and click OK button. We need to download the Python WebDriver libraries for the browsers. We can go to the Selenium website and click on the Downloads tab. In the Selenium Client & WebDriver Language Bindings section, find Python and click the Download link. Here are the drivers for the browsers like Firefox (GeckoDriver), Chrome (ChromeDriver) and Edge (EdgeDriver) which we can download. Now, we are ready for selenium using python. Here is my first Selenium Python example:
# Selenium WebDriver Python coding
# Import WebDriver library from the Selenium package
from selenium import webdriver
# Import By to find elements on the web page
from selenium.webdriver.common.by import By
# Give the location of the browser driver. r means a raw Python string.
exec_path = r"E:\Training\SeleniumPython\Downloads\geckodriver-v0.26.0-win32\geckodriver.exe"
URL = r"https://www.wikipedia.org/"
english_link_locator = "js-link-box-en"
search_locator = "searchInput"
search_text = "Software"
# Define the Selenium WebDriver variable with the executable path.
driver = webdriver.Firefox(executable_path=exec_path)
# Navigate to the URL.
driver.get(URL)
driver.maximize_window()
# Find the English link.
english_link_element = driver.find_element(By.ID, english_link_locator)
english_link_element.click()
# Find the Search text box.
input_box_element = driver.find_element(By.ID, search_locator)
input_box_element.send_keys(search_text)
input_box_element.submit()
# The quit method closes the browser windows.
# driver.quit()
Here is my second Selenium Python example with WebDriverWait Python using multiple language link locators :
# Selenium WebDriver Python coding
from selenium import webdriver
from selenium.webdriver.common.by import By
# Import statements for explicit wait
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 = r"https://www.wikipedia.org/"
# Define a list of locators for the language links.
language_locators = ["js-link-box-en", "js-link-box-ru", "js-link-box-de"]
search_locator = "searchInput"
search_text = "Software"
wait_time = 5
driver = webdriver.Firefox(executable_path=exec_path)
driver.get(URL)
# Define the wait variable for explicit wait.
wait = W(driver, wait_time)
driver.maximize_window()
for i in range(len(language_locators)):
language_link = wait.until(E.presence_of_element_located((By.ID, language_locators[i])))
language_link.click()
input_box_element = wait.until(E.presence_of_element_located((By.ID, search_locator)))
input_box_element.send_keys(search_text)
input_box_element.submit()
# Pause the script for a few seconds.
time.sleep(4)
driver.back()
driver.back()
# driver.quit()
That is all in this Python Automation Testing tutorial. Want to learn more details like how to find the locators? Want to know about the synchronization issue in Test Automation and how to resolve it? Or want to see the above Selenium Python code working? Please view my Selenium Python tutorial 1. Thank you.
# Selenium WebDriver Python coding
from selenium import webdriver
from selenium.webdriver.common.by import By
# Import statements for explicit wait
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 = r"https://www.wikipedia.org/"
# Define a list of locators for the language links.
language_locators = ["js-link-box-en", "js-link-box-ru", "js-link-box-de"]
search_locator = "searchInput"
search_text = "Software"
wait_time = 5
driver = webdriver.Firefox(executable_path=exec_path)
driver.get(URL)
# Define the wait variable for explicit wait.
wait = W(driver, wait_time)
driver.maximize_window()
for i in range(len(language_locators)):
language_link = wait.until(E.presence_of_element_located((By.ID, language_locators[i])))
language_link.click()
input_box_element = wait.until(E.presence_of_element_located((By.ID, search_locator)))
input_box_element.send_keys(search_text)
input_box_element.submit()
# Pause the script for a few seconds.
time.sleep(4)
driver.back()
driver.back()
# driver.quit()
That is all in this Python Automation Testing tutorial. Want to learn more details like how to find the locators? Want to know about the synchronization issue in Test Automation and how to resolve it? Or want to see the above Selenium Python code working? Please view my Selenium Python tutorial 1. Thank you.
Hey there,
ReplyDeleteNice blog
check out our blogs
Best project management company in India