python-3.x Selenium无法将密钥发送到GoogleMap输入Selenium

lvmkulzt  于 2023-05-02  发布在  Python
关注(0)|答案(1)|浏览(100)

我正试图向谷歌Map发送键以执行搜索,但谷歌Map只在浏览器屏幕处于焦点时接受键。我试过输入框点击,最大化窗口,动作链,滚动到视图。下面是代码。

import time
from undetected_chromedriver import Chrome, ChromeOptions
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By

"""Initialize and return the drivers"""
chrome_options = ChromeOptions()
chrome_options.add_argument('--incognito')
chrome_options.add_argument(
    "--disable-blink-features=AutomationControlled")
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument("--mute-audio")
chrome_options.add_argument("--window-size=1400,1000")
chrome_options.add_argument(
    'user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36')
driver_installation = ChromeDriverManager().install()
service = Service(driver_installation)
service.service_args = chrome_options
driver = Chrome(service=service)
driver.get("https://maps.google.com")
time.sleep(3)
address = """Rett Syndrome Research Trust Inc
67 Under Cliff Rd
Trumbull, CT 6611"""
map_input = driver.find_element(By.ID, "searchboxinput")
driver.maximize_window()
driver.execute_script("arguments[0].scrollIntoView(false);", map_input)
driver.execute_script("arguments[0].click()", map_input)
map_input.send_keys(address)
actions = ActionChains(driver)
actions.click(map_input).send_keys(address).perform()
driver.quit()

以上都不起作用。提前感谢任何帮助

5kgi1eie

5kgi1eie1#

我想这和你设置的所有选项有关。下面的代码对我来说很好。

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://maps.google.com")
address = """Rett Syndrome Research Trust Inc
67 Under Cliff Rd
Trumbull, CT 6611"""
map_input = driver.find_element(By.ID, "searchboxinput")
map_input.send_keys(address)

相关问题