how to find search results using selenium webdriver - java

I am setting up a test to perform a search and after the search is complete, i want to capture the results line that says "About xxx results (x.xx seconds)". I can get this to work using the firefox webdriver, but it does not work when I use IE or Chrome driver. I need help determining what is wrong with my code. Here is the code snippet:
public void runSearch(WebDriver driver) {
WebElement element = driver.findElement(By.name("search"));
element.sendKeys("Kearney");
element.sendKeys(Keys.RETURN);
String itext = driver.findElement( By.cssSelector("div#resInfo-0")).getText();
System.out.println("Search returned '" + itext + "'.");
}
Here is the html of my page after the search has been made:
<head></head>
<body>
<header id="main_header_iphone"></header>
<div class="clear"></div>
<header id="main-header"></header>
<section>
<div class="content_nn">
<div class="subnav">
<!--
<div class="bannerBox">
<div class="btn_events"><…
-->
<div class="logo"></div>
<!--
START Search
-->
<div class="search_container">
<div id="cse" style="width:100%;">
<div class="gsc-control-cse gsc-control-cse-en">
<div class="gsc-control-wrapper-cse" dir="ltr">
<form class="gsc-search-box" accept-charset="utf-8"></form>
<div class="gsc-results-wrapper-nooverlay gsc-results-wrapper-visible">
<div class="gsc-tabsAreaInvisible"></div>
<div class="gsc-tabsAreaInvisible"></div>
<div class="gsc-above-wrapper-area">
<table class="gsc-above-wrapper-area-container" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="gsc-result-info-container">
<div id="resInfo-0" class="gsc-result-info">
About 31 results (0.11 seconds)
</div>
</td>
</tr>
</tbody>
</table>
</div>
<div class="gsc-adBlockNoHeight" style="height: 0px; font-weight: normal; text-align: center;"></div>
<div class="gsc-wrapper"></div>
</div>
</div>
</div>
</div>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript"></script>
<script type="text/javascript" src="http://www.google.com/uds/?file=search&v=1&hl=en"></script>
<link rel="stylesheet" type="text/css" href="http://www.google.com/uds/api/search/1.0/65b21018ad4df09e3eb5a21326b72d0b/default+en.css"></link>
<script type="text/javascript" src="http://www.google.com/uds/api/search/1.0/65b21018ad4df09e3eb5a21326b72d0b/default+en.I.js"></script>
<!--
END Search
-->
</div>
<div class="text_rural_h2" style="float:left; width:100%"></div>
<div class="index_page_heading" style="float:left;"></div>
<div class="letest_news2" style="float:left;"></div>
</div>
<footer id="footer"></footer>
</div>
</section>
<div id="topcontrol" style="position: fixed; bottom: 25px; right: 10px; opacity: 0; cursor: pointer;" title="Scroll Back to Top"></div>
<table class="gstl_50 gssb_c" cellspacing="0" cellpadding="0" style="width: 137px; display: none; top: 153px; position: absolute; left: 97px;"></table>
<div style="display:none"></div>
</body>

It is possible, the search result page takes more time for loading than WebDriver expects.
I propose to wait until the next page is loaded with WebDriverWait methods.
element.sendKeys("Kearney");
element.sendKeys(Keys.RETURN);
// Wait!
WebDriverWait wait = new WebDriverWait(driver, 20);
By waitFor = By.cssSelector("div#resInfo-0");
WebElement lblSearchResults = wait.until(ExpectedConditions.ElementIsVisible(waitFor));
//
String itext = lblSearchResults.getText();
Please see also this topic - WebDriver: Advanced Usage

For IE and Chrome you need an extra driver. Start with the chrome driver and check if this solves you problem.

Related

I reference my CSS page to my HTML and it simply won't have any effect

