I am trying to have selenium to do the following:
- Open a website
- Click on the search box
- Type "Seattle" in the search box
- Select the first result from the suggested results
- Hit Enter
- Click on the new search box
- Type "Chicago" in the new search box
- 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:
This is the final result I want to see:
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)