2016-11-10 46 views
-2

Website url 我正在使用硒-python 我無法找到網站中的特定元素。我正在尋找webscrape它的信息{數據座標}。我找不到該元素並從中獲取信息。請在此分享您對此問題的看法。 Screenshot of the HTML code and the element。在此先感謝無法找到硒python中的特定元素

嘗試代碼: -

fill=driver.find_element_by_xpath("//div[contains(@class,'inner fill')]") 

latlong=fill.find_element_by_xpath("//div[contains(@class,'map.space-map.leaflet-container')]") 

latlong.find_attribute["data-coordinates"] 

Result :- Message: Unable to locate element: {"method":"xpath","selector":"//div[contains(@class,'map.space-map.leaflet-container')]"} 
+0

你遇到兄弟html DOM元素。使用xpath來查找'n-element'。此外,請分享您已經嘗試過的代碼。 –

+0

歡迎來到Stack Overflow!請閱讀[問]。請提供您嘗試過的代碼以及執行結果,包括任何錯誤消息等。還請提供指向頁面和/或相關HTML的鏈接。 – JeffC

+0

謝謝@Igor Savinkin和JeffC。這是我試過的代碼。填= driver.find_element_by_xpath( 「// DIV [含有(@類, '內填充')]」) 經緯度= fill.find_element_by_xpath( 「// DIV [含有(@類,'地圖空間的地圖小葉容器」 ) latlong.find_attribute [「data-coordinates」] and result was this Message:無法找到元素:{「method」:「xpath」,「selector」:「// div [contains(@ class,'map space -map leaflet-container')]「和網址是https://www.appearhere.co.uk/spaces/north-kensington-upcycling-store-and-cafe –

回答

0

如果我可以加我的兩分錢:

,你可以做到這一點與硒+ BeautifulSoup(https://www.crummy.com/software/BeautifulSoup/bs4/doc/)的幫助下(做:PIP安裝BeautifulSoup )不是運行下面的代碼:

from selenium import webdriver 
from bs4 import BeautifulSoup 


driver = webdriver.Chrome() 
url= "https://www.appearhere.co.uk/spaces/north-kensington-upcycling-store-and-cafe" 
driver.maximize_window() 
driver.get(url) 

content = driver.page_source.encode('utf-8').strip() 
soup = BeautifulSoup(content,"html.parser") 
cord = soup.find("div", {"class": "map space-map leaflet-container leaflet-fade-anim"})['data-coordinates'] 
print cord 
driver.quit() 

它將打印座標:

[51.5235108631773, -0.206594467163086] 

希望你只想這個。享受

+0

令人驚歎。我明白了。這工作,感謝很多! @thebadguy –

+0

但我只是有一個小小的懷疑。爲什麼它有時工作,並拋出這個錯誤有時。 Data_Coordinates = soup.find( 「DIV」,{ 「類」: 「地圖空間的地圖小葉容器小葉-淡入動畫」})[ '數據座標'] 類型錯誤: 'NoneType' 對象未標化的 –

+0

的時間同步可以發出,你可以在driver.get(url)之後添加wait或time.sleep(3)。希望它能解決問題 – thebadguy