简体   繁体   中英

How to get the list of items from the p tags

I am trying to write a test in cucumber jvm which grabs the customers address from the page and assert its is not null.

<div class="padder">
<h3>Your details</h3>
<p>
<strong>MR Test Test</strong>
</p>
<p>Selly Road</p>
<p>London</p>
<p>GBR</p>
<p>TR02XZ</p>
</div>
</div>

I used xpath to find the first line of the address and the remaining p tags:

private static final By CUSTOMER_ADDRESS = By.xpath("//*[@id='yourDetails']/div/p[position() >=2]");

This is what I wrote so far but for some reason even though in the xpath I have declared to grab all the p tags after the 2nd p tag my below test does not work:

public List<WebElement> returnAllText(By element) {
    List<WebElement> all = driver.findElements(element);
    for (WebElement elements: all) {
       System.out.println(elements.getText());
    }
    return all;
}

When the loop runs it finds each line of the address but then it is lost when the return statement is run. How do I catch/store each p tag value and then group it as one string value and assert it is not null

I have debugged the code and the below is the results

When I debug line and evaluate the expression:

List<WebElement> all = driver.findElements(element)

result = {java.util.ArrayList@4562} size = 4
[0] = {org.openqa.selenium.remote.RemoteWebElement@4564}"[[RemoteWebDriver: chrome on MAC            (32479ef6b2783086b4c1d1ba0bbd1405)] -> xpath: //*[@id='yourDetails']/div/p[position() >=2]]"

[1] = {org.openqa.selenium.remote.RemoteWebElement@4565}"[[RemoteWebDriver: chrome on MAC (32479ef6b2783086b4c1d1ba0bbd1405)] -> xpath: //*[@id='yourDetails']/div/p[position() >=2]]"

[2] = {org.openqa.selenium.remote.RemoteWebElement@4566}"[[RemoteWebDriver: chrome on MAC (32479ef6b2783086b4c1d1ba0bbd1405)] -> xpath: //*[@id='yourDetails']/div/p[position() >=2]]"

[3] = {org.openqa.selenium.remote.RemoteWebElement@4567}"[[RemoteWebDriver: chrome on MAC (32479ef6b2783086b4c1d1ba0bbd1405)] -> xpath: //*[@id='yourDetails']/div/p[position() >=2]]"

Then I evaluate:

for(WebElement elements: all)

result = {java.util.ArrayList@4276} size = 4
[0] = {org.openqa.selenium.remote.RemoteWebElement@4288}"[[RemoteWebDriver: chrome on MAC (32479ef6b2783086b4c1d1ba0bbd1405)] -> xpath: //*[@id='yourDetails']/div/p[position() >=2]]"

[1] = {org.openqa.selenium.remote.RemoteWebElement@4289}"[[RemoteWebDriver: chrome on MAC (32479ef6b2783086b4c1d1ba0bbd1405)] -> xpath: //*[@id='yourDetails']/div/p[position() >=2]]"

[2] = {org.openqa.selenium.remote.RemoteWebElement@4290}"[[RemoteWebDriver: chrome on MAC (32479ef6b2783086b4c1d1ba0bbd1405)] -> xpath: //*[@id='yourDetails']/div/p[position() >=2]]"

[3] = {org.openqa.selenium.remote.RemoteWebElement@4291}"[[RemoteWebDriver: chrome on MAC (32479ef6b2783086b4c1d1ba0bbd1405)] -> xpath: //*[@id='yourDetails']/div/p[position() >=2]]"

Then I evaluate:

System.out.println(elements.getText())

result = {java.lang.String@4598}"London"
value = {char[9]@4599}
hash = 0
hash32 = 0

Evaluate:

return all;

result = {java.util.ArrayList@4276} size = 4
[0] = {org.openqa.selenium.remote.RemoteWebElement@4288}"[[RemoteWebDriver: chrome on MAC (32479ef6b2783086b4c1d1ba0bbd1405)] -> xpath: //*[@id='yourDetails']/div/p[position() >=2]]"

[1] = {org.openqa.selenium.remote.RemoteWebElement@4289}"[[RemoteWebDriver: chrome on MAC (32479ef6b2783086b4c1d1ba0bbd1405)] -> xpath: //*[@id='yourDetails']/div/p[position() >=2]]"

[2] = {org.openqa.selenium.remote.RemoteWebElement@4290}"[[RemoteWebDriver: chrome on MAC (32479ef6b2783086b4c1d1ba0bbd1405)] -> xpath: //*[@id='yourDetails']/div/p[position() >=2]]"

[3] = {org.openqa.selenium.remote.RemoteWebElement@4291}"[[RemoteWebDriver: chrome on MAC (32479ef6b2783086b4c1d1ba0bbd1405)] -> xpath: //*[@id='yourDetails']/div/p[position() >=2]]"

Your debug results (only "London") and your statement in the question ("the loop [...] finds each line") contradict each other. I suggest that you rewrite your method like this:

public String returnAllText(By element) {
    List<WebElement> all = driver.findElements(element);
    StringBuilder sb = new StringBuilder();
    int i = 1;
    for (WebElement element: all) {
        sb.append( i++ ).append( ": " ).append( element.getText() );
    }
    return sb.toString();
}

And let's see what this produces after the return from the method . -- The numbering (i) can be removed later, and a line feed might be added, depending on how you want the composed lines.

I managed to resolve the issue. For some reason using the xpath to retrieve all the p tags was not working so instead I used the div id that was one level higher up (parent of the div class=padder):

 private static final String CUSTOMER_DETAILS = "yourDetails";

this allowed me to retrieve all the elements containing text until the closing div

<h3>Your details</h3>
<p>
<strong>MR Test Test</strong>
</p>
<p>Selly Road</p>
<p>London</p>
<p>GBR</p>
<p>TR02XZ</p>

then I used this method to construct the address:

// return all text in a list and build into a string
public String returnAddress(By element) {
    String[] addressSplit = driver.findElement(element).getText().split("\n");
    StringBuilder str = new StringBuilder();
    for (int addressLength = 2; addressLength <= addressSplit.length - 1; addressLength++) {
        str.append(addressSplit[addressLength] + "\n");
    }
    return str.toString().trim();
}


// this calls the method above
public String customerAddressIsPresent() {
    return returnAddress(By.id(CUSTOMER_DETAILS));
}

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