javaseleniumdata-layers

How to capture datalayer object and print in my eclipse console using selenium


I need to capture the datalayer from the console tab in the chrome developer tools



Solution

  • Below code will help you to get data layer information

       JavascriptExecutor executor= (JavascriptExecutor)getDriver();
    
        ArrayList<Map<String, List<String> >> datalist = new ArrayList<>();
    
        //Execute google tag manage in the console using javascript executor to fetch values       
        datalist =  (ArrayList) executor.executeScript("return window.dataLayer");
    
        // Parse through GTM arrayList  
        for(int a=0; a < datalist .size(); a++) {
            for (String key : datalist .get(a).keySet()) {
                System.out.println(key + "      " + datalist .get(a).get(key));
    
            }
        }