How To Select Auto Suggestion Address In Dropdown Using Selenium

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys

from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import Select
import time

# Removes SSL Issues With Chrome
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--ignore-ssl-errors')
options.add_argument('--ignore-certificate-errors-spki-list')
options.add_argument('log-level=3') 
options.add_argument('--disable-notifications')
#options.add_argument('--headless') # Comment to view browser actions

# Initiate Chrome Driver
url = 'https://www.dunelm.com/product/caldonia-check-natural-eyelet-curtains-1000187301'
driver = webdriver.Chrome(executable_path="C:webdriverschromedriver.exe",options=options)
#driver.implicitly_wait(10) # Global Wait Settings
driver.get(url)

# Accept Cookie Popup
WebDriverWait(driver, 40).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="root"]/div[1]/div[6]/div/div/div/div[2]/button'))).send_keys(Keys.ENTER)

# Enter Curtain Size in Drop Drop
size = driver.find_element(By.NAME, 'curtainSize')
select_size = Select(size)
select_size.select_by_visible_text('W 228cm (90") x D 182cm (72") - £170')

# Click 'Check in Local Store'
WebDriverWait(driver, 40).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="product-top-wrapper"]/div[2]/div/div[4]/div/div/div/div[2]/div[2]/div/div'))).click()
 
# Enter Postcode or Location
postcode = "Aylesbury HP19 8BU, UK"
WebDriverWait(driver, 40).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="root"]/div[2]/div[1]/div[2]/form/div/div/div/input'))).send_keys(postcode)

#driver.close() 

URL: https://www.dunelm.com/product/caldonia-check-natural-eyelet-curtains-1000187301

The above script utilizes Selenium and is aimed at automating the process of:

  • Launching the above url
  • Accepting the cookie popup
  • Selecting the curtain size
  • Clicking text link ‘Check in Local Store’
  • Enter postcode or location

Issue

enter image description here

I’m unable to proceed beyond this point as I do not know what is required to accept the address details entered as an auto suggestion.
I tried using the code below without success

WebDriverWait(driver, 40).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="root"]/div[2]/div[1]/div[2]/form/div/div/div/input'))).send_keys(Keys.ENTER)  

Any help would be greatly appreciated.

Answer

You need to press enter, after you copy the address into address bar. There are multiple ways to do it. The below solution is demonstrating ActionsChain, key press

Code:-

# Enter Postcode or Location
postcode = "Aylesbury HP19 8BU, UK"
WebDriverWait(driver, 40).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="root"]/div[2]/div[1]/div[2]/form/div/div/div/input'))).send_keys(postcode)

time.sleep(5)
ActionChains(driver).key_down(Keys.RETURN).pause(2).key_up(Keys.RETURN).perform()

Imports :

from selenium.webdriver.common.action_chains import ActionChains