Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Selenium WebDriver: Core Web Element Locators and Practical Operations

Tech 1

To begin browser automation with Selenium WebDriver, initialize a browser instance and navigate to a target URL:

from selenium import webdriver

# Initialize Chrome browser
chrome_session = webdriver.Chrome()
# Navigate to Baidu homepage
chrome_session.get("https://www.baidu.com")

# Validate navigation via page title
assert "百度" in chrome_session.title, "Incorrect page loaded"
# Validate navigation via current URL
assert "baidu" in chrome_session.current_url, "Incorrect domain loaded"

Selenium WebDriver provides multiple strategies to locate web elements. Below are 8 core approaches:

ID Locator

Use a unique HTML id attribute for precise element targeting:

from selenium import webdriver

chrome_session = webdriver.Chrome()
chrome_session.get("https://www.baidu.com")

# Locate search input by ID and type text
search_box = chrome_session.find_element_by_id("kw")
search_box.send_keys("Python automation")

Name Locator

Target elements using the name attribute:

from selenium import webdriver

chrome_session = webdriver.Chrome()
chrome_session.get("https://www.baidu.com")

chrome_session.find_element_by_name("wd").send_keys("Python automation")

Class Name Locator

Locate elements via the class attribute (note: only one class name is allowed per call):

from selenium import webdriver

chrome_session = webdriver.Chrome()
chrome_session.get("https://www.baidu.com")

chrome_session.find_element_by_class_name("s_ipt").send_keys("Python automation")

Tag Name Locator

HTML tags define element types, but they are rarely unique across a page, so this strategy is mostly for educational purposes:

from selenium import webdriver

chrome_session = webdriver.Chrome()
chrome_session.get("https://www.baidu.com")

# This will likely fail as multiple input tags exist
chrome_session.find_element_by_tag_name("input").send_keys("Python automation")

Link Text Locator

Specifically designed for hyperlinks with exact visible text:

from selenium import webdriver
import time

chrome_session = webdriver.Chrome()
chrome_session.get("https://www.baidu.com")

# Click "News" link
chrome_session.find_element_by_link_text("新闻").click()
time.sleep(4)
chrome_session.close()

Partial Link Text Locator

Fuzzy-match hyperlinks using a substring of theeir visible text:

from selenium import webdriver
import time

chrome_session = webdriver.Chrome()
chrome_session.get("https://www.baidu.com")

chrome_session.find_element_by_partial_link_text("图").click()
time.sleep(4)
chrome_session.close()

XPath Locator

A flexible path-based strategy for locating elements anywhere in the DOM, ideal for dynamic or non-unique attributes:

from selenium import webdriver
import time

chrome_session = webdriver.Chrome()
chrome_session.get("https://www.baidu.com")

# Search using wildcard XPath
chrome_session.find_element_by_xpath("//*[@id='kw']").send_keys("Selenium tips")
time.sleep(4)
chrome_session.close()

CSS Selector Locator

Generally faster and more concise than XPath, uses CSS syntax to target elements:

from selenium import webdriver
import time

chrome_session = webdriver.Chrome()
chrome_session.get("https://www.baidu.com")

# Clear and populate search box with CSS selector
search_box = chrome_session.find_element_by_css_selector("#kw")
search_box.clear()
search_box.send_keys("CSS selectors Selenium")
time.sleep(4)
chrome_session.close()

Extract Text and Validate Content

from selenium import webdriver
import time

chrome_session = webdriver.Chrome()
chrome_session.get("https://www.baidu.com")
assert "百度" in chrome_session.title

# Search and click
search_box = chrome_session.find_element_by_css_selector("#kw")
search_box.send_keys("Selenium text extraction")
search_button = chrome_session.find_element_by_xpath("//*[@id='su']")
search_button.click()

# Wait for results to load
time.sleep(3)

# Extract and validate search results text
results_container = chrome_session.find_element_by_xpath("//*[@id='content_left']")
results_text = results_container.text
print(results_text)
assert "Selenium text extraction" in results_text

time.sleep(5)
chrome_session.quit()

Simulate Keyboard Shortcuts

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

chrome_session = webdriver.Chrome()
chrome_session.get("https://www.baidu.com")
assert "百度" in chrome_session.title

search_box = chrome_session.find_element_by_css_selector("#kw")
search_box.send_keys("Selenium keyboard actions")
time.sleep(2)

# Trigger search with Enter key instead of clicking
search_box.send_keys(Keys.ENTER)
time.sleep(5)
chrome_session.quit()

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.