我试着从谷歌Map上打印出5所第一工程学院的名字。我已经做了下面的代码,但我不能找到为什么运行后没有结果!
你能帮帮忙吗?
非常感谢。
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 "parisserie in Paris" 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 names of the first 5 results using "aria-label"
results = driver.find_elements(By.XPATH, "//a[@class='place-result-container-place-link']/@aria-label")
# Print the names for debugging
for i, result in enumerate(results[:5], start=1):
print(f"Result {i}: {result}")
# Close the browser
driver.quit()
我在寻求帮助
2条答案
按热度按时间vngu2lb81#
下面是一个完整的脚本(基于您的设置),它直接指向搜索结果的URL,并使用更好的选择器来查找学校名称:
ma8fv8wu2#
**问题:**你下面的表达式似乎不正确,我没有看到它定位任何元素。
请查看以下工作代码:
控制台输出: