我正在嘗試從 Google 的開發者網站上查找自由女神像國家紀念碑的 Google Place ID:https ://developers.google.com/maps/documentation/javascript/examples/places-placeid-finder#maps_places_placeid_finder-css
我想點擊“輸入位置”的搜索欄,輸入“自由女神像國家紀念碑”,按向下箭頭鍵,然后按回車鍵得到這個結果。
但是,我的代碼甚至沒有點擊搜索欄。請幫我。
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')
更新代碼:
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)
uj5u.com熱心網友回復:
嘗試使用以下代碼
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)
更多資訊:https ://pythonbasics.org/selenium-keyboard/
uj5u.com熱心網友回復:
使用 Exmplity 等待還有另一種解決方案,而且您不需要按下鍵可能只需 Enter 鍵
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)
uj5u.com熱心網友回復:
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()
您的元素位于 iframe 中,發送鍵等非字串的方式如下。也不要使用通用 xpath,因為它們很脆弱,通常會中斷,而是使用更具包容性的選擇器。
進口:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/465104.html
下一篇:使用請求時變數中的錯誤處理
