如何 Select 使用 Selenium 在下拉列表中自动建议地址

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:\webdrivers\chromedriver.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

上面的脚本利用了 Selenium 并且旨在自动化以下过程:

问题

我无法继续下一步,因为我不知道需要什么才能接受作为自动建议输入的地址详细信息
我尝试使用下面的代码但没有成功

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)  

如有任何帮助,我们将不胜感激。

将地址复制到地址栏后,您需要按回车键。有多种方法可以做到这一点。下面的解决方案正在演示 ActionsChain, key press

代码:-

# 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()

进口:

from selenium.webdriver.common.action_chains import ActionChains