0

I have html code that is very similar to this:

<TH CLASS="ddtitle">MovieOne</TH>
        <TABLE  CLASS="datadisplaytable" ><CAPTION class="captiontext">Movies</CAPTION>
    <TR>
    <TH CLASS="ddheader" scope="col" >Genre</TH>
    <TH CLASS="ddheader" scope="col" >Time</TH>
    <TH CLASS="ddheader" scope="col" >Days</TH>
    <TH CLASS="ddheader" scope="col" >Where</TH>
    <TH CLASS="ddheader" scope="col" >Date Range</TH>
    <TH CLASS="ddheader" scope="col" >Seating</TH>
    <TH CLASS="ddheader" scope="col" >Actors</TH>
    </TR>
    <TR>
    <TD CLASS="dddefault">Action</TD>
    <TD CLASS="dddefault">10:00 am - 12:00 pm</TD>
    <TD CLASS="dddefault">SMTWTHFSA</TD>
    <TD CLASS="dddefault">AMC Showplace</TD>
    <TD CLASS="dddefault">Aug 20, 2014 - Sept 12, 2014</TD>
    <TD CLASS="dddefault">Reservations</TD>
    <TD CLASS="dddefault">Will Ferrel (<ABBR title= "Primary">P</ABBR>)  target="Will Ferrel" ></TD>
    </TR>
    </TABLE>
<TH CLASS="ddtitle">MovieTwo</TH>
        <TABLE  CLASS="datadisplaytable" ><CAPTION class="captiontext">Movies</CAPTION>
    <TR>
    <TH CLASS="ddheader" scope="col" >Genre</TH>
    <TH CLASS="ddheader" scope="col" >Time</TH>
    <TH CLASS="ddheader" scope="col" >Days</TH>
    <TH CLASS="ddheader" scope="col" >Where</TH>
    <TH CLASS="ddheader" scope="col" >Date Range</TH>
    <TH CLASS="ddheader" scope="col" >Seating</TH>
    <TH CLASS="ddheader" scope="col" >Actors</TH>
    </TR>
    <TR>
    <TD CLASS="dddefault">Action</TD>
    <TD CLASS="dddefault">11:00 am - 12:30 pm</TD>
    <TD CLASS="dddefault">SMTWTHFSA</TD>
    <TD CLASS="dddefault">Showplace Cinemas</TD>
    <TD CLASS="dddefault">Aug 20, 2014 - Sept 12, 2014</TD>
    <TD CLASS="dddefault">TBA</TD>
    <TD CLASS="dddefault">Zach Galifinakis (<ABBR title= "Primary">P</ABBR>)  target="Zach Galifinakis" ></TD>
    </TR>
    </TABLE>
<TH CLASS="ddtitle">MovieThree</TH>
<BR>
<BR>
Coming Soon
<BR>

What I want to be able to do, is take the individual table data that is relevant for the movie title, and if a Movie doesn't have a table I want to say the values are TBA. So far, I am able to get the relevant table information, but I am unable to skip a table. For example I use this code to get the genre of the movie:

int tcounter = 1;

for (Element elements : li) {
WebElement genre = driver.findElement(By.xpath("//table[@class='datadisplaytable']/descendant::table["+tcounter+"]//td[1]"));
WebElement time = driver.findElement(By.xpath("//table[@class='datadisplaytable']/descendant::table["+tcounter+"]//td[2]"));
WebElement days = driver.findElement(By.xpath("//table[@class='datadisplaytable']/descendant::table["+tcounter+"]//td[3]"));
WebElement where = driver.findElement(By.xpath("//table[@class='datadisplaytable']/descendant::table["+tcounter+"]//td[4]"));
WebElement date_range = driver.findElement(By.xpath("//table[@class='datadisplaytable']/descendant::table["+tcounter+"]//td[5]"));
WebElement seating = driver.findElement(By.xpath("//table[@class='datadisplaytable']/descendant::table["+tcounter+"]//td[6]"));
WebElement actors = driver.findElement(By.xpath("//table[@class='datadisplaytable']/descendant::table["+tcounter+"]//td[7]"));
tcounter++;
}

