Symfony Panther provides multiple powerful methods for selecting DOM elements, combining the flexibility of CSS selectors with the precision of WebDriver locators. This guide covers all available selection methods with practical examples.
CSS Selector Methods
find()
and findAll()
The most commonly used methods for CSS-based element selection.
use Symfony\Component\Panther\PantherTestCase;
$client = PantherTestCase::createPantherClient();
$crawler = $client->request('GET', 'https://example.com');
// Find first matching element
$firstButton = $crawler->find('button.primary');
$navElement = $crawler->find('#navigation');
// Find all matching elements
$allLinks = $crawler->findAll('a');
$tableRows = $crawler->findAll('tbody tr');
$cards = $crawler->findAll('.card[data-status="active"]');
filter()
Method
Filters the current selection using CSS selectors.
// Chain filters for precise selection
$specificItems = $crawler
->filter('.product-list')
->filter('.item')
->filter('[data-price]');
// Complex CSS selectors
$visibleElements = $crawler->filter('div:not(.hidden)');
$formInputs = $crawler->filter('form input[type="text"], form textarea');
WebDriver Locator Methods
findElement()
and findElements()
Direct WebDriver methods using the By
class for precise element targeting.
use Facebook\WebDriver\WebDriverBy;
$client = PantherTestCase::createPantherClient();
$client->request('GET', 'https://example.com');
// By ID
$element = $client->findElement(WebDriverBy::id('submit-button'));
// By class name
$elements = $client->findElements(WebDriverBy::className('error-message'));
// By name attribute
$input = $client->findElement(WebDriverBy::name('username'));
// By tag name
$paragraphs = $client->findElements(WebDriverBy::tagName('p'));
// By link text (exact match)
$link = $client->findElement(WebDriverBy::linkText('Sign In'));
// By partial link text
$partialLink = $client->findElement(WebDriverBy::partialLinkText('Learn'));
// By CSS selector
$element = $client->findElement(WebDriverBy::cssSelector('.btn.btn-primary'));
XPath Selection Methods
filterXPath()
Method
Use XPath expressions for complex element selection scenarios.
// Basic XPath selections
$elementsByXPath = $crawler->filterXPath('//div[@class="content"]');
$siblingElements = $crawler->filterXPath('//h1/following-sibling::p');
// Advanced XPath examples
$conditionalElements = $crawler->filterXPath('//input[@type="checkbox" and @checked]');
$textContainsElements = $crawler->filterXPath('//span[contains(text(), "Error")]');
$nthElements = $crawler->filterXPath('//table/tbody/tr[position() > 2]');
// XPath with WebDriver
$element = $client->findElement(WebDriverBy::xpath('//button[text()="Submit"]'));
Specialized Selection Methods
Form Element Selection
// Select buttons by text, value, id, or name
$submitButton = $crawler->selectButton('Submit');
$buttonById = $crawler->selectButton('save-btn');
$buttonByValue = $crawler->selectButton('Save Changes');
// Select links by text or title
$homeLink = $crawler->selectLink('Home');
$titleLink = $crawler->selectLink('Go to Dashboard');
// Working with forms
$form = $crawler->selectButton('Login')->form();
$form['username'] = 'testuser';
$form['password'] = 'password123';
$client->submit($form);
Element Interaction Methods
// Get element text and attributes
$elementText = $crawler->filter('.message')->text();
$linkHref = $crawler->filter('a.download')->attr('href');
$inputValue = $crawler->filter('input[name="email"]')->attr('value');
// Check element properties
$isVisible = $crawler->filter('.notification')->count() > 0;
$hasClass = $crawler->filter('.button')->attr('class');
Chaining and Traversal
Method Chaining
// Chain multiple selections
$result = $crawler
->filter('.product-container')
->filter('.product-item')
->first()
->filter('.price')
->text();
// Traverse DOM relationships
$parentElement = $crawler->filter('.child-element')->parents();
$nextSibling = $crawler->filter('.current')->nextAll();
$previousSibling = $crawler->filter('.current')->previousAll();
Error Handling and Best Practices
Safe Element Selection
// Check if elements exist before interaction
$elements = $crawler->filter('.target-element');
if ($elements->count() > 0) {
$text = $elements->first()->text();
// Process the element
}
// Use try-catch for WebDriver methods
try {
$element = $client->findElement(WebDriverBy::id('dynamic-element'));
$element->click();
} catch (\Facebook\WebDriver\Exception\NoSuchElementException $e) {
// Element not found, handle gracefully
}
Performance Considerations
// Cache frequently used selectors
$productContainer = $crawler->filter('.products');
$products = $productContainer->filter('.product-item');
// Use specific selectors instead of traversing
// Good: Direct selection
$price = $crawler->filter('.product[data-id="123"] .price');
// Less efficient: Multiple traversals
$price = $crawler->filter('.product')
->reduce(function ($node) {
return $node->attr('data-id') === '123';
})
->filter('.price');
Summary
Symfony Panther offers comprehensive DOM selection capabilities:
- CSS Methods:
find()
,findAll()
,filter()
for standard CSS selectors - WebDriver Methods:
findElement()
,findElements()
withWebDriverBy
class - XPath Methods:
filterXPath()
for complex selections - Specialized Methods:
selectButton()
,selectLink()
for form interactions - Traversal Methods: Parent, sibling, and child navigation
Choose the method that best fits your selection needs - CSS selectors for simplicity, XPath for complex logic, or WebDriver methods for maximum precision.