I am currently working on a java project and have made an html page with a design (css) page to accompany it. I have referenced it but it still won't have any effect. I hard reloaded the page still no changes. Don't really understand why this is happening. I keep getting "net::ERR_ABORTED 404" on localhost. The stylesheet file name is indeed correct (being loginStyle.css). They are also both located in the same folder. Would appreciate any help. Thanks!
.btn-color {
background-color: #0e1c36;
color: #fff;
}
.profile-image-pic {
height: 200px;
width: 200px;
object-fit: cover;
}
.cardbody-color {
background-color: #ebf2fa;
}
<!DOCTYPE html>
<html lang="en" xmlns:thymeleaf.org>
<head>
<link rel="stylesheet" type="text/css" href="loginStyle.css" />
</head>
<div class="container">
<div class="row">
<div class="col-md-6 offset-md-3">
<h2 class="text-center text-dark mt-5">Welcome</h2>
<div class="card my-5">
<form class="card-body cardbody-color p-lg-5" th:action="#{/userLogin}" th:object="${user}" method="post">
<div class="text-center">
<img src="https://cdn.pixabay.com/photo/2016/03/31/19/56/avatar-1295397__340.png" class="img-fluid profile-image-pic img-thumbnail rounded-circle my-3" width="200px" alt="profile">
</div>
<div class="mb-3">
<input type="text" class="form-control" id="Username" aria-describedby="emailHelp" placeholder="User Name" th:field="*{Id}">
</div>
<div class="mb-3">
<input type="password" class="form-control" id="password" placeholder="password" th:field="*{password}">
</div>
<div class="text-center"><button type="submit" class="btn btn-color px-5 mb-5 w-100">Login</button></div>
<div id="emailHelp" class="form-text text-center mb-5 text-dark">Not Registered?
<a href="#" class="text-dark fw-bold"> Create an Account
</a>
</div>
</form>
</div>
</div>
</div>
</div>
</html>

How to add pdf inside a thymeleaf pdf using flying saucer

I am using java, thymeleaf and flying saucer to generate pdf. I need to add another pdf (by fetching from Aws S3).
I tried iFrame and Embed but it is not working, I am not getting the second pdf in my generated pdf file!
Please help me in finding some way to add the pdf inside of pdf using thymeleaf;
My code-
<body>
<div class="header">
<div style="float: right; color: #000971">
Logo</div>
</div>
<div style="">
<div style="text-align: center; padding-top: 15px; "><h1 style="color:#000971;"><span th:text="${Name}"></span></h1></div>
<div style="padding-top: 30px; margin-left: 50px">
<h1 style="color:darkcyan;"><u>Report:</u></h1>
<h1 style="color: darkcyan; margin-top: 250px">Report By: <br />
<span style="color:#000971;" th:text="${firstName}"></span> <span style="color:#000971;" th:text="${lastName}"></span></h1>
<!-- <h1 style="color: #006382;">Last Name: <span style="color:#6FB8D9;" th:text="${lastName}"></span></h1>-->
<br/>
<div id="lastContent" style="margin-top: 80px">
<h2 style="color: darkcyan">REPORT DATE:</h2>
<h2 style="color: #000971">Created Date: <span th:text="${createdTime}"></span></h2>
<h2 style="color: #000971">Last Updated Date: <span th:text="${lastUpdated}"></span></h2>
</div>
</div>
</div>
<div>
<iframe src="http://www.wright.edu/~david.wilson/eng3000/samplereport.pdf" style="width:100%;height:700px;"></iframe>
<embed src="./samplereport.pdf" width="600" height="500" alt="pdf" pluginspage="http://www.adobe.com/products/acrobat/readstep2.html" />
</div>
</body>

Expand a menu inside frame with selenium java

