Residential Proxies
Allowlisted 200M+ IPs from real ISP. Managed/obtained proxies via dashboard.
Proxies
Residential Proxies
Allowlisted 200M+ IPs from real ISP. Managed/obtained proxies via dashboard.
Residential (Socks5) Proxies
Over 200 million real IPs in 190+ locations,
Unlimited Residential Proxies
Use stable, fast, and furious 700K+ datacenter IPs worldwide.
Static Residential proxies
Long-lasting dedicated proxy, non-rotating residential proxy
Dedicated Datacenter Proxies
Use stable, fast, and furious 700K+ datacenter IPs worldwide.
Web Unblocker
View content as a real user with the help of ABC proxy's dynamic fingerprinting technology.
Proxies
API
Proxy list is generated through an API link and applied to compatible programs after whitelist IP authorization
User+Pass Auth
Create credential freely and use rotating proxies on any device or software without allowlisting IP
Proxy Manager
Manage all proxies using APM interface
Proxies
Residential Proxies
Allowlisted 200M+ IPs from real ISP. Managed/obtained proxies via dashboard.
Starts from
$0.77/ GB
Residential (Socks5) Proxies
Over 200 million real IPs in 190+ locations,
Starts from
$0.045/ IP
Unlimited Residential Proxies
Use stable, fast, and furious 700K+ datacenter IPs worldwide.
Starts from
$79/ Day
Rotating ISP Proxies
ABCProxy's Rotating ISP Proxies guarantee long session time.
Starts from
$0.77/ GB
Static Residential proxies
Long-lasting dedicated proxy, non-rotating residential proxy
Starts from
$5/MONTH
Dedicated Datacenter Proxies
Use stable, fast, and furious 700K+ datacenter IPs worldwide.
Starts from
$4.5/MONTH
Knowledge Base
English
繁體中文
Русский
Indonesia
Português
Español
بالعربية
This article systematically explains the core principles and practical applications of the contains(@class, 'value') selector in XPath, covering key scenarios such as dynamic class name processing, multiple class name matching, performance optimization, and provides solutions to deal with the complex class name structure of modern Web frameworks.
The technical principle and grammatical structure of the class contains selector
Basic grammar analysis
The XPath contains() function is used to detect whether an attribute value contains a specific substring. Its core syntax is:
//tagname[contains(@class, 'partial-class-name')]
For example, //div[contains(@class, 'menu')] matches all div elements whose class attribute contains "menu", such as class="main-menu" or class="menu-item".
Comparison with exact match
Exact match: @class='target' requires the attribute value to be exactly equal to "target" and cannot handle scenarios with multiple class names or dynamic prefixes;
Contains matching: contains(@class, 'target') matches class names that contain the substring anywhere, which is suitable for dynamic classes in frameworks such as Bootstrap (such as col-md-6).
Handling the priority of multiple class names
When an element contains multiple class names (such as class="btn btn-primary active"), you can filter it through chained contains:
//button[contains(@class, 'btn') and contains(@class, 'active')]
Core application scenarios and practical cases
Dynamic class name matching
Modern web frameworks (such as React and Vue) often generate random hash class names (such as _1a2b3c), but retain some fixed semantic prefixes. For example:
//div[contains(@class, 'product-card_')] // matches the class name product-card_1a2b generated by Next.js
Responsive layout positioning
Bootstrap's grid system class names (such as col-md-6, col-lg-4) can be used to achieve cross-device positioning through partial matching:
//div[contains(@class, 'col-')] // Select all grid column elements
Status Tracking
For dynamic state changes (such as activated state, disabled state), capture elements by changing the class name:
//li[contains(@class, 'active')]/a // Get the currently activated navigation link
Compound selector optimization
Combine with other attributes to achieve precise positioning, for example, match elements with the "modal" class and a data-testid attribute:
//div[contains(@class, 'modal') and @data-testid='login-form']
Common Problems and Performance Optimization Strategies
Partial match conflict
When the substring appears in a non-target class name (such as class="btn confirmation" matches contains(@class, 'on')), the solution is:
Increase positioning accuracy: Narrow the scope by combining hierarchical relationships or adjacent elements
//form[@id='signup']//button[contains(@class, 'btn')]
Use regular expressions (XPath 2.0+ is required, and some browsers are not compatible):
//*[matches(@class, '\bprimary-button\b')]
Performance bottlenecks and solutions
The full document scanning feature of contains() may lead to inefficient execution. Optimization strategies include:
Hierarchical limitation: Narrow the search scope by parent node first
//ul[@id='main-nav']//li[contains(@class, 'dropdown')]
Index acceleration: Direct positioning using position index (applicable to fixed structures)
(//div[contains(@class, 'card')])[1] // Select the first matching element
Dynamic page compatibility
Dealing with asynchronous loading issues of SPA (single page application):
Explicit waits: Configuring dynamic wait conditions in tools like Selenium
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//div[contains(@class, 'loader')]"))
)
Incremental matching: Dynamically generate XPath based on the changing characteristics of the class name (such as the timestamp suffix loading_1623984000)
Advanced Technique: Combine with other XPath functions to enhance location capabilities
1 Combining starts-with and ends-with
// Matches class names starting with "icon-"
//span[starts-with(@class, 'icon-')]
// Matches class names ending with "-active" (XPath 2.0+)
//div[ends-with(@class, '-active')]
2 Multiple inclusion filtering
Locate class names that contain multiple keywords at the same time (the order does not matter):
//div[contains(@class, 'user') and contains(@class, 'admin')]
3. Class name normalization
Use normalize-space() to eliminate extra spaces:
//div[contains(normalize-space(@class), 'selected')]
The impact of AI and visualization tools on XPath positioning
AI intelligent generation: Automated XPath generation tools based on visual recognition (such as Testim and Applitools) can analyze the page structure and automatically output optimized paths including classes;
Browser DevTools enhancement: Chrome's latest developer tools now support the generation of contains(@class) expressions by right-clicking an element;
Cross-frame adapter: For Web Components and Shadow DOM, XPath positioning needs to be combined with shadow-root penetration technology:
//custom-element::shadow-root//div[contains(@class, 'inner-component')]
Conclusion
In web crawling and automated testing, the proper use of the class contains selector can significantly improve the robustness of the script. When faced with large-scale data collection, combined with abcproxy's high-quality proxy IP services (such as static residential proxies or data center proxies), it can effectively bypass the anti-crawling mechanism and ensure the stability of the XPath positioning process. If you need to achieve efficient and secure network request management in complex scenarios, you can visit the abcproxy official website to explore customized solutions.
Featured Posts
Popular Products
Residential Proxies
Allowlisted 200M+ IPs from real ISP. Managed/obtained proxies via dashboard.
Residential (Socks5) Proxies
Over 200 million real IPs in 190+ locations,
Unlimited Residential Proxies
Use stable, fast, and furious 700K+ datacenter IPs worldwide.
Rotating ISP Proxies
ABCProxy's Rotating ISP Proxies guarantee long session time.
Residential (Socks5) Proxies
Long-lasting dedicated proxy, non-rotating residential proxy
Dedicated Datacenter Proxies
Use stable, fast, and furious 700K+ datacenter IPs worldwide.
Web Unblocker
View content as a real user with the help of ABC proxy's dynamic fingerprinting technology.
Related articles
How does idope improve data collection efficiency
This article explores the core role of idope in data collection, analyzes how proxy IP can optimize its operating efficiency, and analyzes the adaptation solution provided by abcproxy.
Big Data Ecommerce: Core Value and Application
This article discusses the technical architecture and commercial value of Big Data Ecommerce, and analyzes the transformation path of the e-commerce industry driven by data. abcproxy provides underlying support for big data e-commerce through proxy IP services, helping enterprises achieve precise operations.
Class in XPath: Syntax, Application and Advanced
This article systematically explains the core principles and practical applications of the contains(@class, 'value') selector in XPath, covering key scenarios such as dynamic class name processing, multiple class name matching, performance optimization, and provides solutions to deal with the complex class name structure of modern Web frameworks.