I am tring to pass the value from excel to xpath but I am getting noSuchElementFoundException.
This is the code :
public String accountBalance(String accountNameToFind)
{
String accountBalance = null;
accountBalance = driver.findElement(By.xpath("//*[contains(text(),'" + accountNameToFind + "')]/following-sibling::td")).getText();
return accountBalance;
}
HTML:
<tr>
<th scope="row" class="">
SDRSP
<sup>2</sup> - <span class="td-copy-nowrap">1253 3292AUS</span>
<strong>
<a href="servlet/ca.tdbank.banking.servlet.SiteTransferOutServlet?dest=BROKER" class="td-link-standalone td-link-standalone-secondary">
<span class="td-copy-nowrap">
WebBroker
<span class="td-link-icon">›</span>
</span>
</a>
</strong>
</th>
<td class="td-copy-align-right">
$10,000.00
</td>
<td class="td-copy-align-centre">
</td>
</tr>
If your element located inside frame, you need to switch to it first and then handle element:
driver.switchTo().frame("frame_ID");
accountBalance=driver.findElement(By.xpath("//*[contains(text(),'"+accountNameToFind+"')]/following-sibling::td")).getText();
...some other actions...
driver.switchTo().defaultContent();
If your frame has no id attribute you can try to use another attributes, e.g. class name:
driver.switchTo().frame(driver.findElement(By.cssSelector("frame.frameClassName")));
Your function should be simplified to the below.
public String accountBalance(String accountNameToFind)
{
return driver.findElement(By.xpath("//th[contains(text(),'" + accountNameToFind + "')]/following-sibling::td")).getText();
}
I replaced * with th in the XPath because I'm assuming that the th is going to be the only element that you want to use. The accountName may exist elsewhere on the page and be causing issues.
If this is in a frame/iframe, you will need to switch to the frame first as in #Andersson's answer.
This may be a case where this portion of the page is dynamic so you may have to wait for the element to be visible before trying to scrape the data. See WebDriverWait with ExpectedConditions.elementToBeVisible().
Related
I am new to selenium and I need to get the value £1000 which is inside
td class='ng-ngclass' /td
and i need to find that by finding text 'Wrapper1' from
th class="ng-ngclass" colspan="3" - Wrapper1 - /th
I should pass the text 'Wrapper1' in %s in the below constant
I've tried the below one
private static final String WRAPPER_VALUE = "//th[#class='ng-ngclass'and contains(text(), %s)] and //td[#class='ng-ngclass']" in FirePath but it is returning me 1 number: NaN in firefox console.. What does that really mean?
Please help me. Any help will be appreciated.
<div id="summaryEncash" class="body scroll-body" ng-class=" {clsOnlyAdPlan: hasOverview === false}">
<!-- ngRepeat: asset in assetTypes -->
<table class="data-stripe ng-scope" ng-repeat="asset in assetTypes" analytics="homePortfolioTap" ng-click="showAssetDetail(0)" cellspacing="0" cellpadding="0">
<tbody>
<tr ng-show="isCurrentValue">
<th class="ng-ngclass" colspan="3">Wrapper1</th>
</tr>
<tr class="summary-value" ng-show="isCurrentValue">
<td class="ng-ngclass">£1000</td>
<td/>
<td class="arrow">
<i class="next next-arrow"/>
</td>
</tr>
The Xpath expression //td[#class='ng-ngclass'] returns the td element with its text node.
The Xpath expression //td[#class='ng-ngclass']/text() should return the text value of td element. So the string £1000. Then you have to parse it to get a number.
Html Code
<table id="tblRenewalsFiled" class="StatusList" width="100%" cellspacing="0" cellpadding="0" border="0">
<tbody data-bind="foreach: RenewalFilterModels">
<tr>
<td>
<a id="aFilterMenu_NotAudited" class="aFilter"
data-bind="click: $parent.ShowFilter, attr:{'id':'aFilterMenu_' + StatusName}" href="#">
Not Audited
<span class="count">13</span>
</a>
</td>
</tr>
</tbody>
</table>
from the above, when I use the getText, method it will return "Not Audited 13"
String filterValue = driver.findElement(By.id("aFilterMenu_NotAudited").getText();
my expectation was only for Not Audited, provide an solution or suggestion to truncate the span class count i.e "13"
So all I can to advice is to take cssSelector "a.aFilter, span", it will return an array of two elements [ 'Not Audited 13', '13' ]
String[] value = driver.findElement(By.id("aFilterMenu_NotAudited").getText();
then you are truncating the second element from the end of the first one:
String myText = value[0].substring(0, value[0].length() - value[1].length());
Looks like a bit tricky.
Another way is using xpath:
//a[#class='aFilter'][not(self::span)]/text()
two different Div's inside the Div have an check box, so i want to click the checkbox (i.e inside the "divPatPortfolioStatusCount"), both checkbox xpath are similar
(i.e By.xpath("//input[#accesskey='2']")
Html Code
<div id="divCreatePortfolio" class="wrapper">
<table class="adminlistfilter" width="100%" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr data-bind="if: RowCounts>0, attr: {PortfolioId: Id, DescName:Name}" portfolioid="2" descname="Client-Default">
<td style="width: 5%;">
<input type="checkbox" data-bind="attr: { accesskey: Id }" accesskey="2">
</td>
</tr>
</tbody>
</table>
</div>
<div id="divPatPortfolioStatusCount" class="wrapper">
<table class="adminlistfilter" width="100%" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr data-bind="if: RowCounts>0, attr: {StatusId: Id, DescName:Name}" statusid="2" descname="Abandoned">
<td style="width: 5%;">
<input type="checkbox" data-bind="attr: { accesskey: Id }" accesskey="2">
</td>
</tr>
</tbody>
</table>
</div>
</div>
my Java code
WebElement statusDiv= driver.findElement(By.id("divPatPortfolioStatusCount"));
WebElement checkBox = statusDiv.findElement(By.xpath("//input[#accesskey='2']"));
checkBox.click();
while executing under the "divCreatePortfolio" checkbox only checked not for "divPatPortfolioStatusCount" let me know the problem with my xpath
You need click on those 2 different check box separately as below right?
//To check Status checkbox
driver.findElement(By.xpath("//div[#id='divCreatePortfolio']//input")).click();
//To check Status count checkbox
driver.findElement(By.xpath("//div[#id='divPatPortfolioStatusCount']//input")).click();
I would suggest you to use css and nth-child() function and control child index from test
body>div:nth-child(1) input
body>div:nth-child(2) input
Changing the number of nth-child(1) from 1 to 2 will find consecutive check boxes
update the xpath in my code
WebElement statusTable = driver.findElement(By
.xpath("//*[#id='divPatPortfolioStatusCount']/table/tbody"));
List<WebElement> rows = statusTable.findElements(By.tagName("tr"));
for (int i = 0; i < rows.size(); i++) {
WebElement checkBoxSts = driver
.findElement(By
.xpath("//*[#id='divPatPortfolioStatusCount']/table/tbody/tr["
+ i + "]"));
String statusAccessKey = checkBoxSts.getAttribute("statusid");
if (statusAccessKey.equals(portfolioId)) {
WebElement checkbox = driver
.findElement(By
.xpath("//*[#id='divPatPortfolioStatusCount']/table/tbody/tr["
+ i + "]/td[1]/input"));
checkbox.click();
break;
}
}
collect the statusid from database with respective of status and pass the statusid in this method with parameter, and proceed
you can do it as :
List<WebElements> lst = driver.findElements(By.xpath("//input[#accesskey='2']"));
for (WebElement web : lst)
if(!web.isSelected())
web.click();
And if you want to select input based on div id u can use:
//div[#id='divPatPortfolioStatusCount']//input
I have:
<table class="cast_list">
<tr><td colspan="4" class="castlist_label"></td></tr>
<tr class="odd">
<td class="primary_photo">
<a href="/name/nm0000209/?ref_=ttfc_fc_cl_i1" ><img height="44" width="32" alt="Tim Robbins" title="Tim Robbins"src="http://ia.media-imdb.com/images/G/01/imdb/images/nopicture/32x44/name-2138558783._V379389446_.png"class="loadlate hidden " loadlate="http://ia.media-imdb.com/images/M/MV5BMTI1OTYxNzAxOF5BMl5BanBnXkFtZTYwNTE5ODI4._V1_SY44_CR1,0,32,44_AL_.jpg" /></a> </td>
<td class="itemprop" itemprop="actor" itemscope itemtype="http://schema.org/Person">
<a href="/name/nm0000209/?ref_=ttfc_fc_cl_t1" itemprop='url'> <span class="itemprop" itemprop="name">Tim Robbins</span>
</a> </td>
<td class="ellipsis">
...
</td>
how can I get only the information inside the second td class? (td class= itemprop). I want to get "/name/nm0000209/?ref_=ttfc_fc_cl_t1" and "Tim Robbins".
This is my code:
Elements elms = doc.getElementsByClass("cast_list").first().getElementsByTag("table");
Elements tds = elms.select("td");
for(Element td : tds){
if(td.attr("class").contains("itemprop")){
Elements links = tds.select("a[href]");
for(Element link : links){
if(link.attr("href").contains("name/nm"))
{
String castname = link.text();
String castImdbId = link.attr("href");
System.out.println("CastName:" + castname + "\n");
System.out.println("CastImdbID:" + castImdbId + "\n");
}
but it also returns the text of the link inside td class="primary_phptp" which is null, this is part of my output:
CastName:
CastImdbID:/name/nm0000209/?ref_=ttfc_fc_cl_i1
CastName:Tim Robbins
CastImdbID:/name/nm0000209/?ref_=ttfc_fc_cl_t1
CastName:
......
Could someone please let me know where is my problem? I think the condition if(td.attr("class").contains("itemprop")) does not work at all.
Thanks,
Use a different css selector instead of td. Since the right <td> is identified be the class, why not use it:
td.itemprop
Your java code then would start like this instead
Elements tds = elms.select("td.itemprop");
I'm automating a task using Java and Selenium.
I want to set a checkbox (which is in the first column of a table) based on whether the value in the second column matches my input value. For example, in the following code snippet, the value "Magnus" matches my input value so I want to set the checkbox associated with it.
<table class="cuesTableBg" width="100%" cellspacing="0" border="0" summary="Find List Table Result">
<tbody>
<tr class="cuesTableBg">
<tr class="cuesTableRowEven">
<tr class="cuesTableRowOdd">
<td align="center">
<input class="content-nogroove" type="checkbox" name="result[1].chked" value="true">
<input type="hidden" value="1c62dd7a-097a-d318-df13-75de31f54cb9" name="result[1].col[0].stringVal">
<input type="hidden" value="Magnus" name="result[1].col[1].stringVal">
</td>
<td align="left">
<a class="cuesTextLink" href="userEdit.do?key=1c62dd7a-097a-d318-df13-75de31f54cb9">Magnus</a>
</td>
<td align="left"></td>
<td align="left">Carlsen</td>
<td align="left"></td>
</tr>
<tr class="cuesTableRowEven">
</tbody>
</table>
But I'm unable to do it. In the above case, the following two lines serve the purpose (as my input value matches with that in the second row):
WebElement checkbox = driver.findElement(By.xpath("//input[#type = 'checkbox' and #name = 'result[1].chked']"));
checkbox.click();
But it can't be used as the required value might not always be in the second row.
I tried following code block but to no avail:
List<WebElement> rows = driver.findElements(By.xpath("//table[#summary = 'Find List Table Result']//tr"));
for (WebElement row : rows) {
WebElement userID = row.findElement(By.xpath(".//td[1]"));
if(userID.getText() == "Magnus") {
WebElement checkbox = row.findElement(By.xpath(".//input[#type = 'checkbox']"));
checkbox.click();
break;
}
}
For what it's worth, XPath of the text in the second column:
/html/body[#id='mainbody']/table/tbody/tr/td/div[#id='contentautoscroll']/form/table[2]/tbody/tr[3]/td[2]/a
I don't know about CSS Selectors. Will it help here?
If you know the input value already you just use below xpath to select respected checkbox
"//a[text(),'Magnus']/parent::td/preceding-sibling::td/input[#type='checkbox']"
Update:
"//a[text()='Magnus']/parent::td/preceding-sibling::td/input[#type='checkbox']"
While comparing two strings equals should be used instead of ==
Replace
if(userID.getText() == "Magnus")
with
String check1 = userID.getText();
if(check1.equals("Magnus")
Seems like it was a silly mistake on my part. The following code snippet, a fairly straightforward one, worked.
List<WebElement> rows = driver.findElements(By.xpath("//table[#class='cuesTableBg']//tr"));
for (WebElement row : rows) {
WebElement secondColumn = row.findElement(By.xpath(".//td[2]"));
if(secondColumn.getText().equals("Magnus")) {
WebElement checkbox = row.findElement(By.xpath(".//td[1]/input"));
checkbox.click();
break;
}
}