Automate Browser with Selenium Python

Selenium is an open-source tool that we use to automate web browsers. Python is a programming language that we can use in automation and scripting. When used together, Selenium and Python can be a powerful combination for automating web applications, testing web pages, and scraping data from websites. In this article, we will explore the basics of Selenium with Python and show some examples of how to use them in action.

Setting up Selenium with Python

First, let’s take a look at how to set up Selenium with Python. To use, you need to have both the Selenium WebDriver and the Python bindings installed. You can install them using pip, the package manager for Python:

pip install selenium

Once you have installed the Selenium package, you must also download the appropriate WebDriver for the browser you want to automate. For example, if you’re going to automate Chrome, you need to download the ChromeDriver from the official website. Once the WebDriver is installed, you must add its location to your system PATH so that Selenium can find it. To do this, add the path to the WebDriver to the PATH environment variable.

Using Selenium with Python to open a web page

Now that you have everything set up, let’s see some examples of how to use Selenium with Python. In this example, we will use Selenium to open a web page and get the page’s title.

from selenium import webdriver

# We must create a new Chrome browser instance
browser = webdriver.Chrome()

# navigate to the URL
browser.get("https://www.example.com")

# get the title of the page
title = browser.title

# print the title to the console
print(title)

# close the browser
browser.quit()

In this example, we have imported the web driver module present in the selenium package. We then create a new instance of the Chrome browser using the webdriver.Chrome() function. We use the get() function to navigate to the URL we want to open, and then we use the title property to get the page’s title. Finally, we print the title to the console and close the browser using the quit() function.

Using Selenium with Python to interact with web pages

Another helpful feature of Selenium is the ability to interact with web pages and simulate user actions. In this example, we will use Selenium to fill out and submit a form.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

# Creating a new Chrome browser instance
browser = webdriver.Chrome()

# navigate to the URL
browser.get("https://www.example.com/form")

# find the name and email fields and fill them out
name_field = browser.find_element_by_name("name")
name_field.send_keys("John Doe")

email_field = browser.find_element_by_name("email")
email_field.send_keys("[email protected]")

# find the submit button and click it
submit_button = browser.find_element_by_xpath("//input[@type='submit']")
submit_button.click()

# close the browser
browser.quit()

In this example, we first import the Keys module from the Selenium.webdriver.common.keys package, which allows us to simulate keyboard actions. We then use the find_element_by_name() function to find the name and email fields of the form, and we use the send_keys() function to fill them out. We then use the find_element_by_xpath() function to find the submit button of the form, and we use the click() function to click it. Ultimately, we close the browser using the quit() function.

Using Selenium with Python for web scraping

Selenium is also helpful in scraping data from websites. In this example, we will use Selenium to scrape a table from a web page and save it as a CSV file.

import csv
from selenium import webdriver

# Creating a new Chrome browser instance
browser = webdriver.Chrome()

# navigate to the URL
browser.get("https://www.example.com/table")

In this example, we first import the CSV module, which allows us to write data to a CSV file. After which, a new instance of the Chrome browser gets created using the webdriver.Chrome() function and navigate to the URL we want to scrape.

After that, we use the find_element_by_xpath() function to find the table on the web page, and we use the find_elements_by_tag_name() function to find all the rows and cells of the table. We then loop through the rows and cells of the table and write the data to a CSV file using the csv.writer() function. Finally, we close the browser using the quit() function

import csv
from selenium import webdriver

# Creating a new Chrome browser instance
browser = webdriver.Chrome()

# navigate to the URL
browser.get("https://www.example.com/table")

# find the table element and all its rows and cells
table = browser.find_element_by_xpath("//table")
rows = table.find_elements_by_tag_name("tr")

# create a new CSV file and write the table data to it
with open("table.csv", "w") as csvfile:
    writer = csv.writer(csvfile)
    for row in rows:
        cells = row.find_elements_by_tag_name("td")
        row_data = [cell.text for cell in cells]
        writer.writerow(row_data)

# close the browser
browser.quit()

Selenium Python button click

