データが存在する場合、ページに次の「テーブルビュー」要素があるページをスクレイピングしようとしています:
<div class="floatRight">
<a id="MainContent_TabContainerUsageAndCost_TabPanelGasUsage_GasUsageView_UCGasUsage_lnkTableView2" title="Table View" href="javascript:__doPostBack('ctl00$ctl00$MainContent$TabContainerUsageAndCost$TabPanelGasUsage$GasUsageView$UCGasUsage$lnkTableView2','')" 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>
セレンでは、データのスクレイピングを開始する前に、上記の 2 つの条件を確認しようとしています。私は次のことを試しました:
if self.selenium.is_element_present('link=Table view'):
self.selenium.click('link=Table view')
else:
return [] # data not found
ただし、「if」ステートメントは常に「true」を返すため、ページに「テーブルビュー」リンク/要素がなくても「クリック」が呼び出されます。
そこで、代わりに次のようにエラー メッセージをチェックするようにコードを書き直しました。
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 を正しく使用していません。お知らせ下さい。