Unble to locate the dynamic search result using Selenium java - java

I am trying to validate search results with searched values. There's a table which displays all the existing keywords. When particular keyword is need to be listed user can search the keyword. search results are displayed in the same table which all keywords were listed before searching.
In particularly when I search for keyword android, "android headlines" and "android" keywords are displayed in the table body. I got the location of these two elements using xpath and printed in the console.But in the console it displays as "Description" and "Oppo" which displays in 1st and 2nd row which had all the keywords listed.
below is the HTML code.
<div id="keyword-grid" class="grid-view">
<table class="items table table-striped">
<thead>
<tr>
<th id="keyword-grid_c0"><a class="sort-link" href="/index.php/keyword/Admin?Keyword_sort=name">Name<span class="caret"></span></a></th>
<th id="keyword-grid_c1"><a class="sort-link" href="/index.php/keyword/Admin?Keyword_sort=behaviour">Behaviour<span class="caret"></span></a></th>
<th id="keyword-grid_c2"><a class="sort-link" href="/index.php/keyword/Admin?Keyword_sort=status">Status<span class="caret"></span></a></th>
<th id="keyword-grid_c3"><a class="sort-link" href="/index.php/keyword/Admin?Keyword_sort=createdAt">Created At<span class="caret"></span></a></th>
<th id="keyword-grid_c4"><a class="sort-link" href="/index.php/keyword/Admin?Keyword_sort=createdBy">Created By<span class="caret"></span></a></th>
<th class="button-column" id="keyword-grid_c5"> </th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Description</td>
<td>Manual</td>
<td><span class="badge badge-success">Active</span></td>
<td>2018-01-16 05:48:02</td>
<td>Admin</td>
<td class="button-column"><a class="view" title="View" rel="tooltip" href="/index.php/keyword/View/163"><i class="icon-eye-open"></i></a> </td>
<tr class="odd">
<td>Oppo</td>
<td>Manual</td>
<td><span class="badge badge-success">Active</span></td>
<td>2017-11-16 06:32:19</td>
<td>Digitalbox Testing</td>
<td class="button-column"><a class="view" title="View" rel="tooltip" href="/index.php/keyword/View/161"><i class="icon-eye-open"></i></a> </td>
</tr>
<tr class="even">
<td>android headlines</td>
<td>Manual</td>
<td><span class="badge badge-success">Active</span></td>
<td>2017-11-15 07:53:02</td>
<td>Admin</td>
<td class="button-column"><a class="view" title="View" rel="tooltip" href="/index.php/keyword/View/160"><i class="icon-eye-open"></i></a> </td>
</tr>
<tr class="odd">
<td>api</td>
<td>Manual</td>
<td><span class="badge badge-success">Active</span></td>
<td>2017-11-15 07:53:02</td>
<td>Admin</td>
<td class="button-column"><a class="view" title="View" rel="tooltip" href="/index.php/keyword/View/159"><i class="icon-eye-open"></i></a> </td>
</tr>
<tr class="even">
<td>android</td>
<td>Manual</td>
<td><span class="badge badge-important">Deleted</span></td>
<td>2017-11-09 09:43:50</td>
<td>Admin</td>
<td class="button-column"><a class="view" title="View" rel="tooltip" href="/index.php/keyword/View/158"><i class="icon-eye-open"></i></a> </td>
</tr>
</tbody>
</table>
</div>
This is the code I tried to get searched results.
driver1.findElement(By.xpath("//*[#id=\"keyword-grid\"]/table/tbody/tr[1]"));
driver1.findElement(By.xpath("//*[#id=\"keyword-grid\"]/table/tbody/tr[2]"));
System.out.print(driver1.findElement(By.xpath("//*[#id=\"keyword-grid\"]/table/tbody/tr[1]")).getText());
System.out.print(driver1.findElement(By.xpath( "//*[#id=\"keyword-grid\"]/table/tbody/tr[2]")).getText());
How can I print the results of android search?

Related

How to generate a PDF report from JSP page