To click a button using Selenium in Python, you can follow these steps:

  1. Import the necessary modules: You will need to import the webdriver module from the selenium package.
  2. Create an instance of the webdriver: You can create an instance of the webdriver class to open a browser window. You can choose the browser of your choice, such as Chrome, Firefox, or Edge.
  3. Navigate to the webpage: You can use the get() method to navigate to the webpage where the button is located.
  4. Locate the button: You can use one of the methods provided by Selenium to locate the button element, such as find_element_by_id(), find_element_by_name(), find_element_by_xpath(), or find_element_by_css_selector(), depending on how the button is defined in the HTML code.
  5. Click the button: Once you have located the button element, you can use the click() method to simulate a click on the button.

Here is an example code snippet that demonstrates how to click a button using Selenium in Python:

from selenium import webdriver

# Create an instance of the webdriver (in this case, Chrome)
driver = webdriver.Chrome()

# Navigate to the webpage with the button
driver.get("https://example.com")

# Locate the button element (in this case, by ID)
button = driver.find_element_by_id("my-button")

# Click the button
button.click()

# Close the browser window
driver.quit()

In this example, we are using the Chrome browser and navigating to the https://example.com webpage. We are locating the button element by its ID (my-button) and clicking it using the click() method. Finally, we are closing the browser window using the quit() method.

Selenium find element

In Selenium, you can use various methods to locate elements on a webpage. Here are some of the most common methods to find elements using Selenium:

  1. By ID: You can use the find_element_by_id() method to locate an element by its ID attribute.
element = driver.find_element_by_id("my-element-id")

  1. By name: You can use the find_element_by_name() method to locate an element by its name attribute.
element = driver.find_element_by_name("my-element-name")

  1. By class name: You can use the find_element_by_class_name() method to locate an element by its class name.
element = driver.find_element_by_class_name("my-element-class")

  1. By tag name: You can use the find_element_by_tag_name() method to locate an element by its HTML tag name.
element = driver.find_element_by_tag_name("a")

  1. By CSS selector: You can use the find_element_by_css_selector() method to locate an element using a CSS selector.
element = driver.find_element_by_css_selector(".my-class > a")

  1. By XPath: You can use the find_element_by_xpath() method to locate an element using an XPath expression.
element = driver.find_element_by_xpath("//div[@class='my-class']/a")

In all of these methods, the driver object refers to the instance of the webdriver class that you have created to interact with the webpage.

Once you have located an element, you can interact with it by calling various methods on the element object, such as click(), send_keys(), text, and many more, depending on the type of element and the action you want to perform on it.

Selenium get attribute

In Selenium, you can use the get_attribute() method to retrieve the value of an attribute of an element on a webpage. The get_attribute() method takes a single argument, which is the name of the attribute you want to retrieve.

Here is an example code snippet that demonstrates how to use the get_attribute() method to retrieve the value of an attribute:

from selenium import webdriver

# Create an instance of the webdriver (in this case, Chrome)
driver = webdriver.Chrome()

# Navigate to the webpage with the element
driver.get("https://example.com")

# Locate the element
element = driver.find_element_by_id("my-element")

# Retrieve the value of the "href" attribute
href_value = element.get_attribute("href")

# Print the value of the "href" attribute
print(href_value)

# Close the browser window
driver.quit()

In this example, we are navigating to the https://example.com webpage and locating an element with an ID of my-element. We are then using the get_attribute() method to retrieve the value of the href attribute of the element, which contains the URL to which the element links. We are then printing the value of the href attribute to the console.

You can use the get_attribute() method to retrieve the values of other attributes as well, such as class, style, name, value, and many more.

Selenium headless

In Selenium, you can run your tests in a headless mode, which means that the browser window will not be displayed on the screen while the tests are running. This can be useful in situations where you don’t need to see the browser window and want to save resources by not rendering the window.

To run your tests in a headless mode using Selenium, you need to use a headless driver, which is a driver that runs in the background without displaying the browser window. Different browsers have different headless drivers available, such as:

  • Chrome: ChromeOptions with --headless option
  • Firefox: FirefoxOptions with --headless option
  • Safari: SafariOptions with TechnologyPreview option
  • Edge: EdgeOptions with --headless option

