Getting Error while identifying an element in Selenium WebDriver - selenium

I have to select the above highlighted link New V1 Project but I am not able to identify the element.
I've tried using linktext function but it is giving error... can anybody help?
Hierachy is something like:-
<tbody>
<tr>
<td width="40" align="center">
<td width="5">
<td align="center" noWrap="" style="border-bottom-color: black; border-bottom-width: 1px; border-bottom-style: solid;" bgColor="#cccccc">
<a class="navSelected" onmouseover="window.status=''; return true" onmouseout="window.status=''; return true" href="index.cfm?fuseaction=project.view&version=1">
<td width="5">
<td width="5">
<td align="center" noWrap="" style="border-bottom-color: black; border-bottom-width: 1px; border-bottom-style: solid;" bgColor="#cccccc">
<a class="nav" onmouseover="window.status=''; return true" onmouseout="window.status=''; return true" href="index.cfm?fuseaction=project.create_form&version=1">
<td width="5">
<td width="5">
<td align="center" noWrap="" style="border-bottom-color: black; border-bottom-width: 1px; border-bottom-style: solid;" bgColor="#cccccc">
<a class="nav" onmouseover="window.status=''; return true" onmouseout="window.status=''; return true" href="index.cfm?fuseaction=project.create_form&version=2">
</tr>
</tbody>
please see the highlighted HTML code in image... I am not sure how this Text element is linked with a tag
My Code that I've used to identify element is:
WebElement v1Link = driver.findElement(By.linktext("New V1 Project"));
WebElement v1Link = driver.findElement(By.xpath("//tr/td[3]/a[#text()= 'New v1 Project']"));
but none of the above 2 methods are working... please somebody help.

You can find the link by the following xpath:
//td/a[normalize-space(text()) = 'New V1 Project']
FYI, your version of xpath was almost correct:
no need to put # before the text()
the text is New V1 Project, not New v1 Project
you need to use normalize-space()
Hope that helps.

Try using cssselector-
WebElement v1Link = driver.findElement(By.cssSelector("a.nav[href='index.cfm?fuseaction=project.create_form&version=2']"));

Related

LINQ-to-XML in HtmlAgilityPack to select TR

This may straightforward for someone, so if you are that one please take a look at this:
I have a table with more rows, but one row I want to extract based on a given text, here is the table excerpt:
... <tr>
<td class="border-1px-solid-black" colspan="1">
<img onclick="PopupCenter('{{InvoiceDetails.0.Link}}')" style="max-height: 35px; max-width: 35px; cursor: pointer;" src="/images/report.png" /></td>
<td class="border-1px-solid-black" colspan="11">
<p>{{InvoiceDetails.0.Description}}</p>
</td>
<td class="border-1px-solid-black" colspan="2" style="text-align: right;">
<p>{{InvoiceDetails.0.Quantity}}</p>
</td>
<td class="border-1px-solid-black" colspan="2" style="text-align: right;">
<p>{{InvoiceDetails.0.Amount}} </p>
</td>
</tr>...
I am using HtmlAgilityPack and VB.NET and I want to get TR that contains InvoiceDetails.0 in any element.
How to do it?
Thanks!
I will post the way I've finished this, but this is not the shortest way:
nodes = doc.DocumentNode.SelectNodes("//*[text()[contains(., '" + find + "')]]")
If nodes IsNot Nothing Then
node = nodes(0)
While node.ParentNode.Name <> "tr"
node = node.ParentNode
End While
node = node.ParentNode
'and here I have reference to TR node
Next
End If
If you have shorter way please write here.

How to locate the parent element of a child element identified through innerText?

