Skip to main content
Practice

Frequently Used Selenium Methods

In this lesson, we will introduce some additional methods commonly used in Selenium.


Waiting for Page Load with implicitly_wait Method

Since the loading speed of web pages varies, it's common to encounter situations where you need to wait for a specific element to appear.

The implicitly_wait method sets a maximum wait time for the page to load.

Example of Waiting for Page Load
from selenium import webdriver

# Create a Chrome WebDriver instance
driver = webdriver.Chrome()

# Wait up to 10 seconds for the page to load
driver.implicitly_wait(10)

# Navigate to a specific URL
driver.get("https://www.example.com")

By using the implicitly_wait method, you can wait for the page to load completely without unnecessary time wastage.


Waiting for Specific Conditions with WebDriverWait Class

The WebDriverWait class allows you to wait until a specific condition (e.g., an HTML element exists, is clickable) is met.

For example, you can use it with the presence_of_element_located method from the expected_conditions module to wait for a specific element to appear.

The code below waits up to 10 seconds for a specific element to appear.

Example of presence_of_element_located Method
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Wait up to 10 seconds for myDynamicElement to appear
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "myDynamicElement"))
)

Commonly Used expected_conditions Methods

  • presence_of_element_located: Wait until an element appears

  • element_to_be_clickable: Wait until an element is clickable

  • text_to_be_present_in_element: Wait until an element contains specific text


Selecting from Dropdown Menus with Select Class

When you need to select a specific item from a dropdown menu on a web page, use the Select class.

The code below is an example of selecting "Option 1" from a dropdown menu.

Example of Selecting from a Dropdown Menu
from selenium.webdriver.support.ui import Select

# Locate the dropdown menu element
select_element = driver.find_element("id", "dropdownMenu")

# Create a Select object
select = Select(select_element)

# Select Option 1
select.select_by_visible_text("Option 1")

Saving Screenshots with save_screenshot Method

During automation tasks, you may want to record the state of a web page at a specific moment or debug issues by saving a screenshot using the save_screenshot method.

Note: The save_screenshot method is not supported in the current practice environment. You can install Python and the Selenium library on your computer and confirm its operation by running the script.

Example of Saving a Screenshot
from selenium import webdriver

# Create a Chrome WebDriver instance
driver = webdriver.Chrome()

# Open a specific web page
driver.get('https://www.example.com')

# Save a screenshot file (not supported in the practice environment)
driver.save_screenshot("screenshot.png")

# Close the browser
driver.quit()

The save_screenshot method saves the current browser screen as an image file.

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.