![](/img/trans.png)
[英]python selenium webdriver cannot figure how to find the element
[英]Selenium webdriver with python to scrape dynamic page cannot find element
所以在stackoverflow上有很多關於動態內容抓取的問題,我經歷了所有這些,但是所有建議的解決方案都不適用於以下問題:
我無法訪問此頁面上的任何 DOM 元素。 請注意,如果我能得到一些有關如何訪問搜索欄和搜索按鈕的提示,那將是一個很好的開始。 See page to scrape最后我想要的是瀏覽地址列表,啟動搜索,並復制屏幕右側顯示的信息。
我嘗試了以下方法:
增加了頁面加載的等待時間
try: WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.ID, "addressInput"))) except: print "address input not found"
問題
您可以使用此 url http://50.17.237.182/PIM/
獲取源:
In [73]: from selenium import webdriver
In [74]: dr = webdriver.PhantomJS()
In [75]: dr.get("http://50.17.237.182/PIM/")
In [76]: print(dr.find_element_by_id("addressInput"))
<selenium.webdriver.remote.webelement.WebElement object at 0x7f4d21c80950>
如果查看返回的源代碼,就會發現帶有該 src url 的 frame 屬性:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>San Francisco Property Information Map </title>
<META name="description" content="Public access to useful property information and resources at the click of a mouse"><META name="keywords" content="san francisco, property, information, map, public, zoning, preservation, projects, permits, complaints, appeals">
</head>
<frameset rows="100%,*" border="0">
<frame src="http://50.17.237.182/PIM" frameborder="0" />
<frame frameborder="0" noresize />
</frameset>
<!-- pageok -->
<!-- 02 -->
<!-- -->
</html>
感謝@Alecxe,這是使用dr.switch_to.frame(0)
的最簡單方法:
In [77]: dr = webdriver.PhantomJS()
In [78]: dr.get("http://propertymap.sfplanning.org/")
In [79]: dr.switch_to.frame(0)
In [80]: print(dr.find_element_by_id("addressInput"))
<selenium.webdriver.remote.webelement.WebElement object at 0x7f4d21c80190>
如果您在瀏覽器中訪問http://50.17.237.182/PIM/
,您將看到與propertymap.sfplanning.org/
完全相同的內容,唯一的區別是您可以使用前者完全訪問元素。
如果你想輸入一個值並點擊搜索框,它是這樣的:
from selenium import webdriver
dr = webdriver.PhantomJS()
dr.get("http://propertymap.sfplanning.org/")
dr.switch_to.frame(0)
dr.find_element_by_id("addressInput").send_keys("whatever")
dr.find_element_by_xpath("//input[@title='Search button']").click()
但是如果你想拉數據,你可能會發現使用 url 查詢是一個更簡單的選擇,你會從查詢中得到一些 json。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.