Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Selenium WebDriver Browser Operations and Element Locating Strategies

Tech 1

WebDriver Common Properties

Property Description
driver.name Browser name
driver.current_url Current page URL
driver.title Current page title
driver.page_source Current page HTML source
driver.current_window_handle Current window handle
driver.window_handles All window handles

Browser Basic Operations

Browser Initialization

Method Purpose
Chrome() Initialize Chrome browser
get() Navigate to specified URL
quit() Close browser session
# Different browser initialization examples
# driver = webdriver.Firefox()
# driver = webdriver.Chrome()
# driver = webdriver.Ie()
# driver = webdriver.Edge()
# driver = webdriver.Opera()

from selenium import webdriver

# Initialize Chrome with specific driver path
chromedriver_path = "D:\\drivers\\chromedriver.exe"
browser = webdriver.Chrome(executable_path=chromedriver_path)

# Navigate to Baidu
browser.get("http://www.baidu.com/")

# Close browser
browser.quit()

Window Management

Method Purpose
maximize_window() Maximize browser window
minimize_window() Minimize browser window
fullscreen_window() Enter fullscreen mode
set_window_size(width, height) Set window dimensions
set_window_position(x, y) Set window position
from time import sleep

# Window manipulation examples
browser.maximize_window()
sleep(2)

browser.minimize_window()
sleep(2)

browser.fullscreen_window()
sleep(2)

browser.set_window_size(400, 300)
sleep(2)

browser.set_window_position(200, 100)
sleep(2)

Page Navigation

Method Purpose
forward() Navigate forward
back() Navigate back
refresh() Refresh current page
close() Close current window
from selenium import webdriver
from time import sleep

# Configure Chrome options
chrome_options = webdriver.ChromeOptions()
driver_path = "D:\\drivers\\chromedriver.exe"
web_browser = webdriver.Chrome(driver_path, options=chrome_options)

# Navigate between pages
web_browser.get("http://www.baidu.com")
sleep(2)

web_browser.get("https://www.eastmoney.com/")
sleep(2)

# Navigate back to Baidu
web_browser.back()
sleep(2)

# Refresh page
web_browser.refresh()
sleep(1)

# Navigate forward
web_browser.forward()

# Close current window
web_browser.close()

Element Locating Strategies

Web automation requires precise element identification. Selenium provides eight primary element location methods.

Element Location Methods

Method Description
find_element_by_id() Locate by ID attribute
find_element_by_name() Locate by name attribute
find_element_by_link_text() Locate by exact link text
find_element_by_partial_link_text() Locate by partial link text
find_element_by_tag_name() Locate by HTML tag name
find_element_by_class_name() Locate by class name
find_element_by_xpath() Locate using XPath expression
find_element_by_css_selector() Locate using CSS selector

Note on element finding methods:

  • find_element_by_xx() returns the first matching element or raises an expection
  • find_elements_by_xx() returns a list of matching elements (empty list if none found)

ID-Based Location

ID attributes are unique in HTML documents, making them ideal for element location.

from selenium import webdriver

browser = webdriver.Chrome()
browser.get('https://www.baidu.com')

# Locate search box by ID and input text
search_input = browser.find_element_by_id("kw")
search_input.send_keys("automation testing")

Name-Based Location

from selenium import webdriver

browser = webdriver.Chrome()
browser.get('https://www.baidu.com')

# Locate element by name attribute
search_field = browser.find_element_by_name("wd")
search_field.send_keys("web automation")

Class Name Location

from selenium import webdriver

browser = webdriver.Chrome()
browser.get('https://www.baidu.com')

# Locate by single class name
search_box = browser.find_element_by_class_name('s_ipt')
search_box.send_keys('selenium')

# For compound classes, use only one class name
wrapper = browser.find_element_by_class_name('s_ipt_wr')

Tag Name Location

HTML tags define element functionality. Multiple elements often share the same tag name.

from selenium import webdriver

browser = webdriver.Chrome("D:\\chromedriver.exe")
browser.get("http://www.baidu.com")

# Find all elements with specific tag
area_elements = browser.find_elements_by_tag_name("area")
if area_elements:
    area_elements[0].click()

Link Text Location

from selenium import webdriver
import time

browser = webdriver.Chrome()
browser.get('https://www.baidu.com')

# Click link with exact text match
news_link = browser.find_element_by_link_text('新闻')
news_link.click()
time.sleep(3)
browser.quit()

Partial Link Text Location

from selenium import webdriver
from time import sleep

chrome_options = webdriver.ChromeOptions()
driver_path = "D:\\drivers\\chromedriver.exe"
browser = webdriver.Chrome(driver_path, options=chrome_options)

browser.get("http://www.baidu.com")
search_input = browser.find_element_by_id("kw")
search_input.send_keys("selenium")
sleep(1)

search_button = browser.find_element_by_id("su")
search_button.click()
sleep(2)

# Click link containing partial text
selenium_link = browser.find_element_by_partial_link_text("selenium中文网")
selenium_link.click()

sleep(3)
browser.quit()

XPath Location

XPath provides flexible element location when standard attributes are unavailable or unreliable.

XPath Expression Types

Absolute Path: Full path from root element

xpath_expression = "/html/body/div[1]/div[1]/div/form/span[1]/input"
element = browser.find_element_by_xpath(xpath_expression)

Relative Path: Flexible path from current context

xpath_expression = "//input[@id='kw']"
element = browser.find_element_by_xpath(xpath_expression)

Common XPath Patterns

# By tag type
xpath_expression = "//input"

# By position
xpath_expression = "//input[2]"
xpath_expression = "//div[last()-1]"

# By attribute
xpath_expression = "//input[@value]"
xpath_expression = "//input[@value='kw']"

# Using operators
xpath_expression = "//input[@value='kw' and @id='ad']"
xpath_expression = "//input[@maxlength<256]"

# Hierarchy and attributes
xpath_expression = "//form/input"
xpath_expression = "//div[@class]/a[3]"

# Wildcards
xpath_expression = "//tools/*"
xpath_expression = "//*[@*='s_tab']"

# Partial matching
xpath_expression = "//a[contains(@href,'logout')]"
xpath_expression = "//a[contains(text(),'退出')]"
from selenium import webdriver
import time

browser = webdriver.Chrome()
browser.get('https://www.baidu.com')

# Locate using XPath
search_element = browser.find_element_by_xpath('//*[@id="kw"]')
search_element.send_keys('xpath example')

time.sleep(3)
browser.quit()

CSS Selector Location

CSS selectors offer concise syntax and faster performance compared to XPath.

Selector Example Description
#id #firstname Select element with id="firstname"
.class .intro Select elements with class="intro"
element>element div > p Select elements with parent
[attribute=value] [target=_blank] Select elements with target="_blank"
from selenium import webdriver
import time

browser = webdriver.Chrome()
browser.get('https://www.baidu.com')

# Verify page title
assert '百度' in browser.title

# Clear and input using CSS selector
search_input = browser.find_element_by_css_selector('#kw')
search_input.clear()
search_input.send_keys('css selector')

time.sleep(3)
browser.quit()

Modern Location Approach

Current Selenium versions recommend using the unified By class for element location.

from selenium import webdriver
from selenium.webdriver.common.by import By

browser = webdriver.Chrome("D:\\chromedriver.exe")
browser.get("http://www.baidu.com")

# Modern element location syntax
search_input = browser.find_element(By.ID, "kw")
search_input.send_keys("selenium")

# Alternative approach for multiple elements
search_fields = browser.find_elements(By.NAME, "wd")
if search_fields:
    search_fields[0].send_keys("selenium")

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.