I need to Generate PDF report in Java. Earlier when i use Cold Fusion it was easier to export html to PDF, is there any way to convert the html to pdf in JAVA.
I tried JSPDF javascript method to convert this page but that doesn't works.
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>View JTC</title>
<link href="assets/css/bootstrap.min.css" rel="stylesheet"
type="text/css" />
<link href="assets/css/style.css" rel="stylesheet" type="text/css" />
<%
if (session.getAttribute("empid") == null) {
response.sendRedirect("index.jsp?message=Session Expired!!");
}
session.setAttribute("cpage", "production");
%>
<jsp:include page="navigationMenu.jsp" />
</head>
<body>
<section class="container-fluid">
<table class="table table-bordered">
<tr>
<th style="width:30%;vertical-align:middle;text-align:center;">logo</th>
<th colspan="2" style="text-align:center;width:70%">Denholm Yam Contracting Comapany L.L.C</th>
</tr>
<tr>
<th rowspan="2" style="text-align:center;vertical-align:middle;">Job Travel Card</th>
<th style="width:10%;">JTC No:</th>
<th></th>
</tr>
<tr>
<th>Date</th>
<th></th>
</tr>
</table>
<table class="table table-bordered">
<tr >
<td style="width:10%;text-align:left;">Project Number</td>
<td style="width:15%;text-align:left;" colspan="2"></td>
<td style="width:10%;text-align:left;">Unit No. / Tag No.</td>
<td style="width:15%;text-align:left;" colspan="4"></td>
</tr>
<tr>
<td>Area Number</td>
<td colspan="2"></td>
<td>Material Class</td>
<td colspan="4"></td>
</tr>
<tr>
<td style="width:15%;text-align:left;">Drawing No: / ISO Number</td>
<td style="width:15%;text-align:left;"></td>
<td style="width:15%;text-align:left;">JC No:</td>
<td style="width:15%;text-align:left;"></td>
<td style="width:10%;text-align:left;">Sheet No</td>
<td style="width:15%;text-align:left;"></td>
<td style="width:10%;text-align:left;">Rev. No.</td>
<td style="width:15%;text-align:left;"></td>
</tr>
<tr>
<td style="width:15%;text-align:left;">Item Description</td>
<td style="width:15%;text-align:left;" colspan="2"></td>
<td style="width:15%;text-align:left;">ITP Stage Nos.</td>
<td style="width:15%;text-align:left;" colspan="4"></td>
</tr>
</table>
<!-- Fitup data -->
<table class="table table-bordered">
<tr>
<th rowspan="12" style="text-align:center;vertical-align:middle;">
<span class="verticaltext">
Fabrication Assembly Fit-Up
</span>
</th>
<th style="text-align:center;vertical-align:middle;" rowspan="2">Weld/joint No</th>
<th style="text-align:center;vertical-align:middle;" rowspan="2">Item/Position No.</th>
<th colspan="2">Material Spec Grade</th>
<th style="text-align:center;vertical-align:middle;" rowspan="2">Size</th>
<th style="text-align:center;vertical-align:middle;" rowspan="2">Thickness</th>
<th colspan="2">Heat/Plate/TestNo</th>
<th style="text-align:center;vertical-align:middle;" rowspan="2">Welder No. for tracking</th>
<th colspan="2">Fitup Result</th>
<th style="text-align:center;vertical-align:middle;" rowspan="2">Date</th>
<th style="text-align:center;vertical-align:middle;" rowspan="2" >Sign</th>
</tr>
<tr>
<th>Material 1</th>
<th>Material 2</th>
<th>Material 1</th>
<th>Material 2</th>
<th>Accept</th>
<th>Reject</th>
</tr>
<c:forEach begin="1" var="xx" end="10">
<tr>
<c:if test="${xx==1}">
</c:if>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</c:forEach>
<tr>
<td colspan="4">Name & Signature of fabrication foreman</td>
<td colspan="3">Released to</td>
<td colspan="2">
<input type="checkbox">
QC/NDE/DIM.
</td>
<td colspan="1">
<input type="checkbox">
Welding
</td>
<td colspan="4">
<input type="checkbox">
Fabrication/Assembly
</td>
</tr>
<tr>
<td colspan="7">Date</td>
<td colspan="7">Date</td>
</tr>
</table>
<!-- welding data -->
<table class="table table-bordered">
<tr>
<th rowspan="13" style="text-align:center;vertical-align:middle;">
<span class="verticaltext">
Welding Details
</span>
</th>
<th style="text-align:center;vertical-align:middle;" rowspan="3">Joint No</th>
<th style="text-align:center;vertical-align:middle;" rowspan="3">WPS No.</th>
<th colspan="3" class="text-center">Welding Process</th>
<th colspan="3" class="text-center">Welder/Operation No.</th>
<th colspan="2" rowspan="2" class="text-center" >Visual Inspection</th>
<th style="text-align:center;vertical-align:middle;" rowspan="3">Date</th>
<th style="text-align:center;vertical-align:middle;" rowspan="3" >Sign</th>
</tr>
<tr>
<th colspan="3" class="text-center">Passes</th>
<th colspan="3" class="text-center">Passes</th>
</tr>
<tr>
<th class="text-center">Root/Hot</th>
<th class="text-center">Fill</th>
<th class="text-center">Cap</th>
<th class="text-center">Root/Hot</th>
<th class="text-center">Fill</th>
<th class="text-center">Cap</th>
<th class="text-center">Accept</th>
<th class="text-center">Reject</th>
</tr>
<c:forEach begin="1" var="xx" end="10">
<tr>
<c:if test="${xx==1}">
</c:if>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</c:forEach>
<tr>
<td colspan="4">Name & Signature of fabrication foreman</td>
<td colspan="3">Released to</td>
<td colspan="2">
<input type="checkbox">
QC/NDE/DIM.
</td>
<td colspan="1">
<input type="checkbox">
Welding
</td>
<td colspan="4">
<input type="checkbox">
Fabrication/Assembly
</td>
</tr>
<tr>
<td colspan="14">Name & Signature of QC Personel</td>
</tr>
<tr>
<td colspan="7">Date</td>
<td colspan="7">Date</td>
</tr>
</table>
</section>
</div>
<script src="assets/jquery/customjs.js" type="text/javascript"></script>
<script src="assets/jquery/jquery-3.5.1.min.js" type="text/javascript"></script>
<script src="assets/jquery/popper.min.js" type="text/javascript"></script>
<script src="assets/jquery/bootstrap.min.js" type="text/javascript"></script>
</body>
</html>
I need to export this page in landscape and all this content scaled into one page. Is there any way? Thank you in advance.
You can use Flyingsaucer.
Flying Saucer is a pure-Java library for rendering arbitrary
well-formed XML (or XHTML) using CSS 2.1 for layout and formatting,
output to Swing panels, PDF, and images.

