Search code examples
pythonselenium-webdriverweb-scrapingselenium-chromedriver

Automating User Zip Code Entry in Python Selenium and Chromedriver


I am trying to automate entering a zip code on a webpage and then scraping the text of the page that is output as a result. (Using Grocery Outlet store locater as an example for privacy reasons)

options = webdriver.ChromeOptions()
options.add_argument('--disable-blink-features=AutomationControlled')
options.add_argument('--ignore-certificate-errors') 
options.add_argument('--log-level=OFF')
options.headless = True
driver = webdriver.Chrome(options=options)

driver.get("https://www.groceryoutlet.com/store-locator")
driver.find_element(By.XPATH,"/html/body/main/div/div[1]/div/div/div/form/div/div/div/div/input").send_keys("90210")

html = driver.page_source
htmltext = BeautifulSoup(html, "lxml").text
cleantext = htmltext.replace('\n','').replace('\t','').replace(',','\n')
cleantext

This doesn't seem to actually pass through the zip code though. The text that is scraped seems like it's just scraping the URL before entering the zip code.

Thanks in advance!


Solution

  • There are a couple options you can use:

    1. Send Key

      element = options.find_element_by_id("searchBoxId") element.sendKeys(Keys.RETURN)

      The problem with this is that not every text box supports the enter/return key to search. Grocery Outlet does, but I've run into several that don't.

    2. Search for Button Element then Click

      element = options.find_element_by_id("searchButtonId") element.click()

      Personally, I find this more reliable as the search button should always exist on the page.

    It's a good idea to write a function that covers both behaviors. In my scripts, I use the click option as a failover if I can't simply send the enter/return key within the search box.