0

How can I get the text out of a map pop-up box based on leafletjs. The url is this. I also get this error: NoSuchElementException

If I searched for parcel and entered the relevant information as seen below, then I want to get all the text on the box with 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)

This anser to the question at What is the best way to avoid NoSuchElementException in Selenium? made use of Java, which I don't understand. I am using Python and am new to all this?

Community
  • 1
  • 1
Umar Yusuf
  • 926
  • 2
  • 13
  • 30
  • Possible duplicate of [What is the best way to avoid NoSuchElementException in Selenium?](http://stackoverflow.com/questions/19536954/what-is-the-best-way-to-avoid-nosuchelementexception-in-selenium) – xandermonkey Jan 13 '17 at 15:22

1 Answers1

0

This is a timing issue, you need to let the browser load the search result and the popup content:

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)

Works for me and prints:

Kadastrale aanduiding: ASD30AK02554G0000
Kadastrale grootte (m2) : 930
aanvullende gegevens bij het kadaster aanvragen
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • But is not printing all the text on the popup box? Thanks for your time. – Umar Yusuf Jan 14 '17 at 06:28
  • @UmarYusuf ah yeah, some parts of the popup are loaded even later, interesting. I'm not familiar with the leaflet.js and this particular site and not completely sure which things you can rely on. But, one approach is to add a one more wait to wait for the `b` element in the popup to contain "Perceelnummer", something along these lines: `wait.until(EC.text_to_be_present_in_element((By.CSS_SELECTOR, '.leaflet-popup-content b'), "Perceelnummer"))`. Let me know if it works or not, thanks. – alecxe Jan 14 '17 at 13:42
  • I kindly need your attension on the extended question at: http://stackoverflow.com/questions/41821183/python-selenium-add-content-to-pandas-dataframe Am trying loop over multi zip codes and scrape their data into padas dataframe – Umar Yusuf Jan 24 '17 at 06:14