How to select a link of a Record in the table contains multiple Rows of different class with same class name for each column

<table class="table hover" id="resultTable">
<thead>
<tr>
<th rowspan="1" class="checkbox-col"><input type="checkbox" id="ohrmList_chkSelectAll" name="chkSelectAll" value=""></th>
<th rowspan="1" style="width:400" class="header">Report Name</th>
<th rowspan="1" style="width:95"><span class="headerCell"></span></th>
<th rowspan="1" style="width:95"><span class="headerCell"></span></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><input type="checkbox" id="ohrmList_chkSelectRecord_5" name="chkSelectRow[]" value="5"></td> <td class="left">PIM Sample Report</td>
<td class="left">Run</td>
<td class="left">Edit</td>
</tr>
<tr class="even">
<td><input type="checkbox" id="ohrmList_chkSelectRecord_6" name="chkSelectRow[]" value="6"></td> <td class="left">Sample Report</td>
<td class="left">Run</td>
<td class="left">Edit</td>
</tr>
</tbody>
</table>
I'm a beginner to selenium, so I need a help to click a link (Run)of a Record from multiple Records in the table contains multiple Rows of different class with same class name for each column. I have attached the code snippet.
I tried this Xpath but is not working:
//table[#class='table hover']/tbody/tr/td[text()='PIM Sample Report'] and ./td[text()='Run']
can someone help me to figure out this?
Try to use below XPath :
//td[.="PIM Sample Report"]/following-sibling::td[.="Run"]/a
This should fetch the link "Run" preceded by td with text "PIM Sample Report"

