简体   繁体   中英

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.

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");

As you mentioned you are able to get the assignee name easily with below xpath:

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

Now, to get the span class value in separate string, eg 1111 FRANKLIN STREET OAKLAND , CALIFORNIA 94607-5200 , you can use the following code block :

List<WebElement> myElements = driver.findElements(By.xpath("//*[@id='printable-area']/table[" + r + "]/tbody/tr[4]/td/a[@class='ng-binding ng-scope']/span[@class='ng-binding ng-scope']"));
List<String> partA = new ArrayList<String>();
List<String> partB = new ArrayList<String>();
for(int i=0; i<myElements.size(); i=i+2)
{
    String textA = myElements.get(i).getAttribute("innerHTML");
    partA.add(textA);
}

for(int j=1; j<myElements.size(); j=j+2)
{
    String textB = myElements.get(j).getAttribute("innerHTML");
    partB.add(textB);
}
System.out.println("The Complete Addresses are : ");
for(int k=0; k<partA.size();k++)
    System.out.println(partA.get(k) + " , " + partB.get(k));    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM