2012-12-08 28 views
1

我想湊一個頁面,在這裏,如果存在數據,該網頁下面的「表格視圖」元素:Python中硒的is_element_present是給「錯誤」的答案

<div class="floatRight"> 
    <a id="MainContent_TabContainerUsageAndCost_TabPanelGasUsage_GasUsageView_UCGasUsage_lnkTableView2" title="Table View" href="javascript:__doPostBack(&#39;ctl00$ctl00$MainContent$TabContainerUsageAndCost$TabPanelGasUsage$GasUsageView$UCGasUsage$lnkTableView2&#39;,&#39;&#39;)" style="color:NoColor;">Table View</a> 

</div> 

否則,該頁面有如下錯誤信息。

<div class="error display"> 
       <span id="MainContent_TabContainerUsageAndCost_TabPanelElectricityUsage_ElectricityUsageView_UCElectricityUsage_lblMesg" style="font-weight:bold;">The account selected has no usage and costs data currently to be displayed.</span> 

</div> 

在硒中,我試圖在開始抓取數據之前檢查上述兩個條件。我試過如下:

if self.selenium.is_element_present('link=Table view'): 
    self.selenium.click('link=Table view') 
else: 
    return [] # data not found 

但是,「如果」語句總是返回「真」,使「點擊」被調用,即使頁面沒有足夠的「表視圖」鏈接/元。

那麼,我重寫了代碼來檢查錯誤消息,而不是,如下所示:

if self.selenium.is_element_present('MainContent_TabContainerUsageAndCost_TabPanelElectricityUsage_ElectricityUsageView_UCElectricityUsage_lblMesg'): 
    return [] # data not found 
else: 
    self.selenium.click('link=Table view') 

而現在,「if」語句總是返回false,使「點擊」正在叫它什麼時候不應該!

顯然,我沒有正確使用is_element_present。請指教。

回答

1

在Se-RC中,腳本可以向單元發送點擊事件,即使它們對用戶不可見 - 它們只需要在DOM中。在Se-WebDriver中,一個元素也必須對用戶可見。如果沒有看到他正在抓取的頁面的完整HTML,則無法進一步診斷。

+0

謝謝 - 這非常有幫助。我能解決這個問題。謝謝! – pratan