I need to select a row in a table where I have a text, hence I would like to leverage the option of selecting a text and then eventually selecting the parent
Now the page looks like:
<tr class=" tableRow1" id="_pu5ufb" dr="1" _awtisprimaryrow="1">
<td width="1" class="tableBody w-tbl-cell" align="center"><span
class="selectColumnMarker">
<div class="w-chk-container">
<input bh="CHKINP" hasaction="false" class="w-chk-native"
id="_m7iynb" value="1" type="checkbox" elementid="_jw4lmb"
issender="false" awnomitcomponent="true" name="_jw4lmb"><label
bh="CHK" class="w-chk w-chk-dsize"></label>
</div>
</span>
</td>
<td align="left" class="tableBody w-tbl-cell">
<span>
<table role="presentation" class="mls" cellpadding="0"
cellspacing="0">
<tbody><tr>
<td class="" id="_tz87e" tabindex="0"><a id="_3iqrbb" href="#"
bh="HL" _sf="true">Analyst</a>
</td>
</tr>
</tbody></table>
</span>
</td><td class="tableBody w-tbl-cell">
</td>
<td class="tableBody w-tbl-cell">
</td>
</tr>
I need to find the text Analyst and then find the associated <tr> class and select the <tr> class.
Any help would be highly appreciable
First, whenever you have mixed content (text and markup) it is better to compare elements' string value than text nodes because inline markup might be splitting the compared text into different text nodes.
Second, you can use:
//tr[td='Analyst']/#class
Note: node-set comparison is an existencial comparison. It means that you are asking if there is some node (some td element in this case) with string value equal to 'Analyst'.
Of course, in HTML there are elements for which white space is not significant for rendering (it's not preserved) despite its presence in the source document. In that case you can use this simple XPath 1.0 expression:
//tr[td[normalize-space()='Analyst']]/#class
Do note: a node-set has a false boolean value if and only if it's empty; you can "nest" predicates (properly, a predicate can be any XPath expression).
I need to find the text Analyst and then find the associated tr class and select the tr class.
XPath 2.01
This XPath,
//tr[td/normalize-space() = "Analyst"]/#class
will select all #class attributes of tr elements containing a td with a space-normalized string value of "Analyst".
Do note, however, that in your sample HTML, such a tr has no #class.
1Thanks for correction, #DebanjanB
Fix your XML file to
<?xml version="1.0"?>
<!DOCTYPE stylesheet [
<!ENTITY nbsp " ">
]>
<root>
<tr class=" tableRow1" id="_pu5ufb" dr="1" _awtisprimaryrow="1">
<td width="1" class="tableBody w-tbl-cell" align="center">
<span class="selectColumnMarker">
<div class="w-chk-container">
<input bh="CHKINP" hasaction="false" class="w-chk-native" id="_m7iynb" value="1" type="checkbox" elementid="_jw4lmb" issender="false" awnomitcomponent="true" name="_jw4lmb"/>
<label bh="CHK" class="w-chk w-chk-dsize"/>
</div>
</span>
</td>
<td align="left" class="tableBody w-tbl-cell">
<span>
<table role="presentation" class="mls" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="" id="_tz87e" tabindex="0">
<a id="_3iqrbb" href="#" bh="HL" _sf="true">Analyst</a>
</td>
</tr>
</tbody>
</table>
</span>
</td>
<td class="tableBody w-tbl-cell">
</td>
<td class="tableBody w-tbl-cell">
</td>
</tr>
</root>
Then, the expression you are looking for is
//tr[td/a/text()='Analyst']/#class
But because the tr element does not have a class attribute, the result is empty.
A bit unclear what exactly you meant by ...find the associated tr class and select the tr class... once you have found ...the text Analyst....
However, as the elements are dynamic element and to locate the element with text as Analyst you can use either of the following Java based Locator Strategies:
linkText
WebElement elem = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Analyst")));
cssSelector:
WebElement elem = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("td.tableBody table.mls a[bh='HL']")))
xpath:
WebElement elem = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//td[contains(#class, 'tableBody')]//table[#class='mls']//a[text()='Analyst']")));
To extract the class attribute of the <tr> element with respect to the text as Analyst you can use the following Java based solution:
xpath:
String tr_class_attrib = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//td[contains(#class, 'tableBody')]//table[#class='mls']//a[text()='Analyst']//preceding::tr[1]"))).getAttribute("class");

How to click on the element with text as SRP Banner as per the html through Selenium and VBA?