Unable to locate the element

I am coding in Java using Selenium Webdriver. I am unable to find the LINK Text "License Manager" in the page. When I tried switching to frame called "text". Selenium was unable to locate the frame named "text" please help.
This is my code:
d.switchTo().frame("enterpriseStatus");
d.findElement(By.id("bannerSettingsButton")).click();//settings button
d.manage().timeouts().pageLoadTimeout(15000, TimeUnit.SECONDS);
d.switchTo().window("name=text");//This is where something goes wrong
d.findElement(By.linkText("License Manager")).click();
This is the frame source:
<body>
<div class="tablecontentarea-div" style="width: 90%;">
<table class="tabletitlebar">
<tr>
<td>
<span class="titlepage">Settings</span>
</td>
</tr>
</table>
<div class="tablecontentpanel" style="padding-top: 15px;">
<table style="width:100%" summary="">
<tr>
<td style="width:50%; padding-right:7px; vertical-align: top;">
<table class="setting-section">
<tr>
<td class="img-col">
<img src="/images/servers_32x32.png"/>
</td>
<td class="desc-col">
<div class="titlesubwhitebg">Device Management</div>
<div>
Add, manage, configure and delete devices.
</div>
</td>
</tr>
<tr>
<td colspan="2">
<hr />
</td>
</tr>
<tr>
<td colspan="2">
<table style="width:100%">
<tr>
<td class="link-col-left">
<table cellpadding="0" cellspacing="0">
<tr>
<td valign="top">» </td>
<td valign="top">Getting Started</td>
</tr>
<tr>
<td valign="top">» </td>
<td valign="top">All Devices</td>
</tr>
<tr>
<td valign="top">» </td>
<td valign="top">Manage Agent Assignments</td>
</tr>
</table>
</td>
<td class="link-col-center">
<table cellpadding="0" cellspacing="0">
<tr>
<td valign="top">» </td>
<td valign="top">Policies</td>
</tr>
<tr>
<td valign="top">» </td>
<td valign="top">Device Groups</td>
</tr>
<tr>
<td valign="top">» </td>
<td valign="top">NetApp Operations Manager</td>
</tr>
<tr>
<td valign="top">» </td>
<td valign="top">Virtualization (Assign ESX Hosts)</td>
</tr>
</table>
</td>
<td class="link-col-right">
<table cellpadding="0" cellspacing="0">
<tr>
<td valign="top">» </td>
<td valign="top">Server Setup: Arrays</td>
</tr>
<tr>
<td valign="top">» </td>
<td valign="top">Server Setup: Databases</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
<table class="setting-section">
<tr>
<td class="img-col">
<img src="/images/alerting_36x36.gif"/>
</td>
<td class="desc-col">
<div class="titlesubwhitebg">Rules & Alerting</div>
<div>
Setup rules that can perform a variety of actions, including running scripts.
</div>
</td>
</tr>
<tr>
<td colspan="2">
<hr />
</td>
</tr>
<tr>
<td colspan="2">
<table style="width:100%">
<tr>
<td class="link-col-left">
<table cellpadding="0" cellspacing="0">
<tr>
<td valign="top">» </td>
<td valign="top">All Rules</td>
</tr>
<tr>
<td valign="top">» </td>
<td valign="top">Threshold Rules</td>
</tr>
<tr>
<td valign="top">» </td>
<td valign="top">Windows Event Log Rules</td>
</tr>
</table>
</td>
<td class="link-col-center">
<table cellpadding="0" cellspacing="0">
<tr>
<td valign="top">» </td>
<td valign="top">Asset Change Rules</td>
</tr>
<tr>
<td valign="top">» </td>
<td valign="top">Scheduled Rules</td>
</tr>
</table>
</td>
<td class="link-col-right">
<table cellpadding="0" cellspacing="0">
<tr>
<td valign="top">» </td>
<td valign="top">Server Setup: Real Time</td>
</tr>
<tr>
<td valign="top">» </td>
<td valign="top">Scripts</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
<table class="setting-section">
<tr>
<td class="img-col">
<img src="/images/file_36x36.gif"/>
</td>
<td class="desc-col">
<div class="titlesubwhitebg">Files</div>
<div>
Configure file analysis, file rules, assign shares to agents.
</div>
</td>
</tr>
<tr>
<td colspan="2">
<hr />
</td>
</tr>
<tr>
<td colspan="2">
<table style="width:100%">
<tr>
<td class="link-col-left">
<table cellpadding="0" cellspacing="0">
<tr>
<td valign="top">» </td>
<td valign="top">File Analysis Rules</td>
</tr>
<tr>
<td valign="top">» </td>
<td valign="top">Directory Analysis Rules</td>
</tr>
</table>
</td>
<td class="link-col-center">
<table cellpadding="0" cellspacing="0">
<tr>
<td valign="top">» </td>
<td valign="top">File Type Groups</td>
</tr>
<tr>
<td valign="top">» </td>
<td valign="top">Assign Remote Shares</td>
</tr>
<tr>
<td valign="top">» </td>
<td valign="top">Assign Local Shares</td>
</tr>
</table>
</td>
<td class="link-col-right">
<table cellpadding="0" cellspacing="0">
<tr>
<td valign="top">» </td>
<td valign="top">Server Setup: File Analysis</td>
</tr>
<tr>
<td valign="top">» </td>
<td valign="top">Discover VM Targets</td>
</tr>
<tr>
<td valign="top">» </td>
<td valign="top">File Repository</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
<table class="setting-section">
<tr>
<td class="img-col">
<img src="/images/user_accounts_36x36.gif"/>
</td>
<td class="desc-col">
<div class="titlesubwhitebg">Users</div>
<div>
Add, edit, and remove user accounts.
</div>
</td>
</tr>
<tr>
<td colspan="2">
<hr />
</td>
</tr>
<tr>
<td colspan="2">
<table style="width:100%">
<tr>
<td class="link-col-left">
<table cellpadding="0" cellspacing="0">
<tr>
<td valign="top">» </td>
<td valign="top">Manage Users</td>
</tr>
</table>
</td>
<td class="link-col-center">
<table cellpadding="0" cellspacing="0">
<tr>
<td valign="top">» </td>
<td valign="top">Server Setup: User Authentication</td>
</tr>
</table>
</td>
<td class="link-col-right">
<table cellpadding="0" cellspacing="0">
<tr>
<td valign="top"> </td>
<td valign="top"> </td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
<td style="width:50%; padding-left:8px; vertical-align: top;">
<table class="setting-section">
<tr>
<td class="img-col">
<img src="/images/server_profiler_36x36.gif"/>
</td>
<td class="desc-col">
<div class="titlesubwhitebg">Storage Manager Server</div>
<div>
Configure and view information about your server.
</div>
</td>
</tr>
<tr>
<td colspan="2">
<hr />
</td>
</tr>
<tr>
<td colspan="2">
<table style="width:100%">
<tr>
<td class="link-col-left">
<table cellpadding="0" cellspacing="0">
<tr>
<td valign="top">» </td>
<td valign="top">Server Setup: All</td>
</tr>
<tr>
<td valign="top">» </td>
<td valign="top">About</td>
</tr>
</table>
</td>
<td class="link-col-center">
<table cellpadding="0" cellspacing="0">
<tr>
<td valign="top">» </td>
<td valign="top">Server Setup: E-mail</td>
</tr>
</table>
</td>
<td class="link-col-right">
<table cellpadding="0" cellspacing="0">
<tr>
<td valign="top">» </td>
<td valign="top">Data Retention</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
<table class="setting-section">
<tr>
<td class="img-col">
<img src="/images/web_update_36x36.gif"/>
</td>
<td class="desc-col">
<div class="titlesubwhitebg">Updates & Upgrades</div>
<div>
Upload new modules and upgrade your agents and pollers.
</div>
</td>
</tr>
<tr>
<td colspan="2">
<hr />
</td>
</tr>
<tr>
<td colspan="2">
<table style="width:100%">
<tr>
<td class="link-col-left">
<table cellpadding="0" cellspacing="0">
<tr>
<td valign="top">» </td>
<td valign="top">Upload Modules</td>
</tr>
</table>
</td>
<td class="link-col-center">
<table cellpadding="0" cellspacing="0">
<tr>
<td valign="top">» </td>
<td valign="top">Upgrade Agents</td>
</tr>
</table>
</td>
<td class="link-col-right">
<table cellpadding="0" cellspacing="0">
<tr>
<td valign="top"> </td>
<td valign="top"> </td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
<table class="setting-section">
<tr>
<td class="img-col">
<img src="/images/reports_36x36.gif"/>
</td>
<td class="desc-col">
<div class="titlesubwhitebg">GUI & Reporter</div>
<div>
Configure GUI and Reporting behavior.
</div>
</td>
</tr>
<tr>
<td colspan="2">
<hr />
</td>
</tr>
<tr>
<td colspan="2">
<table style="width:100%">
<tr>
<td class="link-col-left">
<table cellpadding="0" cellspacing="0">
<tr>
<td valign="top">» </td>
<td valign="top">Server Setup: Reporter</td>
</tr>
</table>
</td>
<td class="link-col-center">
<table cellpadding="0" cellspacing="0">
<tr>
<td valign="top">» </td>
<td valign="top">Server Setup: GUI</td>
</tr>
</table>
</td>
<td class="link-col-right">
<table cellpadding="0" cellspacing="0">
<tr>
<td valign="top"> </td>
<td valign="top"> </td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
<table class="setting-section">
<tr>
<td class="img-col">
<img src="/images/license_36x36.gif"/>
</td>
<td class="desc-col">
<div class="titlesubwhitebg">License Summary</div>
<div>
View your license type and number of storage modifiers.
</div>
</td>
</tr>
<tr>
<td colspan="2">
<hr />
</td>
</tr>
<tr>
<td colspan="2">
<table style="width:100%">
<tr>
<td class="link-col-left">
<table cellpadding="0" cellspacing="0">
<tr>
<td valign="top">» </td>
<td valign="top">License Manager</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
<table class="setting-section">
<tr>
<td class="img-col">
<img src="/images/Icon.Thwack.gif"/>
</td>
<td class="desc-col">
<div class="titlesubwhitebg">thwack Community</div>
<div>
Import shared application templates posted on thwack, a community site for Solarwinds users. thwack features helpful devices, tips and downloads from over 20,000 network engineers.
</div>
</td>
</tr>
<tr>
<td colspan="2">
<hr />
</td>
</tr>
<tr>
<td colspan="2">
<table style="width:100%">
<tr>
<td class="link-col-left">
<table cellpadding="0" cellspacing="0">
<tr>
<td valign="top">» </td>
<td valign="top">Storage thwack Forum</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</div>
</body>
To switch to window you have to use WindowsHandle.
You may try using below function provided that you are handeling windows with different titles.
private String mainWindowsHandle; // Stores current window handle
public static boolean swithToWindow(WebDriver driver,String title){
mainWindowsHandle = driver.getWindowHandle();
Set<String> handles = driver.getWindowHandles(); // Gets all the available windows
for(String handle : handles)
{
driver.switchTo().window(handle); // switching back to each window in loop
if(driver.getTitle().equals(title)) // Compare title and if title matches stop loop and return true
return true; // We switched to window, so stop the loop and come out of funcation with positive response
}
driver.switchTo().window(mainWindowsHandle); // Switch back to original window handle
return false; // Return false as failed to find window with given title.
}

