Custom ActionsUsing Crawl Custom Action

Using Crawl Custom Action

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.

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.

Prerequisites

Basic knowledge of Java programming Selenium WebDriver installed

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

Java code looping through main and sub category WebElements using the Crawl custom action
FieldDescription
Navigate through categoriesFind 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 subcategoryLoop through each subcategory element and click on it using subCategory.get(k).click();.
Perform category-specific actionsPerform any actions specific to each category, such as crawling each and every product or corresponding category is displayed PLP
Return to main pageUse 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.