I am trying to click on href using selenium vba in webpage but i am getting below error message.
below is the code which i have tried.
Sub test()
Dim Driver As New Selenium.FirefoxDriver
Driver.Get "http://sums.99acres.com/sums/?profilename=sums"
Driver.FindElementByName("userlogin") _
.SendKeys("xyz") _
.Submit
Driver.FindElementByName("password") _
.SendKeys("12345") _
.Submit
Driver.Get "http://sums.99acres.com/sums/getmyproducts.php?trans_id=1819-T0639786"
Driver.Wait 5000
Driver.FindElementByXPath("//a[contains(#class,'navlink')][contains(text(),'SRP Banner')]").Click
End Sub
I have also tried
Driver.FindElementByLinkText("SRP Banner").Click
Below is the error message i have received
Below is the screen shot where i want to click
Below is the html code.
<frame src="http://sums.99acres.com/sums/products_offered.php?cid=547e14073e8fca793e4629a2acca12c5Si13489029+1534998765+1534998792&trans_id=1819-T0639786" name="left" frameborder="1">
<html><head><script type="text/javascript" src="https://bam.nr-data.net/1/1621b6db8b?a=103016861&v=1071.385e752&to=NV1TZkBVXBdVWk1RVgwXZEBbG0ERWUoWSEsNXERRRkdtC1JfXEpcBhZBWkI%3D&rst=847&ref=http://sums.99acres.com/sums/products_offered.php&ap=736&be=817&fe=840&dc=839&perf=%7B%22timing%22:%7B%22of%22:1534998816389,%22n%22:0,%22f%22:0,%22dn%22:0,%22dne%22:0,%22c%22:0,%22ce%22:0,%22rq%22:13,%22rp%22:798,%22rpe%22:801,%22dl%22:803,%22di%22:838,%22ds%22:838,%22de%22:839,%22dc%22:839,%22l%22:839,%22le%22:840%7D,%22navigation%22:%7B%7D%7D&at=GRpQEAhPTxk%3D&jsonp=NREUM.setToken"></script><script src="https://js-agent.newrelic.com/nr-1071.min.js"></script><script type="text/javascript">window.NREUM||(NREUM={}),__nr_require=function(e,t,n){function r(n){if(!t[n]){var o=t[n]={exports:{}};e[n][0].call(o.exports,function(t){var o=e[n][1][t];return r(o||t)},o,o.exports)}return t[n].exports}if("function"==typeof __nr_require)return __nr_require;for(var o=0;o<n.length;o++)r(n[o]);return r}({1:[function(e,t,n){function r(){}function o(e,t,n){return function(){return i(e,[f.now()].concat(u(arguments)),t?null:this,n),t?void 0:this}}var i=e("handle"),a=e(2),u=e(3),c=e("ee").get("tracer"),f=e("loader"),s=NREUM;"undefined"==typeof window.newrelic&&(newrelic=s);var p=["setPageViewName","setCustomAttribute","setErrorHandler","finished","addToTrace","inlineHit","addRelease"],d="api-",l=d+"ixn-";a(p,function(e,t){s[t]=o(d+t,!0,"api")}),s.addPageAction=o(d+"addPageAction",!0),s.setCurrentRouteName=o(d+"routeName",!0),t.exports=newrelic,s.interaction=function(){return(new r).get()};var m=r.prototype={createTracer:function(e,t){var n={},r=this,o="function"==typeof t;return i(l+"tracer",[f.now(),e,n],r),function(){if(c.emit((o?"":"no-")+"fn-start",[f.now(),r,o],n),o)try{return t.apply(this,arguments)}catch(e){throw c.emit("fn-err",[arguments,this,e],n),e}finally{c.emit("fn-end",[f.now()],n)}}}};a("setName,setAttribute,save,ignore,onEnd,getContext,end,get".split(","),function(e,t){m[t]=o(l+t)}),newrelic.noticeError=function(e){"string"==typeof e&&(e=new Error(e)),i("err",[e,f.now()])}},{}],2:[function(e,t,n){function r(e,t){var n=[],r="",i=0;for(r in e)o.call(e,r)&&(n[i]=t(r,e[r]),i+=1);return n}var o=Object.prototype.hasOwnProperty;t.exports=r},{}],3:[function(e,t,n){function r(e,t,n){t||(t=0),"undefined"==typeof n&&(n=e?e.length:0);for(var r=-1,o=n-t||0,i=Array(o<0?0:o);++r<o;)i[r]=e[t+r];return i}t.exports=r},{}],4:[function(e,t,n){t.exports={exists:"undefined"!=typeof window.performance&&window.performance.timing&&"undefined"!=typeof window.performance.timing.navigationStart}},{}],ee:[function(e,t,n){function r(){}function o(e){function t(e){return e&&e instanceof r?e:e?c(e,u,i):i()}function n(n,r,o,i){if(!d.aborted||i){e&&e(n,r,o);for(var a=t(o),u=m(n),c=u.length,f=0;f<c;f++)u[f].apply(a,r);var p=s[y[n]];return p&&p.push([b,n,r,a]),a}}function l(e,t){v[e]=m(e).concat(t)}function m(e){return v[e]||[]}function w(e){return p[e]=p[e]||o(n)}function g(e,t){f(e,function(e,n){t=t||"feature",y[n]=t,t in s||(s[t]=[])})}var v={},y={},b={on:l,emit:n,get:w,listeners:m,context:t,buffer:g,abort:a,aborted:!1};return b}function i(){return new r}function a(){(s.api||s.feature)&&(d.aborted=!0,s=d.backlog={})}var u="nr#context",c=e("gos"),f=e(2),s={},p={},d=t.exports=o();d.backlog=s},{}],gos:[function(e,t,n){function r(e,t,n){if(o.call(e,t))return e[t];var r=n();if(Object.defineProperty&&Object.keys)try{return Object.defineProperty(e,t,{value:r,writable:!0,enumerable:!1}),r}catch(i){}return e[t]=r,r}var o=Object.prototype.hasOwnProperty;t.exports=r},{}],handle:[function(e,t,n){function r(e,t,n,r){o.buffer([e],r),o.emit(e,t,n)}var o=e("ee").get("handle");t.exports=r,r.ee=o},{}],id:[function(e,t,n){function r(e){var t=typeof e;return!e||"object"!==t&&"function"!==t?-1:e===window?0:a(e,i,function(){return o++})}var o=1,i="nr#id",a=e("gos");t.exports=r},{}],loader:[function(e,t,n){function r(){if(!x++){var e=h.info=NREUM.info,t=d.getElementsByTagName("script")[0];if(setTimeout(s.abort,3e4),!(e&&e.licenseKey&&e.applicationID&&t))return s.abort();f(y,function(t,n){e[t]||(e[t]=n)}),c("mark",["onload",a()+h.offset],null,"api");var n=d.createElement("script");n.src="https://"+e.agent,t.parentNode.insertBefore(n,t)}}function o(){"complete"===d.readyState&&i()}function i(){c("mark",["domContent",a()+h.offset],null,"api")}function a(){return E.exists&&performance.now?Math.round(performance.now()):(u=Math.max((new Date).getTime(),u))-h.offset}var u=(new Date).getTime(),c=e("handle"),f=e(2),s=e("ee"),p=window,d=p.document,l="addEventListener",m="attachEvent",w=p.XMLHttpRequest,g=w&&w.prototype;NREUM.o={ST:setTimeout,SI:p.setImmediate,CT:clearTimeout,XHR:w,REQ:p.Request,EV:p.Event,PR:p.Promise,MO:p.MutationObserver};var v=""+location,y={beacon:"bam.nr-data.net",errorBeacon:"bam.nr-data.net",agent:"js-agent.newrelic.com/nr-1071.min.js"},b=w&&g&&g[l]&&!/CriOS/.test(navigator.userAgent),h=t.exports={offset:u,now:a,origin:v,features:{},xhrWrappable:b};e(1),d[l]?(d[l]("DOMContentLoaded",i,!1),p[l]("load",r,!1)):(d[m]("onreadystatechange",o),p[m]("onload",r)),c("mark",["firstbyte",u],null,"api");var x=0,E=e(4)},{}]},{},["loader"]);</script>
<title>SUMS : MAINPAGE</title>
<style>
a:link, a:visited, a:active{color:#0000ff;}
</style>
</head>
<body style="font-family:verdana, Helvetica, sans-serif;color:#000;font-size:11px;">
<br><br><br>
<table width="80%" align="center" style="font-family:verdana, Helvetica, sans-serif;color:#000;font-size:11px;">
<tbody><tr><td>
<table width="100%" aligh="center"><tbody><tr align="center">
<td width="25%" style="color:#9400D3;font-size:14px;text-decoration:underline;">SUMS</td>
<td width="25%" style="color:#9400D3;font-size:14px;text-decoration:underline;">CRM</td>
<td width="25%" style="color:#9400D3;font-size:14px;text-decoration:underline;">Mis</td>
<td width="25%" style="color:#9400D3;font-size:14px;text-decoration:underline;"><!--a href="../sums/logout.php?name=vishal&cid=547e14073e8fca793e4629a2acca12c5Si13489029+1534998765+1534998807" -->Logout</td>
</tr></tbody></table><br>
</td></tr></tbody></table>
<br><br><table width="300" border="0" cellspacing="2" cellpadding="2" style="font-family:verdana, Helvetica, sans-serif;color:#000;font-size:11px;">
<tbody><tr> <td colspan="2" style="color:#ff6600;font-weight:bold;font-size:18px;">PRODUCTS OFFERED</td></tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td width="19">1.</td>
<td width="374"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Tahoma','sans-serif'"><b><a target="right" href="http://sums.99acres.com/sums/zedo_srp_products.php?module=SUMS&trans_id=1819-T0639786&productType=SRPB&prod=SRPB" class="navlink">SRP Banner </a></b> <span style="COLOR: gray"> (Pending: 1 of 1)</span></span></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
</tbody></table>
<br><br>
<script type="text/javascript">window.NREUM||(NREUM={});NREUM.info={"beacon":"bam.nr-data.net","licenseKey":"1621b6db8b","applicationID":"103016861","transactionName":"NV1TZkBVXBdVWk1RVgwXZEBbG0ERWUoWSEsNXERRRkdtC1JfXEpcBhZBWkI=","queueTime":0,"applicationTime":736,"atts":"GRpQEAhPTxk=","errorBeacon":"bam.nr-data.net","agent":""}</script>
</body></html>
As per the HTML you have shared to click on the desired element first you need to Switch To the Frame then lookout for the element and you can use the following solution:
Driver.Get "http://sums.99acres.com/sums/getmyproducts.php?trans_id=1819-T0639786"
Driver.Wait 5000
Driver.SwitchToFrame.FindElementByXPath("//frame[#name='left' and contains(#src,'http://sums.99acres.com/sums/products_offered.php')]", timeout:=10000)
Driver.Wait 3000
Driver.FindElementByXPath("//a[#class='navlink' and contains(#href,'http://sums.99acres.com/sums/zedo_srp_products.php?module')][contains(.,'SRP Banner')]").Click
This is too long to fit as a comment legibly. Can you try chucking the pageSource HTML into an HTMLDocument and clicking from there? Following code lines go in continuing from the Wait line you already have, reproduced below:
Driver.Wait 5000
Dim html As New HTMLDocument, ele As Object
html.body.innerHTML = Driver.PageSource
Set ele = html.getElementsByTagName("form")(0)
ele.querySelector("a[href*='trans_id=1819']").Click
I have left it in several stages so you can debug whether the ele is even set before attempting a click.

PHP selenium find parent sibiling

I have below code and I want find second td value. How can I select text with <br/> in it?
<tr>
<td valign="top" style="width: 85px">
<span class="fieldtext">Address:</span>
</td>
<td valign="top">
Shaftesbury House, 1st floor
<br/>20 Tylney Road
<br/>Bromley
<br/>Greater London
<br/>BR1 2RL
<br/>United Kingdom
<br/>
</td>
<td style="width: 200px; vertical-align: top; text-align: right;" rowspan="2" />
</tr>
Assuming you want to find the td element by the Address: label defined previously, you can use the following-sibling axis:
//td[span = 'Address:']/following-sibling::td
Then, after locating the element, call getText() method:
$driver->findElement(WebDriverBy::xpath("//td[span = 'Address:']/following-sibling::td"))->getText();
Try as below :-
$driver->findElement(WebDriverBy::xpath('//tr/td[2]'))->getText();
Hope it helps....:)