Here is an example code snippet that demonstrates how to run tests in headless mode using the Chrome driver:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

# Create a ChromeOptions object and set the headless option
chrome_options = Options()
chrome_options.add_argument('--headless')

# Create an instance of the Chrome driver with the ChromeOptions object
driver = webdriver.Chrome(options=chrome_options)

# Navigate to the webpage
driver.get("https://example.com")

# Find and interact with elements as usual
element = driver.find_element_by_id("my-element")
element.click()

# Close the browser window
driver.quit()

In this example, we are using the ChromeOptions class to create a ChromeOptions object and set the --headless option. We are then creating an instance of the Chrome driver with the options parameter set to the ChromeOptions object. We can then navigate to the webpage and interact with elements as usual.

When running tests in headless mode, it’s important to be aware of the potential differences in behavior compared to running tests in a regular mode with a visible browser window. For example, some elements may not be visible or interactable in headless mode, so you may need to adjust your tests accordingly.

Selenium hover

In Selenium, you can simulate mouse hover events over elements using the ActionChains class. The ActionChains class allows you to perform a series of actions, such as clicking, hovering, dragging, and more.

Here is an example code snippet that demonstrates how to perform a hover action using ActionChains:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

# Create an instance of the webdriver (in this case, Chrome)
driver = webdriver.Chrome()

# Navigate to the webpage with the element
driver.get("https://example.com")

# Locate the element that you want to hover over
element = driver.find_element_by_id("my-element")

# Create an instance of the ActionChains class
action_chains = ActionChains(driver)

# Move the mouse to the center of the element
action_chains.move_to_element(element).perform()

# Close the browser window
driver.quit()

In this example, we are navigating to the https://example.com webpage and locating an element with an ID of my-element. We are then creating an instance of the ActionChains class and using the move_to_element() method to move the mouse to the center of the element. We are then calling the perform() method to execute the action.

Selenium refresh page

In Selenium, you can refresh the current webpage using the refresh() method of the webdriver object. Here is an example code snippet that demonstrates how to refresh a webpage using Selenium:

from selenium import webdriver

# Create an instance of the webdriver (in this case, Chrome)
driver = webdriver.Chrome()

# Navigate to the webpage that you want to refresh
driver.get("https://example.com")

# Refresh the webpage
driver.refresh()

# Close the browser window
driver.quit()

In this example, we are creating an instance of the webdriver class and navigating to the https://example.com webpage. We are then using the refresh() method to refresh the page. Finally, we are closing the browser window using the quit() method.

Selenium waits for the element

In Selenium, you can wait for an element to be present, visible, clickable, or have a certain attribute value using the WebDriverWait class. The WebDriverWait class provides a way to wait for a certain condition to be met before proceeding with the script.

Here is an example code snippet that demonstrates how to wait for an element to be present using the WebDriverWait class:

python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Create an instance of the webdriver (in this case, Chrome)
driver = webdriver.Chrome()

# Navigate to the webpage with the element
driver.get("https://example.com")

# Wait for the element to be present
wait = WebDriverWait(driver, 10)
element = wait.until(EC.presence_of_element_located((By.ID, "my-element")))

# Close the browser window
driver.quit()

In this example, we are navigating to the https://example.com webpage and waiting for an element with an ID of my-element to be present on the page. We are using the WebDriverWait class to wait for up to 10 seconds for the element to be present. The presence_of_element_located method of the expected_conditions module is used to wait for the presence of an element with a locator (in this case, the ID my-element). Once the element is present, it is stored in the element variable.

Selenium Vs. BeautifulSoup

Selenium is primarily used for web automation and testing. It simulates user behavior by controlling a web browser (such as Chrome, Firefox, or Edge) to interact with web pages. With Selenium, you can navigate to a webpage, fill out forms, click buttons, and scrape dynamic content requiring interaction with JavaScript.

On the other hand, Beautiful Soup is a library for parsing HTML and XML documents. It provides a convenient way to extract information from web pages by traversing the HTML/XML tree and finding specific tags or attributes. Beautiful Soup is best used for extracting data from static web pages, where the content is already loaded in the HTML source and does not require interaction with JavaScript.