I am trying to expand the option "Applications":
And this is its html:
What I have done until now:
// Wait until presence of navigation frame
element = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("/html/frameset/frameset/frame[1]")));
// switch to navigation frame
driver.switchTo().frame(driver.findElement(By.name("navigation")));
// Wait until presence of Aplications option
element = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div/div/table/tbody/tr[2]/td/div[5]/a")));
// Click to expand Aplicactions options
element.click();
When it step into click I can see it is positioned over the option of Aplications because it is showing the tooltip / title "Aplications" like if we put the mouse over the option and the href of this option downside left javascript:expandCollapse('2');
I know the code is running until here because the other piece of code for login is working.
What I am not sure is the switch to frame because I have looked up for frameset and it shows that we can switch directly to the frame.
I am new, I apologize if this is messy. If something is missing to find the error, tell me to edit the post.
Thanks in advance.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="" rel="styleSheet" type="text/css">
<title></title>
</head>
<frameset name="ISCmain" rows="35,*" frameborder="1" border="1" resize="yes">
<frame title="Header mark message" src="" name="header"
noresize="" scrolling="no" marginwidth="0" marginheight="0">
<frameset cols="25%,*" resize="yes">
<frame title="Navigation mark" src="" name="navigation"
style="border-right: 1px solid #d7d7d7;" resize="yes" marginwidth="10" marginheight="10">
<html>
<head>
</head>
<body style="direction:ltr" marginwidth="10" marginheight="10" onload="initAll();" class="navtree"
leftmargin="0" topmargin="0">
<div style="direction:ltr">
<link href="" rel="styleSheet" type="text/css">
<script language="javascript">
</script>
<div class="navBody" style="background-color:#FFFFFF;">
<table height="26" width="100%" cellspacing="0" cellpadding="0"
style="background-color:#FFFFFF;">
<tbody>
<tr height="26" style="border-left: 1px solid #3970B1;background-color:#FFFFFF">
<td class="wpsGpFilter" valign="center" align="left" width="100%"
style="background-image:url(/ibm/console/images/isclite/grpfilter_background.gif);background-repeat: repeat-x;">
<select name="navFilterSelection" id="navFilterSelection" dir="ltr"
onchange="determineAction();">
<option value="allTasks" selected="" label=""></option>
<option value="com.ibm.isclite.MyTaskFilter" label=""></option>
<option value="PF+com.ibm.websphere.product" label="">
</option>
</select>
<noscript>
<input type="submit" title='' value=''>
</noscript>
</td>
</tr>
<tr>
<td align="left" colspan="2" class="navtree" style="background-color:#FFFFFF">
<ul class="nav-child" dir="ltr">
<li class="navigation-bullet"><a style="text-decoration:none"
href="/ibm/console/navigation.do?wpageid=com.ibm.isclite.welcomeportlet.layoutElement.A&moduleRef=com.ibm.isclite.ISCAdminPortlet"
target="detail" dir="ltr" title=""></a></li>
</ul>
<div nowrap="" class="main-task" style="margin-left:0.3em;"><a
style="color:#000000;text-decoration:none;"
href="javascript:expandCollapse('0');" title=""><img
src="/ibm/console/images/arrow_collapsed.gif" title="Expand"
alt="Expand" id="I0" border="0" align="absmiddle"></a></div>
<div class="nav-child-container" style="margin-left: 0.3em; display: none;"
id="N0">
<ul class="nav-child" dir="ltr">
<li class="navigation-bullet"><a style="text-decoration:none"
href="/ibm/console/navigatorCmd.do?forwardName=guidedactivity.resource&WSC=true"
target="detail" dir="ltr" title=""></a></li>
</ul>
<ul class="nav-child" dir="ltr">
<li class="navigation-bullet"><a style="text-decoration:none"
href="/ibm/console/navigatorCmd.do?forwardName=guidedactivity.web&WSC=true"
target="detail" dir="ltr" title=""></a></li>
</ul>
</div>
<div nowrap="" class="main-task" style="margin-left:0.3em;"><a
style="color:#000000;text-decoration:none;"
href="javascript:expandCollapse('1');" title=""><img
src="/ibm/console/images/arrow_collapsed.gif" title="Expand"
alt="Expand" id="I1" border="0" align="absmiddle"></a></div>
<div class="nav-child-container" style="margin-left: 0.3em; display: none;"
id="N1">
<ul class="nav-child" dir="ltr">
<li class="navigation-bullet"><a style="text-decoration:none"
href="/ibm/console/navigatorCmd.do?forwardName=ApplicationServer.content.main&WSC=true"
target="detail" dir="ltr" title=""></a></li>
</ul>
<ul class="nav-child" dir="ltr">
<li class="navigation-bullet"><a style="text-decoration:none"
href="/ibm/console/navigatorCmd.do?forwardName=WebServer.content.main&WSC=true"
target="detail" dir="ltr" title=""></a></li>
</ul>
<ul class="nav-child" dir="ltr">
<li class="navigation-bullet"><a style="text-decoration:none"
href="/ibm/console/com.ibm.ws.console.sib.sibresources.forwardCmd.do?forwardName=SIBMQServer.content.main&WSC=true"
target="detail" dir="ltr" title=""></a>
</li>
</ul>
</div>
<div nowrap="" class="main-task" style="margin-left:0.3em;"><a
style="color:#000000;text-decoration:none;"
href="javascript:expandCollapse('2');" title="Aplications"><img
src="/ibm/console/images/arrow_expanded.gif" title="Expand"
alt="Expand" id="I2" border="0"
align="absmiddle">Aplications</a></div>
<div class="nav-child-container" style="margin-left: 0.3em; display: block;"
id="N2">
<ul class="nav-child" dir="ltr">
<li class="navigation-bullet"><a style="text-decoration:none"
href="/ibm/console/navigatorCmd.do?forwardName=ApplicationDeployment.content.main&WSC=true"
target="detail" dir="ltr" title=""></a>
</li>
</ul>
<ul class="nav-child" dir="ltr">
<li class="navigation-bullet"><a style="text-decoration:none"
href="/ibm/console/com.ibm.ws.console.appmanagement.action.forwardCmd.do?forwardName=appmanagement.upload&WSC=true"
target="detail" dir="ltr" title=""></a></li>
</ul>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>
<frame title="Content Mark"
src="/ibm/console/navigation.do?wpageid=com.ibm.isclite.welcomeportlet.layoutElement.A&moduleRef=com.ibm.isclite.ISCAdminPortlet"
name="detail" resize="yes" marginwidth="0" marginheight="0">
</frameset>
</frameset>
<noframes>
...
</noframes>
</html>
Whenever you encounter frameset remember you don't need to switch over it to let selenium interact with inside element.
Instead do switch to frame itself (Implementing WebDriverWait for more stability):
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.name("navigation")));
and finally your desired element you can pick :
element = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div/div/table/tbody/tr[2]/td/div[5]/a")));
// Click to expand Aplicactions options
element.click();
It not a recommendation to use Absolute xpath switch to relative xpath.

