Search code examples
seleniumxpathselenium-chromedriverkey

Selenium - arrow Down and Enter not possible?


i try to automate a site with the below code -

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
import time

link = "https://www.bcassessment.ca/"
options = Options()
options.add_argument("start-maximized")
options.add_argument('window-size=1920x1080')                                 
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
srv=Service(ChromeDriverManager().install())
driver = webdriver.Chrome (service=srv, options=options)    
waitWebDriver = WebDriverWait (driver, 10)    
driver.get (link)        
waitWebDriver.until(EC.element_to_be_clickable( \
  (By.XPATH, "//input[@id= 'rsbSearch']"))).send_keys("106 Maple")          
driver.find_element(By.XPATH, "//input[@id= 'rsbSearch']").send_keys(Keys.ARROW_DOWN)      
driver.find_element(By.XPATH, "//input[@id= 'rsbSearch']").send_keys(Keys.ENTER)      
time.sleep(500) 

The textinput to the field works fine - but then its necessary to press the Down-Key and afterwards the Return-Key to get the search-result. But this is not working with Selenium.

Why is that and what is necessary to do so i get to the search result (eg. press down and enter)?


Solution

  • I see manually after entering the searching string you need to click on the search input, then to press down arrow, wait until suggested result is presented, then press down key again and then press the Enter key.
    Also, I see So please try this, it should work

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.chrome.service import Service
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.keys import Keys
    from webdriver_manager.chrome import ChromeDriverManager
    import time
    
    link = "https://www.bcassessment.ca/"
    options = Options()
    options.add_argument("start-maximized")
    options.add_argument('window-size=1920x1080')                                 
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-gpu')
    srv=Service(ChromeDriverManager().install())
    driver = webdriver.Chrome (service=srv, options=options)    
    waitWebDriver = WebDriverWait (driver, 10)    
    driver.get (link)        
    waitWebDriver.until(EC.element_to_be_clickable( \
      (By.XPATH, "//input[@id= 'rsbSearch']"))).send_keys("106 Maple")          
    searc_input = driver.find_element(By.XPATH, "//input[@id= 'rsbSearch']")
    searc_input.click()
    time.sleep(0.1) 
    searc_input.send_keys(Keys.ARROW_DOWN)      
    time.sleep(0.4) 
    searc_input.send_keys(Keys.ARROW_DOWN)      
    time.sleep(0.4) 
    searc_input.send_keys(Keys.ENTER)