Prabu Prabu - 11 months ago 133
Java Question

Span class getText() using Selenium WebDriver with Java

Its a table inside one of the row contains the below td available, I want to get the span class value in separate string (its dynamic n number of span may available),
means

1111 FRANKLIN STREET OAKLAND, CALIFORNIA 94607-5200
:

HTML

<td colspan="5">
<p class="caption">Assignee</p>
<a class="ng-binding ng-scope" alt="View assignment(s)" ui-sref="search.resultAssignee(goToAssignee(assignee))" ng-repeat-start="assignee in abstract.patAssigneeName track by $index" href="#assigneeName=THE%20REGENTS%20OF%20THE%20UNIVERSITY%20OF%20CALIFORNIA">UNIVERSITY OF CALIFORNIA</a>
<p class="ng-scope">
<span class="ng-binding ng-scope" ng-if="abstract.patAssigneeAddress1[$index] !== ''">1111 FRANKLIN STREET<br></span>
<span class="ng-binding ng-scope" ng-if="abstract.patAssigneeCity[$index] !== '' && abstract.patAssigneeState[$index] !== ''">OAKLAND, CALIFORNIA 94607-5200<br></span>
<span class="ng-binding"> </span>
</p>
<br class="ng-scope" ng-repeat-end="">
</td>


I getting the assignee name easily with below XPath (iterate with table):

String recAssignee = driver.findElement(By.xpath(" //*[@id='printable-area']/table[" + r + "]/tbody/tr[4]/td/a")).getText().replace("Assignee", "");


Please help to get the span class value.

Answer Source

First get all span elements and keep them on a WebElement list. Then get the element using getText() in the loop. You can use like below:

List<WebElement> allSpanElements = driver.findElements(By.xpath(" //*[@id='printable-area']/table[" + r + "]/tbody/tr[4]/td/p[2]/span"));
for (WebElement elem : allSpanElements) {
    String address = elem.getText();
    //System.out.println(address);
}

You can use to keep the value in the String array.

You can also try using the class name instead of xpath like -

By.className("ng-binding ng-scope");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download