How to run First Script in Selenium with Python
Selenium is a popular open-source automation testing tool used to automate web browsers. It allows we to simulate user interaction with a website, automate repetitive tasks, and test web applications. Python is one of the most popular programming languages used for Selenium automation testing.
To configure the Selenium with python Click Here
In this blog, we will guide we through the steps to run our first Selenium script using Python. We assume that we have already installed Python and Selenium on our system.
Step 1: Launch the browser
The first step is to launch the browser. For this example, we will use Google Chrome. We can download the latest version of Chrome from the official website. Once we have installed Chrome, we also need to download the appropriate driver for Our operating system. We can download the Chrome driver from the following link: https://sites.google.com/a/chromium.org/chromedriver/downloads.
After downloading the Chrome driver, we need to specify the path to the driver in our Selenium script.
Here is the code to launch the Chrome browser:
from selenium import webdriver driver_path = "path/to/chromedriver.exe" driver = webdriver.Chrome(executable_path=driver_path)
Step 2: Navigate to a website
The next step is to navigate to a website. For this example, we will navigate to the Google homepage.
Here is the code to navigate to the Google homepage:
driver.get("https://www.google.com/")
Step 3: Interact with the webpage
The final step is to interact with the webpage. For this example, we will search for the keyword “Python” on Google.
Here is the code to interact with the webpage:
search_box = driver.find_element_by_name("q")
search_box.send_keys("Python")
search_box.submit()
Step 4: Close the browser
After we have finished interacting with the webpage, we need to close the browser.
Here is the code to close the browser:
driver.quit()
The final script should look something like this:
from selenium import webdriver
driver_path = "path/to/chromedriver.exe"
driver = webdriver.Chrome(executable_path=driver_path)
driver.get("https://www.google.com/")
search_box = driver.find_element_by_name("q")
search_box.send_keys("Python")
search_box.submit()
Step 5: Run the script
Save the above code in a file named “selenium_example.py” and run it using the following command:
python selenium_example.py
We should see the web browser launch and navigate to Google, enter a search query and display the search results.