Clicking element in table based on condition on another element in the same row

I have the following example structure for a table I'm looking at:
<tbody>
<tr class="OddRow">
<td class="TimeField">
7:20 AM
</td>
<td class="TextField"></td>
<td id="Price_2_1" class="MoneyField"></td>
<td class="LinkField">
<a id="basketControl_2_1" class="sr_AddToBasket">
Add to Basket
</a>
</td>
</tr>
<tr class="EvenRow">
<td class="TimeField"></td>
<td class="TextField"></td>
<td id="Price_2_2" class="MoneyField"></td>
<td class="LinkField"></td>
</tr>
...OddRow
...EvenRow
</tbody>
What I would like to be able to do is click (using selenium) on the element with class "sr_AddToBasket" when the value in class "TimeField" is something I specify. Only one row of the table can ever have the specified time in a particular instance.
I'm really stuck as to how to go about this so any help will be greatly appreciated! If it helps I'm currently attempting this in python, but have some knowledge of java.
Suppose your structure looks like below after you add two more rows:
<table>
<tbody>
<tr class="OddRow">
<td class="TimeField">
7:20 AM
</td>
<td class="TextField"></td>
<td id="Price_2_1" class="MoneyField"></td>
<td class="LinkField">
<a id="basketControl_2_1" class="sr_AddToBasket">
Add to Basket
</a>
</td>
</tr>
<tr class="EvenRow">
<td class="TimeField"></td>
<td class="TextField"></td>
<td id="Price_2_2" class="MoneyField"></td>
<td class="LinkField"></td>
</tr>
<tr class="OddRow">
<td class="TimeField"></td>
<td class="TextField"></td>
<td id="Price_2_1" class="MoneyField"></td>
<td class="LinkField"></td>
</tr>
<tr class="EvenRow">
<td class="TimeField">
9:00 PM
</td>
<td class="TextField"></td>
<td id="Price_2_2" class="MoneyField"></td>
<td class="LinkField">
<a id="basketControl_2_1" class="sr_AddToBasket">
Add to Basket
</a>
</td>
</tr>
</tbody>
</table>
So, now say you want to click on the 2nd link, then please use the below Java code:
driver.findElement(By.xpath("//td[#class='TimeField' and contains(text(),'9:00 PM')]/following-sibling::td/a[#class='sr_AddToBasket']")).click();
You can replace the '9:00 PM' above with your value.
if driver.find_elements_by_css_xpath("//tr[#class='EvenRow']/td[#class='TimeField']").get_attribute("value")=="your value":
driver.find_element_by_xpath("//a[#class='sr_AddToBasket']").click()
this get_attribute should return you the value you set and simple if condition matches the criteria of execution. Then next you find the link you want and click. I used xpath since it's helps us to identify element in a table easily.
you can use the following:
This question were answered here http://patch-recepteur.blogspot.com/2017/04/forum-clicking-element-in-table-based.html
<table>
<tbody>
<tr class="OddRow">
<td class="TimeField">
7:20 AM
</td>
<td class="TextField"></td>
<td id="Price_2_1" class="MoneyField"></td>
<td class="LinkField">
<a id="basketControl_2_1" class="sr_AddToBasket">
Add to Basket
</a>
</td>
</tr>
<tr class="EvenRow">
<td class="TimeField"></td>
<td class="TextField"></td>
<td id="Price_2_2" class="MoneyField"></td>
<td class="LinkField"></td>
</tr>
<tr class="OddRow">
<td class="TimeField"></td>
<td class="TextField"></td>
<td id="Price_2_1" class="MoneyField"></td>
<td class="LinkField"></td>
</tr>
<tr class="EvenRow">
<td class="TimeField">
9:00 PM
</td>
<td class="TextField"></td>
<td id="Price_2_2" class="MoneyField"></td>
<td class="LinkField">
<a id="basketControl_2_1" class="sr_AddToBasket">
Add to Basket
</a>
</td>
</tr>
</tbody>
</table>