elements refers to a list storing all links on the webpage (result for [1] would be action, [2] would be 10:00 am - 12:00pm ...). This is within a for loop that increments the value of the tcounter by 1 in order to receive the data for different tables. Is there a way I can be able to tell the program to see if a table is present under the TH class, and if not give the values TBA and skip it?

This is my second attempt based on siking's answer:

List<WebElement> linstings = driver.findElements(By.className("ddtitle"));
    String genre = "";
    String time = "";
    String days = "";
    String where = "";
    String dateRange = "";
    String seating = "";
    String actors = "";
    for(WebElement potentialMovie : linstings) {
        try {
            WebElement actualMovie = potentialMovie.findElement(By.xpath("//table[@class='datadisplaytable']"));
            // System.out.println("Actual: " + actualMovie.getText());
            // make all your assignments, for example:
            type = actualMovie.findElement(By.xpath("/descendant::table//td")).getText();
            time = actualMovie.findElement(By.xpath("/descendant::table//td[2]")).getText();
            days = actualMovie.findElement(By.xpath("/descendant::table//td[3]")).getText();
            location = actualMovie.findElement(By.xpath("/descendant::table//td[4]")).getText();
            dates = actualMovie.findElement(By.xpath("/descendant::table//td[5]")).getText();
            schedType = actualMovie.findElement(By.xpath("/descendant::table//td[6]")).getText();
            instructor = actualMovie.findElement(By.xpath("/descendant::table//td[7]")).getText();
            System.out.println(genre+" "+time+" "+days+" "+where+" "+dateRange+" "+actors);

        } catch(Exception ex) {
            // there is no table, so:
            genre = "TBA";
        }
    }

The problem with this code is that it keeps returning the values for only the first table.

1 Answer 1

1

I trimmed down your HTML sample to the following:

<TH CLASS="ddtitle">MovieOne</TH>
<TABLE CLASS="datadisplaytable">
    <CAPTION class="captiontext">Movies</CAPTION>
    <TR>
        <TH CLASS="ddheader" scope="col">Genre</TH>
    </TR>
    <TR>
        <TD CLASS="dddefault">Action</TD>
    </TR>
</TABLE>
<TH CLASS="ddtitle">MovieTwo</TH>
<BR/>
<BR/>
Coming Soon
<BR/>
<TH CLASS="ddtitle">MovieThree</TH>
<TABLE CLASS="datadisplaytable">
    <CAPTION class="captiontext">Movies</CAPTION>
    <TR>
        <TH CLASS="ddheader" scope="col">Genre</TH>
    </TR>
    <TR>
        <TD CLASS="dddefault">Action</TD>
    </TR>
</TABLE>

Hopefully it is representative of all your cases!

Don't use a counter, but use the actual WebElements to iterate over:

// default all your variables to TBA, like:
String genre = "TBA";

// find all the listings on the page...
List<WebElement> linstings = driver.findElements(By.className("ddtitle"));
// ... and iterate over them
for (WebElement listing : linstings) {

    // grab whatever is the _first_ element under the TH ...
    WebElement potentialMovie = listing.findElement(By.xpath("following-sibling::*[1]"));
    // ... check if it has a child element CAPTION
    if (potentialMovie.findElement(By.xpath("caption")) != null) {
        // make all your assignments, for example:
        genre = potentialMovie.findElement(By.xpath("tr[2]/td[1]")).getText();
    }
}

Please note that this code is untested, your mileage may vary!

Sign up to request clarification or add additional context in comments.

15 Comments

OK, but how would I construct an if statement that would give TBA values to a TH that isn't followed by a table with info. xpath? css selection? For example: how would I tell the program that since movieThree doesn't have a table below it, everything equals TBA?
Where is this "TBA" stored? You need to post more of your code, and be more clear in your requirement.
Thanks for the response, I've added some more information. What I specifically want to do is be able to tell the program that if there is not a table with the movie information directly below the TH tag that contains the movie name (ex: MovieOne), then to give TBA values for each variable, and move on to the next table.
Cool thanks. I need to work on the mileage for a little bit, but I can tell already that using a loop is a much better way to go than the counter, I'll let you know if I can get it to work fully.
Hey SiKing, I just edited my question to include my attempt using your answer. The problem I'am having now is that it keeps returning only the information from the first table. Did I not use your answer correctly?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.