【问题标题】:Selenium can't find leaflet-interactive elementsSelenium 找不到传单交互元素
【发布时间】:2022-08-19 04:30:50
【问题描述】:

我试图在具有react-leaflet 地图的网页上获取元素。页面导入如下:

// Map.js component

import { Map, TileLayer, GeoJSON, Pane } from \'react-leaflet\'

...
  return(
    <Map
      <TileLayer ... />
    />
  )

然后 DOM 看起来像这样:

<div class=\"leaflet-container\">
  <div class=\"leaflet-pane\">
    <div class=\"leaflet-pane custom-pane\">
      <svg class=\"leaflet-zoom-animated\">
        <g>
          <path class=\"leaflet-interactive\">...</path>
          <path class=\"leaflet-interactive\">...</path>
          <path class=\"leaflet-interactive\">...</path>
          <path class=\"leaflet-interactive\">...</path>
          <path class=\"leaflet-interactive\">...</path>
        </g>
      </svg>
    </div>
  </div>
</div>

当我在浏览器控制台中查询元素时,我得到了所有 &lt;path&gt; 元素

document.querySelectorAll(\'.leaflet-interactive\')
// NodeList(5)

但是当我尝试从我的 Python 脚本中获取时,它找不到元素:

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


chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_options(\"detach\", True)

driver = webdriver.Chrome(options=chrome_options)
wait = WebDriverWait(driver, 10)

wait.until(EC.url_contains(\"map\"))

driver.get(url) # the webpage url
wait.until(EC.url_contains(f\"{latitude}/{longitude}\"))

# the following step fails
elements = wait.until(EC.visibility_of_all_elements_located((By.CLASS_NAME, \"leaflet-interactive\")))

不幸的是,它没有给我任何有用的错误(实际上没有例外)。

我正在查看其他问题,但看起来我的代码是正确的 - https://stackoverflow.com/a/63329510/11228445

这是传单渲染页面的问题吗?我看不到任何正在渲染的图层或 iframe。

    标签: python selenium selenium-webdriver leaflet


    【解决方案1】:

    经过一些实验,我验证了切换到

    # any instead of all
    elements = wait.until(EC.visibility_of_any_elements_located((By.CLASS_NAME, "leaflet-interactive"))) 
    

    返回适量的元素。

    所以最有可能发生的是,并非所有元素都在当前视口中呈现(窗口为 800 x 600);因此,在这种情况下,切换到 any 而不是 all 就足够了。

    【讨论】:

      猜你喜欢
      • 2021-08-02
      • 1970-01-01
      • 2020-03-08
      • 2021-12-16
      • 2021-04-22
      • 2021-02-23
      • 1970-01-01
      • 2022-07-01
      • 1970-01-01
      相关资源
      最近更新 更多