Java based selenium webdriver - click on element in dynamically changing table content by row text ( click input by text in same row )

Im stucked in java based selenium usage, i like to click on input box (within table )by another cell text value
Here is my HTML code:
<DIV class=>
<P class=></P>
<UL></UL>
<FORM id="" onsubmit="" method=post name="" action="">
<TABLE width="100%">
<TBODY>
<TR>
<TD class=></TD></TR></TBODY></TABLE>
<TABLE>
<TBODY>
<TR>
<TD class=></TD></TR>
<TR>
<TD>
<TABLE class="" width="70%">
<TBODY>
<TR class="">
<TD></TD>
<TD></TD>
<TD></TD>
<TD></TD></TR>
<TR class="">
<TD><INPUT class="" onclick="" CHECKED type=checkbox><INPUT id= value=true type=hidden name=""> </TD>
<TD></TD>
<TD></TD>
<TD></TD></TR></TBODY></TABLE></TD>
<TR>
<TD><INPUT id="" class= onclick='' value="" type=button name=></TD></TR></TR></TBODY></TABLE>
<TABLE width="100%">
<TBODY>
<TR>
<TD class=""></TD></TR>
<TR>
<TD>
<TABLE class=foo width="100%">
<TBODY>
<TR class=foo-header>
<TD></TD>
<TD></TD>
<TD></TD>
<TD></TD>
<TD></TD>
<TD></TD>
<TD></TD>
<TD></TD>
<TD></TD></TR>
<TR class="foo-odd">
<TD><INPUT onclick="" value="0" type="radio" name=""> </TD>
<TD></TD>
<TD></TD>
<TD></TD>
<TD></TD>
<TD></TD>
<TD></TD>
<TD></TD>
<TD></TD></TR>
<TR class="foo-even">
<TD rowSpan="3"><INPUT onclick="" value="1" CHECKED type="radio" name=""> </TD>
<TD rowSpan="3"></TD>
<TD rowSpan="3"></TD>
<TD rowSpan="3"></TD>
<TD rowSpan="3"></TD>
<TD rowSpan="3"></TD>
<TD rowSpan="3"></TD>
<TD></TD>
<TD rowSpan="3"><INPUT id="" class="" value="" type="" name=""></TD></TR>
<TR class="foo-even">
<TD></TD></TR>
<TR class="foo">
<TD>CCC</TD></TR></TBODY></TABLE></TD></TR>
<TR>
<TD><INPUT class="" onclick="" type=checkbox><INPUT id="" value="false" type="hidden" name=""> </TD></TR></TBODY></TABLE>
<TABLE width="100%">
<TBODY>
<TR>
<TD class=""></TD></TR>
<TR>
<TD>
<TABLE class="foo" width="100%">
<TBODY>
<TR class="foo-header">
<TD></TD>
<TD><BR><INPUT id="" class="" onclick="" value="" type="button" name=""></TD>
<TD colSpan="3"></TD>
<TD></TD></TR>
<TR class="foo-odd">
<TD><INPUT value="0" type="radio" name=""> </TD>
<TD></TD>
<TD></TD>
<TD colSpan="2"></TD>
<TD><SELECT class="" name=""> <OPTION value="0"></OPTION> <OPTION selected value="1"></OPTION></SELECT> </TD></TR>
<TR class="foo-even">
<TD rowSpan="2"><INPUT value="1" type="radio" name=""> </TD>
<TD rowSpan="2"></TD>
<TD rowSpan="2"></TD>
<TD></TD>
<TD></TD>
<TD rowSpan="2"><SELECT class="" name=""> <OPTION value="0"></OPTION> <OPTION selected value="1"></OPTION></SELECT> </TD></TR>
<TR class="foo">
<TD>CCC</TD>
<TD></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE>
<TABLE width="100%">
<TBODY></TBODY></TABLE>
<TABLE width="100%">
<TBODY>
</DIV>
I like to click on the second CCC text row related input,(not by only clicking name or xpath) im tried with followings:
driver.findElement(By.xpath(".//td[contains(text(),'CCC')]/preceding-sibling::td")).click();
Code above working in a *very similar html only contains CCC one time (tested with another HTML code )
WebElement services = driver.findElement(By.xpath(".//*[#id='installationAddressForm']/table[4]/tbody/tr[2]/td/table/tbody/tr[4]/td[1][contains(text(),'CCC')]"));
String contents = services.getText();
System.out.println(contents);
Code above able to find text but when combined with the top code(/preceding-sibling::td) it cannot find the input
Please help.Thanks in advance
The expression td[1][contains(text(),'CCC')] selects the first td and tests whether its text contains CCC.
Replace it with td[contains(text(),'CCC')][1].

Categories