2017-01-13 56 views
0

如何根據leafletjs從地圖彈出框中獲取文本。該url is this。我也得到這個錯誤:NoSuchElementExceptionPython Selenium - 從LeafletJS彈出框和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,並且對所有這些都是新的?

+0

的可能的複製[在Selenium中避免NoSuchElementException的最佳方法是什麼?](http://stackoverflow.com/questions/19536954/what-is-the-best-way-to-avoid-nosuchelementexception-in-selenium) –

回答

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 
+0

但是不打印彈出框中的所有文字?謝謝你的時間。 –

+0

@UmarYusuf啊是的,彈出窗口的某些部分甚至會在後面加載,有趣。我不熟悉leaflet.js和這個特定的網站,並不完全確定你可以依賴哪些東西。但是,一種方法是再添加一個等待來等待彈出窗口中的'b'元素包含「Perceelnummer」,這些行包括:'wait.until(EC.text_to_be_present_in_element((By.CSS_SELECTOR,'.leaflet -popup-content b'),「Perceelnummer」))'。讓我知道它是否有效,謝謝。 – alecxe

+0

我慷慨地需要你的擴展在擴展問題上:http://stackoverflow.com/questions/41821183/python-selenium-add-content-to-pandas-dataframe我試圖循環多個郵政編碼,並將他們的數據刮到padas數據幀 –

相關問題