java - Skip table if not found using selenium -
i have html code similar 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 - 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 - 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 <br>
what want able do, take individual table info relevant film title, , if film doesn't have table want values tba. far, able relevant table information, unable skip table. illustration utilize code genre of movie:
int tcounter = 1; (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 = 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 list storing links on webpage (result [1] action, [2] 10:00 - 12:00pm ...). within loop increments value of tcounter 1 in order receive info different tables. there way can able tell programme see if table nowadays under th class, , if not give values tba , skip it?
this sec effort based on siking's answer:
list<webelement> linstings = driver.findelements(by.classname("ddtitle")); string genre = ""; string time = ""; string days = ""; string = ""; string daterange = ""; string seating = ""; string actors = ""; for(webelement potentialmovie : linstings) { seek { webelement actualmovie = potentialmovie.findelement(by.xpath("//table[@class='datadisplaytable']")); // system.out.println("actual: " + actualmovie.gettext()); // create assignments, 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 no table, so: genre = "tba"; } }
the problem code keeps returning values first table.
i trimmed downwards html sample 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 <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 representative of cases!
don't utilize counter, utilize actual webelement
s iterate over:
// default variables tba, like: string genre = "tba"; // find listings on page... list<webelement> linstings = driver.findelements(by.classname("ddtitle")); // ... , iterate on them (webelement listing : linstings) { // grab whatever _first_ element under th ... webelement potentialmovie = listing.findelement(by.xpath("following-sibling::*[1]")); // ... check if has kid element caption if (potentialmovie.findelement(by.xpath("caption")) != null) { // create assignments, example: genre = potentialmovie.findelement(by.xpath("tr[2]/td[1]")).gettext(); } }
please note code untested, mileage may vary!
java selenium xpath
No comments:
Post a Comment