0

I have basic knowledge with Selenium. I am unable to submit the Google Travel form.

The Google Travel document does not contain any Tag Form or Submit Button. When I use Submit(), an error tells me:

Message: To submit an element, it must be nested inside a form element

I tried unsuccessfully to use Keys.RETURN but without success. My code is :

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

options = Options()
options.binary_location = r'C:\Program Files\Mozilla Firefox\firefox.exe'
driver = webdriver.Firefox(executable_path=r'C:\FLET\geckodriver.exe', options=options)

url = 'https://www.google.com/travel/'
driver.get(url)

your_input = driver.find_element(By.XPATH, "//input\[@type='text'\]")
your_input.clear()
your_input.send_keys('Andalousie')
#your_input.submit()
#your_input.send_keys(Keys.RETURN)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Are you sure you are getting the right element when you use the xpath like that? It seems like a very vague way to search for an element. I would try this instead: `driver.find_element(By.XPATH, "//input[@placeholder='Search for destinations, sights and more']")` Also, I'm not sure if this is an error or not but I've never seen an xpath with a backslash before the brackets (I could be wrong about that though). – Anon Jun 08 '23 at 19:58
  • Another way to submit it would be to send it keys and then to hit the ENTER button using ActionChains `action_chains = webdriver.ActionChains(driver) action_chains.send_keys(Keys.ENTER).perform()` – Anon Jun 08 '23 at 20:08
  • Your solution solves half of my problem. The basic problem seems to come from the Web Browser's Autocomplete. Your solution worked when I first ran my script with your_input.send_keys('Andalusie'). Subsequently, the Submit no longer worked. By mistake, I added a letter (‘a’) to your_input.send_keys('Andalousiea') which made it work successfully again, but only once again. Then, I added another letter (‘r’) and so on. When the content of the search field is known by the Web Browser, it brings up a list of suggestions (Autocomplete) and this seems to prevent the command from executing correctly – Michel Laliberté Jun 08 '23 at 22:41

1 Answers1

0

To send the text Andalousie within the search field in Google Travel and select the first i.e. andalousie all inclusive you can use the following locator strategies:

  • Code block:

    driver.get("https://www.google.com/travel/")
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@aria-autocomplete='inline' and starts-with(@placeholder, 'Search for destinations')]"))).send_keys('Andalousie')
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//ul[@role='listbox']/li"))).click()
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Browser Snapshot:

Google Travel

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Your solution works fine if the autocomplete shows a list. But I want my input text to always be different, looking for the country/states main attraction. So I need to fill in the input text and then submit without selecting it from the list. THANKS – Michel Laliberté Jun 08 '23 at 22:47
  • @MichelLaliberté Afaik, Google would only allow to search for indexed themes, so either way you have to choose & click from the auto-suggestion only, as I have demonstrated. – undetected Selenium Jun 08 '23 at 22:53
  • Thanks for your answers. Can you explain to me that I can enter my text, then launch the search by pressing the Return key on my keyboard, making no selection in the drop-down list. – Michel Laliberté Jun 09 '23 at 20:30