Unable to locate webElement by LinkText using selenium webdriver?

Tried every possible way but could not locate the WebElement of WebTable.
Please find the below HTML excerpt and help me finding the WebElement "Project Costing " :
<!DOCTYPE html>
<html class="firefox win pc standard" lang="en" dir="ltr">
<head>
<body class="PSPAGE" onload="loadAllPgltData('portlets',1465250643644);">
<script type="text/javascript">
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr>
<tr>
<td>
<table id="ptpglts" width="100%" summary="">
<tbody>
<tr>
<td width="33%" valign="top">
<ul id="ptcol1" class="ptpgltdroppable">
<li id="ptpgltli_MENU" class="pthpli ">
<table id="MENU" class="PTPAGELET" width="100%" cellspacing="0" cellpadding="0" border="0" summary="">
<tbody>
<tr>
<tr id="ptpgltbody_row_MENU">
<td id="ptpgltbody_MENU" class="PTPAGELETBODY" width="100%">
<div id="MENU_Data" class="ptprtlcontainer">
<script id="ptPgltReloadThis" type="text/javascript">
<link type="text/css" href="https://pacefin.techmahindra.com/cs/PACEFIN/cache/PSSTYLEREQ_1.css;wad40e4d39521f1256" rel="stylesheet">
<link type="text/css" href="https://pacefin.techmahindra.com/cs/PACEFIN/cache/PSSTYLEDEF_TANGERINE_4.css;wa90f8b654ed2a321b" rel="stylesheet">
<script type="text/javascript">
<script src="https://pacefin.techmahindra.com/cs/PACEFIN/cache/PT_COMMON_MIN_1.js;waaedd9d526f2073e4" type="text/javascript">
<script src="https://pacefin.techmahindra.com/cs/PACEFIN/cache/PT_AJAX_NET_MIN_1.js;waf370f1177a8f9947" type="text/javascript">
<nav id="ptnav2pglt" aria-label="Menu">
<div id="ptnav2srch">
<div id="ptnav2pgltbody">
<ul id="ptnav2tree">
<li id="MYFAVORITES" class="ptnav2fldr" title="Create a list of frequently used transactions.">
<li id="MANAGE_QUOTE_SYSTEM" class="ptnav2fldr" title="Manage Quotes">
<li id="CO_EMPLOYEE_SELF_SERVICE" class="ptnav2fldr" title="">
<li id="CO_MANAGER_SELF_SERVICE2" class="ptnav2fldr" title="">
<li id="EPCO_CUSTOMERS" class="ptnav2fldr" title="Manage customer information.">
<li id="EPCO_CUSTOMER_CONTRACTS" class="ptnav2fldr" title="Access customer contracts.">
<li id="EPCO_ITEMS" class="ptnav2fldr" title="Manage all item-related options.">
<li id="EPPO_PURCHASING" class="ptnav2fldr" title="Manage requisitions, purchase orders, receipts, GPO contracts, and related data.">
<li id="EPCO_PROGRAM_MANAGEMENT" class="ptnav2fldr" title="Use program management tools to review program progress.">
<li id="EPCO_PROJECTS" class="ptnav2fldr" title="Access project costing.">
<div class="ptnav2toggle"> </div>
<a id="fldra_EPCO_PROJECTS" class="ptntop" href="https://pacefin.techmahindra.com/psp/PACEFIN/EMPLOYEE/ERP/s/WEBLIB_PTPP_SC.HOMEPAGE.FieldFormula.IScript_AppHP?pt_fname=EPCO_PROJECTS&FolderPath=PORTAL_ROOT_OBJECT.EPCO_PROJECTS&IsFolder=true">Project Costing</a>
For any further information regarding above Please ask.
Try below mentioned xpaths
option 1. //a[text()='Project Costing']
option 2. //*[#id='fldra_EPCO_PROJECTS'] in case this ID value is duplicate then try concatenating it with other attribute like this //*[#id='fldra_EPCO_PROJECTS'][#class='ptntop']
option 3. Or start with the table name //table[#id='ptpglts']/a
In case nothing works then try using the xpath sibling methods
option 4. Use the element just above a step above to your element //*[#class='ptnav2toggle']/following-sibling::a

