【问题标题】:Python Selenium - get text from LeafletJS pop-up box and NoSuchElementExceptionPython Selenium - 从 LeafletJS 弹出框和 NoSuchElementException 获取文本
【发布时间】:2017-05-28 23:43:05
【问题描述】:

如何从基于leafletjs 的地图弹出框中获取文本。 url is this。我也收到此错误:NoSuchElementException

如果我搜索包裹并输入如下所示的相关信息,那么我想获取带有 class_name: 'leaflet-popup-content' 的框上的所有文本?

# Creates an instance driver object...
driver = webdriver.Chrome()

# load the url above
driver.get(url)

# =============
# Find and fill SEARCH BOX by id....
driver.find_element_by_id('searchBox').send_keys('1083CX')

# Send the form by clicking on the searcht botton...
driver.find_element_by_id('searchButton').click()

driver.find_element_by_id('listElementContent0').click()
# driver.find_element_by_class_name('content').click()

# =============
txt = driver.find_element_by_class_name('leaflet-popup-content').text()
print (txt)

What is the best way to avoid NoSuchElementException in Selenium? 的这个问题的分析器使用了 Java,我不明白。我正在使用 Python 并且对这一切都不熟悉?

【问题讨论】:

标签: javascript python selenium selenium-webdriver leaflet


【解决方案1】:

这是时间问题,需要让浏览器加载搜索结果和弹窗内容:

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

# ...

wait = WebDriverWait(driver, 10)

driver.find_element_by_id('searchBox').send_keys('1083CX')
driver.find_element_by_id('searchButton').click()

wait.until(EC.visibility_of_element_located((By.ID, 'listElementContent0'))).click()

# wait for popup to appear and contain data
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, '.leaflet-popup-content b')))

popup = driver.find_element_by_class_name("leaflet-popup-content")
print(popup.text)

为我工作并打印:

Kadastrale aanduiding: ASD30AK02554G0000
Kadastrale grootte (m2) : 930
aanvullende gegevens bij het kadaster aanvragen

【讨论】:

  • 但是不是打印弹出框上的所有文本吗?感谢您的宝贵时间。
  • @UmarYusuf 啊,是的,弹出窗口的某些部分甚至更晚才加载,很有趣。我不熟悉 Leaflet.js 和这个特定的站点,也不完全确定您可以依赖哪些东西。但是,一种方法是再添加一个等待以等待弹出窗口中的b 元素包含“Perceelnummer”,类似于以下内容:wait.until(EC.text_to_be_present_in_element((By.CSS_SELECTOR, '.leaflet-popup-content b'), "Perceelnummer"))。让我知道它是否有效,谢谢。
  • 我需要您关注扩展问题:stackoverflow.com/questions/41821183/… 我正在尝试遍历多个邮政编码并将其数据刮入 padas 数据框
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-19
  • 1970-01-01
  • 2018-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多