import sys
import time
from selenium import webdriver
from
selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
if len(sys.argv) > 1:
Category = sys.argv[1]
else:
raise Exception("No Category inputted")
ser = Service(r"C:\Drivers\chromedriver-win64\chromedriver.exe")
driver = webdriver.Chrome(service=ser)
try:
# Find the search box using a more reliable method (e.g., CSS selector)
search_box = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'input[name="q"]')))
search_box.send_keys(f"Best Recommended {Category} Books")
search_box.submit()
# Add a small delay to allow the search results to load
time.sleep(3)
# Execute the JavaScript script to remove elements
my_script = """
elements = document.getElementsByClassName('cUnQKe');
var elementsArray = Array.from(elements);
elementsArray.forEach(function(element) {
element.remove();
});
"""
driver.execute_script(my_script)
except Exception as e:
print(f"An error occurred: {e}")
finally:
# Close the browser window at the end
driver.quit()