user2433924
user2433924

Reputation: 49

getText between tags with Selenium

I'm trying to parse the following XML file so that I can get some attributes. One thing I need to do is verify that the content between tags is not empty. To do this, I though I would be able to use the getText method provided for web elements.

The XML file:

<results>
    <result index="1">
        <track>
            <creator>Cool</creator>
            <album>Amazing</album>
            <title>Awesome and Fun</title>
        </track>
    </result>
</results>

My code for parsing through and getting what I want is as follows (keep in mind there is more than one result):

boolean result = false;

driver.get(url);

List<WebElement> result_list = driver.findElements(By.xpath(".//result"));

if (result_list.size() == num_results) {

try {
    for (int i = 0; i < result_list.size(); i++) {
    WebElement track = result_list.get(i).findElement(By.xpath(".//track"));

    WebElement creator = track.findElement(By.xpath(".//creator"));
            System.out.println(creator.getText());
    track.findElement(By.xpath(".//album"));
    track.findElement(By.xpath(".//title"));
    }
    result = true;
}
catch (Exception e) {
    result = false;
}
}       
return result;

The problem is that the System.out.println call returns an empty string when there is clearly text between the creator tags. Any help would be greatly appreciated!

Upvotes: 0

Views: 1099

Answers (1)

Farlan
Farlan

Reputation: 1900

The problem here is most likely that loading that xml file into a browser makes your xml document change into an html interpretation by the browser itself. Loading this in Chrome results in a <track></track> tag which confirms your output of a blank string.

I would look at an xml parser instead of trying to do this via an automation tool.

Upvotes: 2

Related Questions