Home > Back-end >  How do I click and press keys using Selenium?
How do I click and press keys using Selenium?

Time:04-26

I'm trying to find a Google Place ID of Statue of Liberty National Monument from a Google's developer website: https://developers.google.com/maps/documentation/javascript/examples/places-placeid-finder#maps_places_placeid_finder-css

I want to click on the search bar where it says "Enter a location", type "Statue of Liberty National Monument", press down arrow key, and press enter key to get this result.

However, my code doesn't even click the search bar. Please help me.

import selenium
import pandas as pd
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
Google_IDS = ['https://developers.google.com/maps/documentation/javascript/examples/places-placeid-finder#maps_places_placeid_finder-css']

for Google_ID in Google_IDS:
  driver.get(Google_ID)
  driver.implicitly_wait(10)
  
  Google_ID = driver.find_element(By.XPATH, '/html/body/div[2]/div/div/div[4]/input')
  Google_ID.click()
  Google_ID.send_keys('Statue of Liberty National Monument')
  Google_ID.send_keys('keys.DOWN')
  Google_ID.send_keys('keys.ENTER')

Updated Code:

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

PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)

wait = WebDriverWait(driver, 10)
Google_IDS = ['https://developers.google.com/maps/documentation/javascript/examples/places-placeid-finder#maps_places_placeid_finder-css']

for Google_ID in Google_IDS:
  driver.get(Google_ID)
  wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"div.devsite-article-body.clearfix > div > devsite-iframe > iframe")))
  Google_ID = driver.find_element(By.CSS_SELECTOR, '#pac-input')
  Google_ID.send_keys('Statue of Liberty National Monument')
  dropdown = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'div.pac-container.pac-logo > div')))
  dropdown.click()

  ID_Info = driver.find_element(By.ID, "place-id")
  print("Google ID:", ID_Info.text)

CodePudding user response:

Try Use following code

PATH = "C:\Program Files(x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
Google_IDS = ['https://developers.google.com/maps/documentation/javascript/examples/places-placeid-finder#maps_places_placeid_finder-css']

for Google_ID in Google_IDS:
  driver.get(Google_ID)
  driver.implicitly_wait(10)
  
  Google_ID = driver.find_element(By.XPATH, '/html/body/div[2]/div/div/div[4]/input')
  Google_ID.click()
  Google_ID.send_keys('Statue of Liberty National Monument')
  Google_ID.send_keys(Keys.DOWN)
  Google_ID.send_keys(Keys.ENTER)

For more :https://pythonbasics.org/selenium-keyboard/

CodePudding user response:

There is another solution with using Exmplity wait and also you don't need to press down key may just Enter key

PATH = "C:\Program Files(x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
Google_IDS = ['https://developers.google.com/maps/documentation/javascript/examples/places-placeid-finder#maps_places_placeid_finder-css']

element = WebDriverWait(driver, 10).until(
for Google_ID in Google_IDS:
  driver.get(Google_ID)
  EC.presence_of_element_located((By.CSS_SELECTOR,"div.devsite-article-body.clearfix > div > devsite-iframe > iframe"))
  
  Google_ID = driver.find_element(By.CSS_SELECTOR,"div.devsite-article-body.clearfix > div > devsite-iframe > iframe")
  Google_ID.click()
  Google_ID.send_keys('Statue of Liberty National Monument')
  Google_ID.send_keys(Keys.ENTER)


CodePudding user response:

wait = WebDriverWait(driver, 10)
Google_IDS = ['https://developers.google.com/maps/documentation/javascript/examples/places-placeid-finder#maps_places_placeid_finder-css']

for Google_ID in Google_IDS:
  driver.get(Google_ID)
  wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"div.devsite-article-body.clearfix > div > devsite-iframe > iframe")))
  Google_ID = driver.find_element(By.CSS_SELECTOR, '#pac-input')
  Google_ID.send_keys('Statue of Liberty National Monument')
  dropdown = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'div.pac-container.pac-logo > div')))
  dropdown.click()

Your element is in an iframe and the way to send non strings such as keys are as follows. Also don't use a generic xpath as those are brittle and generally will break instead use a more inclusive selector.

Imports:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC
  • Related