2014-09-18 216 views
-1

我想在此頁面的彈出窗口中獲取鏈接。 http://stivconsultasexternas.cnbv.gob.mx/ConsultaInformacionEmisoras.aspx 單擊出現頁面的第一個鏈接會顯示一個彈出窗口。我喜歡在薄窗口中獲得鏈接。在彈出窗口中獲取鏈接(python,selenium)

這是我的代碼。我感謝任何幫助。

from selenium import webdriver 
import time 
driver = webdriver.Firefox() 
driver.get("http://stivconsultasexternas.cnbv.gob.mx/ConsultaInformacionEmisoras.aspx") 
link=driver.find_elements_by_partial_link_text(u"Verum") 
time.sleep(5) 
link[0].click() 
time.sleep(5) 
print driver.page_source.encode("utf_8","ignore")#Here I want a html of a popup window. 

非常感謝。

回答

0

您遇到的問題是彈出窗口是iframe。 Selenium只能訪問當前內容,無法訪問兒童iframe中的任何內容。爲了讓硒訪問新iframe,你需要使用switch_to.frame

link[0].click() 
time.sleep(5) 
driver.switch_to.frame("DefaultPlaceholder_callbackPanel_Popup_CIF-1") 
print driver.page_source.encode("utf_8","ignore")#Here I want a html of a popup window. 
driver.switch_to.default_content() 
driver.find_element_by_xpath("//td[@id='DefaultPlaceholder_callbackPanel_Popup_HCB-1']").click() 

編輯#1:

我看着網頁,發現你想要的人口信息網訪問載在此iframe

<iframe id="DefaultPlaceholder_callbackPanel_Popup_CIF-1" title="" src="/Detalle.aspx?enc=vxwQ2MoMebbVt2C93KNYJA%3d%3d" scrolling="auto" frameborder="0" style="height: 578px; width: 100%;"> 

switch_to.frame()可以接受以下作爲標識符:
- 的nameiframe的。由於iframeid="DefaultPlaceholder_callbackPanel_Popup_CIF-1"
- iframe的索引,我用這個特例。這看起來像switch_to.frame(0)
- iframe元素。你可以找到通過find_element_by_定位器iframe元素,然後使用該webelement結果switch_to.frame(el)

編輯#2:

要關閉該對話框,您需要使用switch_to.default_content(),作爲關閉按鈕外iframe。然後可以單擊對話框的關閉按鈕,然後繼續。查看上面代碼中的最後一行,該行應該爲您關閉對話框。

+0

非常感謝。如果不是不方便你解釋我你怎麼知道這個彈出框是DefaultPlaceholder_callbackPanel_Popup_CIF-1?無論如何,我感謝你的幫助。 – Nash 2014-09-19 15:37:26

+0

@Nash希望我的編輯很有用。 – Richard 2014-09-19 16:25:54

+0

對不起後一個問題。我如何關閉現在打開的框架。因爲我想用於打開許多幀。我想,當一個框架打開時,我無法打開另一個框架鏈接。非常感謝。 – Nash 2014-09-19 17:20:13