2016-08-30 11 views
1

Selenium C#を使用して要素の後にテキストを抽出しようとしています。Xpathを使用してテキストを抽出する

XPath Checkerを使用してテキストの検索に使用するXPathを把握しました。

//*[@id='selectedEciId_5555555']/../following-sibling::text()[1] 

上記を使用すると、XPathチェッカーの "5555555(Customer 5 Name)"が返されます。

次のコード行を使用して、変数に "5555555(Customer 5 Name)"というテキストを抽出する次のコードを試しました。

var customer = driver.FindElement(By.XPath("//*@id='selectedEciId_2217611']/../following-sibling::text()[1]")).Text; 

ただし、「引数1のWindow.getComputedStyleがinterface要素を実装していません」というメッセージが表示されます。

テキストを抽出するための正しいコードを手助けできる人はいますか?参考のために、以下のコピー

HTMLコード:

 <label id="iCheck-txcdyh" for="selectedEciId_1111111" class="ibm-access"></label> 
     <div aria-disabled="false" aria-checked="false" style="position: relative;" class="iradio_square-blue" role="radio" aria-labelledby="iCheck-txcdyh"><input style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;" name="selectedCompanyId" id="selectedEciId_1111111" value="1111111" type="radio"><ins style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;" class="iCheck-helper"></ins></div>&nbsp;1111111&nbsp;(Customer 1 Name)<br> 

     <label id="iCheck-x44hah" for="selectedEciId_2222222" class="ibm-access"></label> 
     <div aria-disabled="false" aria-checked="false" style="position: relative;" class="iradio_square-blue" role="radio" aria-labelledby="iCheck-x44hah"><input style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;" name="selectedCompanyId" id="selectedEciId_2222222" value="2222222" type="radio"><ins style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;" class="iCheck-helper"></ins></div>&nbsp;2222222&nbsp;(Customer 2 Name)<br> 

     <label id="iCheck-vljexv" for="selectedEciId_3333333" class="ibm-access"></label> 
     <div aria-disabled="false" aria-checked="false" style="position: relative;" class="iradio_square-blue" role="radio" aria-labelledby="iCheck-vljexv"><input style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;" name="selectedCompanyId" id="selectedEciId_3333333" value="3333333" type="radio"><ins style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;" class="iCheck-helper"></ins></div>&nbsp;3333333&nbsp;(Customer 3 Name)<br> 

     <label id="iCheck-k4wo8e" for="selectedEciId_4444444" class="ibm-access"></label> 
     <div aria-disabled="false" aria-checked="false" style="position: relative;" class="iradio_square-blue" role="radio" aria-labelledby="iCheck-k4wo8e"><input style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;" name="selectedCompanyId" id="selectedEciId_4444444" value="4444444" type="radio"><ins style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;" class="iCheck-helper"></ins></div>&nbsp;4444444&nbsp;(Customer 4 Name)<br> 

     <label id="iCheck-zf06hv" for="selectedEciId_5555555" class="ibm-access"></label> 
     <div aria-disabled="false" aria-checked="false" style="position: relative;" class="iradio_square-blue" role="radio" aria-labelledby="iCheck-zf06hv"><input style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;" name="selectedCompanyId" id="selectedEciId_5555555" value="5555555" type="radio"><ins style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;" class="iCheck-helper"></ins></div>&nbsp;5555555&nbsp;(Customer 5 Name)<br> 

     <label id="iCheck-d9oyt0" for="selectedEciId_6666666" class="ibm-access"></label> 
     <div aria-disabled="false" aria-checked="false" style="position: relative;" class="iradio_square-blue" role="radio" aria-labelledby="iCheck-d9oyt0"><input style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;" name="selectedCompanyId" id="selectedEciId_6666666" value="6666666" type="radio"><ins style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;" class="iCheck-helper"></ins></div>&nbsp;6666666&nbsp;(Customer 6 Name)<br> 

答えて

0

私はセレンがテキストを取得する前に要素のvisibiltyをチェックしようとしているときにエラーが発生していると信じています。

一つの解決策は、javascriptを

var script = @"return arguments[0].nodeValue;"; 

var element = driver.FindElement(By.XPath("//*@id='selectedEciId_1111111']/../following-sibling::text()[1]")); 

IJavaScriptExecutor js = (IJavaScriptExecutor)driver; 
elementText = js.ExecuteScript(script, element).ToString(); 
+0

はありがとうござい使用してテキストを取得することができます。これは、文字列にキャストを追加した後、完全に機能しましたstring customer =(文字列)js.ExecuteScript(スクリプト、要素); –

+0

心配しないで@RobertBelknap。オブジェクトを文字列に変換する答えを更新しました。もう少しエラー処理をするのが良いでしょう。 – shivansh

関連する問題