【问题标题】:How To Select Auto Suggestion Address In Dropdown Using Selenium如何使用 Selenium 在下拉列表中选择自动建议地址
【发布时间】:2021-10-31 17:50:54
【问题描述】:
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() 

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

上述脚本使用 Selenium,旨在自动化以下过程:

  • 启动上述网址
  • 接受 cookie 弹出窗口
  • 选择窗帘尺寸
  • 点击文本链接“签入本地商店”
  • 输入邮政编码或位置

问题

由于我不知道接受作为自动建议输入的地址详细信息需要什么条件,因此我无法继续进行。
我尝试使用下面的代码没有成功

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)  

任何帮助将不胜感激。

【问题讨论】:

    标签: python selenium selenium-webdriver automation autosuggest


    【解决方案1】:

    将地址复制到地址栏后,您需要按回车键。有多种方法可以做到这一点。以下解决方案正在演示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
    

    【讨论】:

    • 如果有多个自动建议的选项,而您无法知道有多少并且不知道所有建议选项之间所需选项的顺序,该怎么办?
    • 已经看过 OP,提前知道地址,阅读这一行I'm unable to proceed beyond this point as I do not know what is required to accept the address,因为我已经给出了确切问题的解决方案。
    • 我明白了。但是,您最好给出更通用的答案,这在其他情况下也很有用。
    • 您好 Cruisepandey,感谢您的及时回复。添加“from selenium.webdriver.common.action_chains import ActionChains”后,解决方案很有效。
    猜你喜欢
    • 1970-01-01
    • 2020-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-22
    • 2020-03-06
    • 2021-07-10
    • 2018-12-15
    相关资源
    最近更新 更多