2017-12-14 10 views
0

JavaでSeleniumを使用しています.Spanタグからテキストを取得できません。空の文字列を返します。 HTMLコード:テキストが含まれているときにdivからテキストを取得できません<span>

<div class="fade-repeat-animation alert ng-isolate-scope alert-error" ng-class="type && "alert-" + type" ng-repeat="alert in alerts" type="alert.type" close="closeAlert($index)"> 
 
    <button class="close" ng-click="close()" type="button" ng-show="closeable">×</button> 
 
    <div ng-transclude=""> 
 
     <span class="ng-scope ng-binding">This text</span> 
 
    </div> 
 
</div>

これは、セレンコードです:

String ipError = within(secs(10)).div(By.xpath("//div[@class='fade-repeat-animation alert ng-isolate-scope alert-error']/div/span")).getAttribute("innerHTML"); 

私はgetText()getAttribute("textContent")も使用することを試みたが、常にString空を返します。

答えて

0

、あなたは次のコード行を使用することができ、テキストThis text印刷するために提供しているHTMLを1として、このコードを試してみてください

String Spantext=driver.findElement(By.xpath("//span[@class='ng-scope ng-binding']")).getText(); 
System.out.println(Spantext); 
+0

@N。 Holは働いている私のコードを試してください – iamsankalp89

+0

ありがとう。このコードは本当に私を助けました!すべて正常に動作します) –

0

angleJSのためにある可能性があります。WebdriverがUIからテキストを読み取ることができません。 このコードを使用して、動作しているかどうか教えてください。

あなたが getText()方法で試してみて
driver.findElement(By.cssSelector(".fade-repeat-animation.alert.ng-isolate-scope .alert-error")).getText(); 
+0

残念ながら助けられませんでした。 "原因:org.openqa.selenium.NoSuchElementException" –

0

System.out.println(new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='fade-repeat-animation alert ng-isolate-scope alert-error']//div/span[@class='ng-scope ng-binding']"))).getAttribute("innerHTML")); 
0

次のコードも機能する必要があります。

text = driver.findElement(By.cssSelector(".ng-scope.ng-binding")).getText(); 

希望はあなたを助けます!

関連する問題