Python Selenium - 从LeafletJS弹出框和NoSuchElementException获取文本

时间:2017-01-13 14:55:19

标签: javascript python selenium selenium-webdriver leaflet

如何根据leafletjs从地图弹出框中获取文本。 url is thisWhat is the best way to avoid NoSuchElementException in Selenium?。我也收到此错误: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)

这篇关于{{3}}问题的内容使用了Java,我不明白。我正在使用Python并且对这一切都不熟悉吗?

1 个答案:

答案 0 :(得分:0)

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

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