CodeGym /Java Course /Python SELF EN /Introduction to XPath and CSS Selectors

Introduction to XPath and CSS Selectors

Python SELF EN
Level 35 , Lesson 4
Available

1. What are XPath and CSS Selectors?

Today, we’re taking a huge step towards mastering web automation with Selenium as we dive into XPath and CSS Selectors. These small yet powerful tools are gonna be your trusty sidekicks for finding elements on web pages. Let's dive together into the world of selectors and learn how to use them for pinpoint accurate and efficient searching.

If an HTML document were a dense forest, then XPath and CSS Selectors would be your cheat-sheet maps. They let you carve a path to the right tree (or element, if you're into programming). These tools help locate elements on web pages, even when they're hidden behind tons of code and a gazillion tags.

XPath

XPath (XML Path Language) is basically a language for navigating in XML documents. But someone must’ve said that HTML is a hybrid XML, right? That’s why XPath is so handy for HTML documents. It can literally lead you to any element along the path you specify.

CSS Selectors

CSS Selectors hail from the world of Cascading Style Sheets. Don’t worry, you don’t need to be a designer to get this! They’re used to precisely target elements by their type, class, ID, and even state. Using them is easier than you think — you’ve probably already used them unconsciously while styling web pages.

2. Applying XPath and CSS Selectors in Code

Now that you know what these selector wonders are, let’s jump straight into practice. We'll learn how to use them with Selenium and do some magic!

Using XPath

Here’s an example of how you can use XPath to find an element in Selenium:

Python

from selenium import webdriver

# Setting up the driver
driver = webdriver.Chrome()

# Opening the page
driver.get('https://example.com')

# Finding an element with XPath
element = driver.find_element_by_xpath('//div[@id="menu"]/ul/li/a')

# Printing the element's text
print(element.text)

# Closing the browser
driver.quit()

Explanation:

  • //div[@id="menu"]/ul/li/a — this is our XPath. It says, "Hey driver, find me the a element that’s inside li, which is inside ul, which is in div with id="menu".

Using CSS Selectors

Now let’s see how we can use CSS Selectors:

Python

from selenium import webdriver

# Setting up the driver
driver = webdriver.Chrome()

# Opening the page
driver.get('https://example.com')

# Finding an element with a CSS Selector
element = driver.find_element_by_css_selector('div#menu > ul > li > a')

# Printing the element's text
print(element.text)

# Closing the browser
driver.quit()

Explanation:

  • div#menu > ul > li > a — our CSS Selector. It looked for the element just like XPath, but with a more concise syntax.

3. Differences Between XPath and CSS Selectors

So, you’re asking what’s the difference between XPath and CSS Selectors? Great question! Let’s break it down when to use what.

Flexibility vs. Simplicity

XPath is more flexible: you can move “up” the DOM tree and use complex logical conditions. This makes it super helpful for more complex queries. However, sometimes the simplicity of CSS Selectors and their succinctness make them the better choice. CSS Selectors are easier to read and write, especially when you need to find an element by class or ID.

Function Support

XPath supports functions — from checking text to working with attributes, like contains() or starts-with(). CSS Selectors, on the other hand, don’t have this level of functionality.

Performance

In some cases, CSS Selectors are faster. That’s because browsers are natively optimized to work with CSS Selectors, making them the go-to choice for simpler tasks.

Syntax

XPath comes with a more complex syntax, which can be both an advantage and a drawback. CSS Selectors, on the other hand, are simpler to write and learn.

4. Real-World Application

Let’s put this knowledge into action for a real task. Say we have a web page with a product table, and we need to gather all product names and their prices. Here’s how we can do it:

Example Using XPath

Python

from selenium import webdriver

# Setting up the driver
driver = webdriver.Chrome()

# Opening the page
driver.get('https://example.com/products')

# Finding all product elements
products = driver.find_elements_by_xpath('//table[@class="product-table"]/tbody/tr')

# Extracting data for each product
for product in products:
    name = product.find_element_by_xpath('.//td[@class="product-name"]').text
    price = product.find_element_by_xpath('.//td[@class="product-price"]').text
    print(f"Product: {name}, Price: {price}")

# Closing the browser
driver.quit()

Example Using CSS Selectors

Python

from selenium import webdriver

# Setting up the driver
driver = webdriver.Chrome()

# Opening the page
driver.get('https://example.com/products')

# Finding all product elements
products = driver.find_elements_by_css_selector('table.product-table > tbody > tr')

# Extracting data for each product
for product in products:
    name = product.find_element_by_css_selector('td.product-name').text
    price = product.find_element_by_css_selector('td.product-price').text
    print(f"Product: {name}, Price: {price}")

# Closing the browser
driver.quit()

5. Tips and Common Pitfalls

When you use XPath and CSS Selectors, there are a couple of traps to watch out for. For example, when you use absolute paths in XPath, there’s a risk that the tiniest change in the HTML structure will break your script. So, always try to use relative paths to stay flexible.

CSS Selectors, on the other hand, can become unreadable if they get too complex, so it’s important to strike a balance between precision and simplicity.

Also, don't forget about error handling. If an element isn’t found, Selenium throws a NoSuchElementException. Use try-except blocks or waiting methods like WebDriverWait to handle this and make your scripts more robust.

1
Task
Python SELF EN, level 35, lesson 4
Locked
Basic use of XPath
Basic use of XPath
2
Task
Python SELF EN, level 35, lesson 4
Locked
Using CSS Selectors
Using CSS Selectors
3
Task
Python SELF EN, level 35, lesson 4
Locked
Searching elements on a page
Searching elements on a page
4
Task
Python SELF EN, level 35, lesson 4
Locked
Comparing XPath and CSS Selectors
Comparing XPath and CSS Selectors
1
Опрос
Getting Started with Selenium,  35 уровень,  4 лекция
недоступен
Getting Started with Selenium
Getting Started with Selenium
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION