Home > Blockchain >  Selenium not clicking search box
Selenium not clicking search box

Time:12-09

I am trying to have selenium to do the following:

  1. Open a website
  2. Click on the search box
  3. Type "Seattle" in the search box
  4. Select the first result from the suggested results
  5. Hit Enter
  6. Click on the new search box
  7. Type "Chicago" in the new search box
  8. Select the first result from the suggested results

I was able to get it to work until step 5, but I can't find a way to do the same tasks with a new search box in step 6.

Here's my code:

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

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)

url = 'https://wego.here.com/'
driver.get(url)

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.input_search"))).click()
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.input_search"))).send_keys('Seattle')
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.input_search"))).send_keys(Keys.ENTER)

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button.btn"))).send_keys(Keys.ENTER)

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.ng_pristine"))).click()
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.ng_pristine"))).send_keys('Chicago')
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.ng_pristine"))).send_keys(Keys.ENTER)

Here's what the source page of the new search box looks like:

1

This is the final result I want to see:

2

CodePudding user response:

In this specific scenario, to make a new search you should first clear the previous search state.
This should work:

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

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)

url = 'https://wego.here.com/'
driver.get(url)

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.input_search"))).click()
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.input_search"))).send_keys('Seattle')
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.input_search"))).send_keys(Keys.ENTER)

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button.btn"))).send_keys(Keys.ENTER)

# clear the previous search results
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".btn_close"))).click()

#perform a new search
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.input_search"))).click()
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.input_search"))).send_keys('Chicago')
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.input_search"))).send_keys(Keys.ENTER)

UPD
For the edited question the answer is:
You are using a wrong locator.
This should work:

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

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)

url = 'https://wego.here.com/'
driver.get(url)

search_input = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.input_search")))
search_input.click()
search_input.send_keys('Seattle')
search_input.send_keys(Keys.ENTER)

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button.btn"))).send_keys(Keys.ENTER)

internal_search_input = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input#itinerary_item_input_0")))
internal_search_input.click()
internal_search_input.send_keys('Chicago')
internal_search_input.send_keys(Keys.ENTER)
  • Related