How to select a text from table and click with selenium

In order page when I make a payment, I get the created payment listed in payments list page. The created payment will always list on the first row of the table. I want to know how to get a text “This is Project:
“ and also be able to click on it. Every payment has its own id in a row i.e. Here is the snipped of the table:
<div id="PAYMENT-CONTAINER" style="clear:both">
<div class="list-widget" id="PAYMENT-LIST">
<ul style="visibility: visible;" class="table">
<li id="393118">
<div class="no-expand col0" style="text-align: center; width: 31px;"><input type="checkbox" class="rowSelect" value="393118"></div>
<div class="no-expand col1" style="text-align: center; width: 57px;">
<span class="tag untagged">•••</span>
</div>
<div class="col2" style="text-align: center; width: 155px;">
07/28/2015
</div>
<div class="col3" style="width: 401px;">
5280 FAST PITCH
</div>
<div class="col4" style="width: 344px;">
This is Project:
</div>
<div class="col5" style="text-align: right; width: 213px;">
$25.00
</div>
<div class="col6" style="width: 188px;">
Stacey Smith
</div>
<div class="col7" style="width: 178px;">
In Process
</div>
</li>
<li id="393119">
<div class="no-expand col0" style="text-align: center; width: 31px;"><input type="checkbox" class="rowSelect" value="393119"></div>
<div class="no-expand col1" style="text-align: center; width: 57px;">
<span class="tag untagged">•••</span>
</div>
<div class="col2" style="text-align: center; width: 155px;">
07/28/2015
</div>
<div class="col3" style="width: 401px;">
5280 FAST PITCH
</div>
<div class="col4" style="width: 344px;">
Donations are for good cause
</div>
<div class="col5" style="text-align: right; width: 213px;">
$26.00
</div>
<div class="col6" style="width: 188px;">
Stacey Smith
</div>
<div class="col7" style="width: 178px;">
In Process
</div>
</li>
You can get the text WebElement by using below xpath:
//div[#class='col4']
or
//div[contains(text(),'This is project');
you can use any one of the xpath to find web element.
Something like this:
WebElement element = driver.findElement(By.xpath("//div[#class='col4']"));
//Print text
print(element.getText());
//Click on that element if it is clickable.
element.click();
Xpath selector for your case is: //div[contains(text(), 'This is Project:')]
. For example, if instance of webdriver is called like "driver", then code should be:
driver.findElement(By.xpath("//div[contains(text(), 'This is Project:')]")).click();
If your required text "This is Project:" always appears in first row, then you can try below xpath:-
//li[1]/div[#class='col4']
//to get text:-
driver.findElement(By.xpath("//li[1]/div[#class='col4']")).getText();
//to click on it:-
driver.findElement(By.xpath("//li[1]/div[#class='col4']")).click();

Categories