TABLE OF CONTENTS
1. Crawl Custom Action Overview
1. Crawl Custom Action Overview
The main purpose of the crawl method is to reduce the number of test cases for different categories (main and sub) by replacing unique XPaths with a common XPath. This reduces the effort required for writing unique XPaths for all the categories.
2. Use Case
The following use case explains how to automate the navigation through main and sub-categories on the Amazon website using Selenium WebDriver with Java.
3. Prerequisites
- Basic knowledge of Java programming
- Selenium WebDriver installed
4. Custom Action Summary
The following code snippet is written in Java and is used for automating the process of clicking through a hierarchical menu structure on a web page. The summary provides code snippet with a short description on navigating through categories, handling web element accessibility, and updating the code for additional categories on the Amazon website.
Code Snippet
Field | Description |
Navigate through categories | Find all main category elements using List<WebElement> mainCategory = driver.findElements(By.xpath("//ul[@id='nav-main-menu']/li"));. Loop through each main category element and click on it using mainCategory.get(i).click();. Find all sub-category elements using List<WebElement> subCategory = driver.findElements(By.xpath("//div[@id='nav-sub-menu']/div"));. Common Xpath are provided. Crawler action uses these Xpath by indexing approach it will go through all the categories. |
Looping subcategory | Loop through each subcategory element and click on it using subCategory.get(k).click();. |
Perform category-specific actions | Perform any actions specific to each category, such as crawling each and every product or corresponding category is displayed PLP |
Return to main page | Use driver.navigate().back(); to return to the main page after processing each sub-category. The loop will continue until all the categories and subcategories are processed. |
Tip - To create, add and update custom action, refer to the article How to use custom methods.
Was this article helpful?
That’s Great!
Thank you for your feedback
Sorry! We couldn't be helpful
Thank you for your feedback
Feedback sent
We appreciate your effort and will try to fix the article