In summary, if you need to interact with dynamic web pages that require JavaScript, then Selenium is a better choice. If you need to extract data from static web pages, then Beautiful Soup is a good choice. However, in some cases, you may want to use both libraries together, with Selenium to navigate and interact with web pages and Beautiful Soup to parse the HTML and extract data.

Scrapy Vs. Selenium

Scrapy is an open-source web crawling framework used to extract data from websites. It provides a powerful and efficient way to scrape large amounts of data from multiple websites at scale. Scrapy is designed to handle a high volume of requests, which makes it a good choice for scraping large datasets from websites that don’t require JavaScript to load the data.

Selenium, on the other hand, is primarily used for web automation and testing, as I mentioned in the previous answer. It can be used for web scraping, but its focus is on interacting with websites that require JavaScript to load content. Selenium allows you to simulate user behavior by controlling a web browser, which can be useful for scraping data from dynamic websites.

In summary, if you need to scrape a large amount of data from multiple websites that don’t require JavaScript to load content, Scrapy is the better choice. If you need to scrape data from websites that require JavaScript to load content or if you need to interact with the website in some way (such as filling out forms), then Selenium is the better choice.

Using Proxy in Selenium

Selenium can be configured to use a proxy server to make requests. This can be useful for several reasons, such as hiding your IP address, bypassing geolocation restrictions, or testing websites from different locations.

Here’s an example of how to configure Selenium to use a proxy server in Python:

from selenium import webdriver

# configure the proxy server
PROXY_HOST = 'proxy.example.com'
PROXY_PORT = 1234
PROXY_USER = 'username'
PROXY_PASS = 'password'

# set up the proxy configuration
proxy_options = {
    'proxy': {
        'httpProxy': f'http://{PROXY_USER}:{PROXY_PASS}@{PROXY_HOST}:{PROXY_PORT}',
        'sslProxy': f'https://{PROXY_USER}:{PROXY_PASS}@{PROXY_HOST}:{PROXY_PORT}',
        'noProxy': 'localhost,127.0.0.1'  # list of hosts that should bypass the proxy
    }
}

# create a new instance of the Chrome browser with the proxy options
browser = webdriver.Chrome(options=webdriver.ChromeOptions().add_experimental_option('proxy', proxy_options))

In this example, we’re creating a new instance of the Chrome browser with proxy options that include the proxy server’s hostname, port number, username, and password. The noProxy option is used to specify a comma-separated list of hosts that should bypass the proxy.

Selenium Python Upload File

To upload a file using Selenium in Python, you can use the send_keys() method to send the file path to the file input field. Here’s an example:

from selenium import webdriver

# create an instance of the Chrome browser
browser = webdriver.Chrome()

# navigate to the page with the file input field
browser.get("https://example.com/upload")

# locate the file input field and send the file path
file_input = browser.find_element_by_name("file")
file_input.send_keys("/path/to/file.pdf")

# submit the form
submit_button = browser.find_element_by_css_selector("button[type='submit']")
submit_button.click()

# close the browser
browser.quit()

In this example, we’re first creating an instance of the Chrome browser and navigating to a page with a file input field. Then we locate the file input field using its name attribute, send the file path to it using the send_keys() method, and submit the form by clicking on the submit button. Finally, we’re closing the browser.

FAQs

What browsers does Selenium with Python support?

Selenium with Python supports various browsers, including Opera, Edge, Chrome, Firefox, and Safari.

Is Selenium with Python easy?

Selenium with Python can be easy to learn if you have some experience with programming in Python and a basic understanding of HTML and CSS, as it provides Python APIs.

Who invented Selenium?

Jason Huggins invented it in 2004.

Which selenium locator is faster?

ID locator in Selenium is the most preferred and fastest way to locate desired WebElements on the page.

Conclusion

In conclusion, it is a powerful combination for automating web applications, testing web pages, and scraping data from websites. Using simple and concise code, you can easily create automated tests, interact with web pages, and extract data from websites. With the versatility and simplicity of Python and the capabilities of Selenium, the possibilities are endless.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments