You can download this code by clicking the button below.
This code is now available for download.
This function uses the Selenium library to search for a given query on a specified website and waits for the search results to load.
Technology Stack : Selenium, WebDriver, WebDriverWait, expected_conditions
Code Type : Function
Code Difficulty : Intermediate
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def search_item_on_website(url, search_query):
# Initialize the WebDriver
driver = webdriver.Chrome()
# Open the URL
driver.get(url)
# Wait for the search box to be clickable and then enter the search query
search_box = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.NAME, "q"))
)
search_box.clear()
search_box.send_keys(search_query)
search_box.send_keys(Keys.RETURN)
# Wait for the search results to load
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "search-results"))
)
# Close the browser
driver.quit()