无法使用selenium与谷歌地图交互以获取路线详细信息

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

我正在尝试与谷歌地图交互以获取路线相关数据。这是我到目前为止所写的。

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

driver = webdriver.Chrome()
driver.get("https://www.google.com/maps")
time.sleep(5)

driver.find_element(By.ID, 'hArJGc').click()
time.sleep(5)
search_boxes = driver.find_elements(By.CLASS_NAME, 'tactile-searchbox-input')
search_boxes[0].clear()
search_boxes[0].send_keys('Hyderabad')
search_boxes[1].clear()
search_boxes[1].send_keys('Guntur')
search_boxes[1].send_keys(Keys.ENTER)

time.sleep(10)

driver.find_element(By.CLASS_NAME, 'goog-inline-block goog-menu-button-dropdown').click()

我正在尝试通过传递其类名来访问下拉按钮以设置出发时间。但它说这样的元素不存在,如下所示

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".goog-inline-block goog-menu-button-dropdown"}
  (Session info: chrome=124.0.6367.91); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception
Stacktrace:
    GetHandleVerifier [0x00007FF7AEF11502+60802]
    (No symbol) [0x00007FF7AEE8AC02]
    (No symbol) [0x00007FF7AED47CE4]
    (No symbol) [0x00007FF7AED96D4D]
    (No symbol) [0x00007FF7AED96E1C]
    (No symbol) [0x00007FF7AEDDCE37]
    (No symbol) [0x00007FF7AEDBABBF]
    (No symbol) [0x00007FF7AEDDA224]
    (No symbol) [0x00007FF7AEDBA923]
    (No symbol) [0x00007FF7AED88FEC]
    (No symbol) [0x00007FF7AED89C21]
    GetHandleVerifier [0x00007FF7AF21411D+3217821]
    GetHandleVerifier [0x00007FF7AF2560B7+3488055]
    GetHandleVerifier [0x00007FF7AF24F03F+3459263]
    GetHandleVerifier [0x00007FF7AEFCB846+823494]
    (No symbol) [0x00007FF7AEE95F9F]
    (No symbol) [0x00007FF7AEE90EC4]
    (No symbol) [0x00007FF7AEE91052]
    (No symbol) [0x00007FF7AEE818A4]
    BaseThreadInitThunk [0x00007FFAFC8E257D+29]
    RtlUserThreadStart [0x00007FFAFE42AA48+40]

但是当我检查网页时发现类名存在。为什么我无法像其他元素一样访问它以及如何解决这个问题?

提前致谢! enter image description here

python google-maps selenium-webdriver automation
1个回答
0
投票

问题:

  1. 一旦启动 URL,就会出现

    Accept/Reject cookies
    弹出窗口。你需要摆脱它

  2. 以下代码不正确。有2个班级

    goog-inline-block
    goog-menu-button-dropdown
    。如果有多个班级
    CLASS_NAME
    定位器将不起作用。

    By.CLASS_NAME, 'goog-inline-block goog-menu-button-dropdown' 
    
  3. 使用selenium的waits,而不是

    time.sleep()

下面是重构后的工作代码:

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

driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://www.google.com/maps")
wait = WebDriverWait(driver, 10)
# below line will click on Accept all cookies button
wait.until(EC.element_to_be_clickable((By.XPATH, "(//span[text()='Accept all'])[1]"))).click()

wait.until(EC.element_to_be_clickable((By.ID, "hArJGc"))).click()

search_boxes = wait.until(EC.visibility_of_all_elements_located((By.CLASS_NAME, 'tactile-searchbox-input')))
search_boxes[0].clear()
search_boxes[0].send_keys('Hyderabad')
search_boxes[1].clear()
search_boxes[1].send_keys('Guntur')
search_boxes[1].send_keys(Keys.ENTER)

wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@class='goog-inline-block goog-menu-button-dropdown']"))).click()
time.sleep(20)
© www.soinside.com 2019 - 2024. All rights reserved.