如何在谷歌地图上向下滚动位置

问题描述 投票:0回答:1

我正在尝试使用下面的代码从谷歌地图加载 20 所工程学校。问题是我需要向下滚动结果才能加载更多结果,否则我只能得到 8 个结果。

有人可以帮忙吗;

提前非常感谢

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

chrome_options = Options()
chrome_options.add_experimental_option("detach", True)

driver = webdriver.Chrome(options=chrome_options)

driver.get("https://www.google.com/")
driver.find_element(By.ID, "L2AGLb").click()

driver.get("https://www.google.com/maps")

# Wait for the page to load and display the search box
time.sleep(3)

# Input the search query for "patisserie in London" and press Enter
search_box = driver.find_element(By.ID, "searchboxinput")
search_box.send_keys("engineering school in london")
search_box.send_keys(Keys.RETURN)

# Wait for the search results to load
time.sleep(5)

# Get the elements based on the below XPath locator
results = driver.find_elements(By.XPATH, "//a[@class='hfpxzc']")

# Print the names for debugging
for i, result in enumerate(results[:20], start=1):
    print(f"Result {i}: {result.get_attribute('aria-label')}")

# Close the browser
driver.quit()

我正在尝试使用下面的代码从谷歌地图加载 20 所工程学校。问题是我需要向下滚动结果才能加载更多结果,否则我只能得到 8 个结果。

python selenium-webdriver pycharm
1个回答
0
投票

你有4个问题:

  1. 默认情况下,地图项选项卡不处于焦点状态。
  2. 新项目是异步加载的,因此滚动后您需要等待它们渲染的条件。
  3. 滚动到最后一项不触发重新渲染,您应该向下滚动一点。
  4. 通过 Selenium 滚动
    ActionChains
    重置焦点。

我的解决方案很脏,但您可以从它开始并根据需要进行改进:

  1. 初始化 while 循环
  2. 获取地图项数组
  3. 通过
    ActionChains
  4. 滚动到最后一项
  5. 通过单击输入区域左下角的元素来聚焦选项卡
  6. 以小延迟执行
    ARROW_DOWN
    事件
  7. 等待最后一个元素位置稳定
  8. 获取新的地图项数组
  9. 如果最后一个元素是前一个地图项元素 - 中断循环,否则 - 重复。
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains, Keys

actionChains = ActionChains(driver)
wait = WebDriverWait(driver, 20)

def wait_for_element_location_to_be_stable(element):
    initial_location = element.location
    previous_location = initial_location
    start_time = time.time()
    while time.time() - start_time < 1:
        current_location = element.location
        if current_location != previous_location:
            previous_location = current_location
            start_time = time.time()
        time.sleep(0.4)

# your previous code

results = driver.find_elements(By.XPATH, "//a[@class='hfpxzc']")

break_condition = False
focus_element = driver.find_element(By.ID, 'zero-input')
while not break_condition:
    temp = results[-1]
    actionChains.scroll_to_element(results[-1]).perform()
    actionChains.move_to_element(focus_element).click().perform()
    for i in range(3):
        actionChains.send_keys(Keys.ARROW_DOWN).perform()
        time.sleep(0.5)
    wait_for_element_location_to_be_stable(temp)

    results = wait.until(EC.presence_of_all_elements_located((By.XPATH, "//a[@class='hfpxzc']")))
    if results[-1] == temp:
        break_condition = True

for i, result in enumerate(results[:20], start=1):
    print(f"Result {i}: {result.get_attribute('aria-label')}")
© www.soinside.com 2019 - 2024. All rights reserved.