LunaNotes

Master Selenium WebDriver Commands: Application, Conditional, Browser, Navigation, and Text Handling

Convert to note

Introduction to Selenium WebDriver Commands

This session covers various Selenium WebDriver command categories to interact with web elements and control browser behavior.

Categories of Commands

  1. Application Commands: Get page information such as title, URL, and source.
  2. Conditional Commands: Validate element properties like visibility, enablement, and selection.
  3. Browser Commands: Manage browser lifecycle with close and quit methods.
  4. Navigational Commands: Control browser navigation with back, forward, and refresh.
  5. Wait Commands: (To be covered in subsequent sessions.)

Application Commands

  • driver.get(URL): Opens the specified URL in the browser.
  • driver.title: Retrieves the current page title.
  • driver.current_url: Fetches the URL of the current page.
  • driver.page_source: Returns the full HTML source of the current page.

Usage Example

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

service = Service('path_to_chromedriver')
driver = webdriver.Chrome(service=service)
driver.get('https://example.com')
print('Title:', driver.title)
print('URL:', driver.current_url)
print('Page source length:', len(driver.page_source))
driver.quit()

For a deeper dive into Selenium automation basics and initial setup, consider the Comprehensive Selenium WebDriver Tutorial: Setup and Basic Automation.


Conditional Commands

Used to verify element states; all accessed via WebElement objects:

  • is_displayed(): Returns True if the element is visible on the page.
  • is_enabled(): Returns True if the element is interactable.
  • is_selected(): Returns True if a checkbox or radio button is selected.

Practical Application

Check visibility and enabled state of an input box, or selection status of radio buttons and checkboxes before performing actions to ensure robust script behavior.


Browser Commands

Manage the browser instance with two primary methods:

  • close(): Closes the focused browser window but leaves the WebDriver session running.
  • quit(): Terminates the entire WebDriver session and closes all associated browser windows.

Key Differences

| Feature | close() | quit() | |--------------------|--------------------------|---------------------------------| | Number of windows | Closes only current window| Closes all windows | | Driver process | Process remains running | Process is terminated |


Navigational Commands

Simulate browser navigation:

  • back(): Goes back to the previous page in browser history.
  • forward(): Moves forward in browser history.
  • refresh(): Reloads the current page.

These are accessed via the driver instance and allow efficient navigation control during automation.


Finding Elements: find_element vs find_elements

| Aspect | find_element | find_elements | |----------------------|----------------------------------------|------------------------------------| | Returns | Single WebElement | List of WebElements (can be empty) | | If multiple matches | Returns the first matched element | Returns all matched elements | | If no element found | Throws NoSuchElementException | Returns an empty list (no exception)|

Practical Tips

  • Use find_element when expecting a single unique element.
  • Use find_elements to retrieve multiple elements, iterate over the list for bulk operations.

Example of iterating multiple footer links:

links = driver.find_elements(By.XPATH, '//div[@class="footer"]//a')
for link in links:
    print(link.text)

Extracting Text from Elements: .text vs .get_attribute('value')

  • .text: Retrieves the visible inner text (text content between the opening and closing tags). Useful for links, buttons, labels.
  • .get_attribute('value'): Retrieves the value of an attribute, commonly used for input fields to get their current value.

Important Distinctions

  • For elements like input boxes, .text returns empty because they have no inner text; use .get_attribute('value') instead.
  • For elements like buttons or links, .text returns the clickable label.

Example

input_box = driver.find_element(By.ID, 'email')
print(input_box.text)  # Usually empty
print(input_box.get_attribute('value'))  # Returns input content

button = driver.find_element(By.ID, 'submit')
print(button.text)  # Returns button label
print(button.get_attribute('value'))  # May be empty if no value attribute

Summary

  • Selenium WebDriver provides categorized commands for page info retrieval, element condition checking, browser and navigation control.
  • Conditional commands ensure element readiness before interaction.
  • Use close() and quit() judiciously depending on the need to close single or all browser windows.
  • Understand differences between find_element and find_elements to avoid exceptions and correctly handle multiple elements.
  • Extract text or attribute values smartly using .text or .get_attribute() depending on element type.

Practicing these commands greatly improves your automation scripts' reliability and efficiency.

For broader context on web automation and related tools, you might find Exploring Puppeteer and Headless Browsers: A Comprehensive Guide insightful. Additionally, strengthening your understanding of HTML and CSS can be beneficial; refer to Comprehensive Guide to HTML and CSS: From Basics to Advanced Techniques for a detailed exploration.

Heads up!

This summary and transcript were automatically generated using AI with the Free YouTube Transcript Summary Tool by LunaNotes.

Generate a summary for free
Buy us a coffee

If you found this summary useful, consider buying us a coffee. It would help us a lot!

Let's Try!

Start Taking Better Notes Today with LunaNotes!