webdriver why the table can get its size but canot get its text

the pagesource:
<TABLE class=mini-listbox-items style="WIDTH: 100%" cellSpacing=0 cellPadding=0>
<TBODY>
<TR class="mini-listbox-item mini-listbox-item-selected" id=mini-45$0 index="0">
<TD class=mini-listbox-checkbox><INPUT id=mini-45$ck$0 type=checkbox CHECKED></TD>
<TD class="">xx</TD></TR>
<TR class=mini-listbox-item id=mini-45$1 index="1">
<TD class=mini-listbox-checkbox><INPUT id=mini-45$ck$1 type=checkbox></TD>
<TD class="">yy</TD></TR></TBODY></TABLE></DIV><INPUT type=hidden value=Y></DIV>
<DIV class=mini-errorIcon></DIV></DIV></DIV>
<DIV class=mini-shadow style="DISPLAY: none; Z-INDEX: 1002; LEFT: 393px; WIDTH: 78px; TOP: 326px; HEIGHT: 46px"></DIV></BODY>
my code:
driver.findElements(By.xpath(("//td[#class='mini-listbox-checkbox']/following-sibling::td[position()=1]"))).size();
driver.findElements(By.xpath(("//td[#class='mini-listbox-checkbox']/following-sibling::td[position()=1]"))).get(0).getText();
the first line,it prints 2,then the second line,it prints none,why?
Your logic is working fine.
int n=driver.findElements(By.xpath(("//td[#class='mini-listbox-checkbox']/following-sibling::td[position()=1]"))).size();
String s=driver.findElements(By.xpath(("//td[#class='mini-listbox-checkbox']/following-sibling::td[position()=1]"))).get(0).getText();
System.out.println(n+"======>>"+s);
The above code is giving correct output only.
2======>>xx