1
+ from selenium import webdriver
2
+ from selenium .webdriver .chrome .service import Service
3
+ from webdriver_manager .chrome import ChromeDriverManager
4
+ from selenium .webdriver .common .by import By
5
+ from selenium .common .exceptions import NoSuchElementException
6
+ from selenium .webdriver .support .ui import WebDriverWait
7
+ from selenium .webdriver .support import expected_conditions as EC
8
+ import time
9
+
10
+ service = Service (ChromeDriverManager ().install ())
11
+ driver = webdriver .Chrome (service = service )
12
+ driver .get ('https://www.radiustheme.com/demo/wordpress/themes/zilly/' )
13
+
14
+ driver .maximize_window ()
15
+ time .sleep (5 )
16
+
17
+ # Scrolling the page down by calculating the middle scroll position
18
+ total_height = driver .execute_script ('return document.body.scrollHeight' )
19
+ scroll_position = (total_height / 2 ) - 100
20
+ driver .execute_script (f'window.scrollTo(0, { scroll_position } )' )
21
+
22
+ time .sleep (4 )
23
+
24
+ # Extracting category elements from the webpage and counting occurrences
25
+ parent_div = driver .find_element (By .XPATH , '//*[@id="rtsb-container-3287462810"]/div' )
26
+ category_elements = parent_div .find_elements (By .XPATH , './/li/a[text()]' )
27
+
28
+ category_counts = {}
29
+
30
+ for category in category_elements :
31
+ category_name = category .text .strip ()
32
+ if category_name :
33
+ category_counts [category_name ] = category_counts .get (category_name , 0 ) + 1
34
+
35
+ total_count = 0
36
+ print ("\n Category Item Counts" )
37
+ print ("----------------------" )
38
+ for category_name , count in category_counts .items ():
39
+ print (f"{ category_name } - { count } " )
40
+ total_count += count
41
+ print ("----------------------" )
42
+ print (f"Total count - { total_count } \n " )
43
+
44
+ # Clicking (See More) to load more content and waiting for the page to load
45
+ driver .find_element (By .XPATH , '//*[@id="content"]/div/div[8]/div/div/div[1]/div/div/div/div/div/a' ).click ()
46
+
47
+ # Scrolling down and waiting for new content to load until no more elements are found
48
+ height = driver .execute_script ('return document.body.scrollHeight' )
49
+ while True :
50
+ driver .execute_script ('window.scrollTo(0, document.body.scrollHeight)' )
51
+ time .sleep (2 )
52
+ try :
53
+ # Click to 'Load More' items if available
54
+ driver .find_element (By .XPATH , '//*[@id="rtsb-builder-content"]/div/div/div/div/div[2]/div/div/div[2]/div[3]/div/button/span' ).click ()
55
+ time .sleep (4 )
56
+ except NoSuchElementException :
57
+ break
58
+ # Check if the page height has changed to ensure more items are loaded
59
+ new_height = driver .execute_script ('return document.body.scrollHeight' )
60
+ if height == new_height :
61
+ break
62
+ height = new_height
63
+
64
+ # Repeating category extraction and counting after additional content load
65
+ parent_div = driver .find_element (By .XPATH , '//*[@id="rtsb-builder-content"]/div/div/div/div/div[2]/div/div/div[2]/ul' )
66
+ category_elements = parent_div .find_elements (By .XPATH , './/li/div/div[1]/div[1]/div[1]/a' )
67
+
68
+ category_counts = {}
69
+ for category in category_elements :
70
+ category_name = category .text .strip ()
71
+ if category_name :
72
+ category_counts [category_name ] = category_counts .get (category_name , 0 ) + 1
73
+
74
+ # Displaying counts for the newly loaded content
75
+ total_count = 0
76
+ print ("\n Category Item Counts" )
77
+ print ("----------------------" )
78
+ for category_name , count in category_counts .items ():
79
+ print (f"{ category_name } - { count } " )
80
+ total_count += count
81
+ print ("----------------------" )
82
+ print (f"Total count - { total_count } " )
83
+
84
+ # Interacting with the page by clicking on 'Add to Cart'
85
+ wait = WebDriverWait (driver , 10 )
86
+ element = wait .until (EC .presence_of_element_located ((By .XPATH , '//*[@id="rtsb-builder-content"]/div/div/div/div/div[2]/div/div/div[2]/ul/li[7]/div/div[3]/div/div/a/span' )))
87
+ driver .execute_script ("arguments[0].click();" , element )
88
+
89
+ # Navigating to the cart page
90
+ time .sleep (4 )
91
+ driver .find_element (By .XPATH , '//*[@id="rtsb-side-content-area-id"]/div/p/a[1]' ).click ()
92
+ time .sleep (4 )
93
+
94
+ # Increasing item quantity in the cart
95
+ driver .find_element (By .XPATH , '//*[@id="rtsb-builder-content"]/div/div/div/div/div[1]/div/div/div/form/table/tbody/tr/td[5]/div/div/div/button[2]/i' ).click ()
96
+ time .sleep (4 )
97
+
98
+ # Capturing screenshot of the cart page
99
+ driver .save_screenshot ('Cart_Page.png' )
100
+ time .sleep (2 )
101
+
102
+ # Removing items from the cart
103
+ driver .find_element (By .XPATH , '//*[@id="rtsb-builder-content"]/div/div/div/div/div[1]/div/div/div/form/table/tbody/tr/td[1]/div/a/i' ).click ()
104
+ time .sleep (4 )
105
+
106
+ # Capturing screenshot after removing items from cart
107
+ driver .save_screenshot ('Removed_Cart.png' )
108
+ time .sleep (2 )
109
+
110
+ # Navigating back to the shop page and then to the home page
111
+ driver .find_element (By .XPATH , '//*[@id="rtsb-builder-content"]/div/div/div/div/div[1]/div/div/div/p/a' ).click ()
112
+ time .sleep (4 )
113
+ driver .find_element (By .XPATH , '//*[@id="content"]/div[1]/div/div/div/div/span[1]/a/span' ).click ()
114
+ time .sleep (4 )
115
+
116
+ # Searching for 'organic' using the search bar
117
+ driver .find_element (By .XPATH , '//*[@id="header-middlebar"]/div/div/div[3]/form/div/div/input' ).send_keys ('organic' )
118
+ time .sleep (4 )
119
+
120
+ # Taking a screenshot of the search suggestions
121
+ driver .save_screenshot ('Search_suggestions.png' )
122
+
123
+ # Extracting and printing the product names from the search results
124
+ parent_div = driver .find_element (By .XPATH , '//*[@id="header-middlebar"]/div/div/div[3]/div/div/ul' )
125
+ product_elements = parent_div .find_elements (By .XPATH , './/li/div[2]/h3/a' )
126
+
127
+ product_names = []
128
+ for product in product_elements :
129
+ product_names .append (product .text )
130
+
131
+ print ("----------------------" )
132
+ print (f"Number of search suggestions: { len (product_names )} \n " )
133
+ for name in product_names :
134
+ print (name )
135
+ print ("----------------------" )
136
+
137
+ input ("\n Press Enter to close the browser..." )
138
+ driver .quit ()
0 commit comments