这篇文章与这篇文章非常相似:使用 selenium 和 python 在鼠标悬停后弹出时提取数据
但我无法找到我想要的答案。
我正在尝试抓取与此非常相似的传单地图:https://leafletjs.com/examples/choropleth/,理想情况下,我想下载将鼠标移到多边形上后出现的所有信息:
原始帖子循环遍历每个圆元素,我想对每个多边形执行相同的操作。
代码试验:
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Chrome
driver.get("https://leafletjs.com/examples/choropleth/")
timeout = 1000
explicit_wait30 = WebDriverWait(driver, 30)
try:
# Wait for all circles to load
poli = explicit_wait30.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, '.leaflet-interactive')))
except TimeoutException:
driver.refresh()
data = []
i=1
for circle in poli:
i+=1
# Execute mouseover on the element
driver.execute_script("const mouseoverEvent = new Event('mouseover');arguments[0].dispatchEvent(mouseoverEvent)", poli)
# Wait for the data to appear …Run Code Online (Sandbox Code Playgroud)