Match text in table and click on it using selenium webdriver - xpath

I have one table in which there is one column called "state". I have to find and click on link which matches with text "closed successful" in that column. xpath for each cell in that column is
"//*[#id='TicketID_xxxxxx']/td[7]/div
Where xxxxxx are numbers of tickets. So how can I find matching text with different xpath values and click on it using selenium webdriver. Please help.
(Optional- I have to click on that element, click on back and find next element with same matching text)

Hi Sanket Patel please do it like below
driver.get(yourWebPageLInk); // link to your web-table web page
// take all of the element under Column "State" inside list
List<WebElement> columVal = driver.findElements(By.xpath("//*[starts-with(#id,'TicketID_')]/td[7]/div"));
// count the size of the list to match with the size of the column state
System.out.println("Size of the contents in the column state is : " +columVal.size());
// now for matching one of the content and then performing some action please
// start a for loop
String oneVal = "closed successful";
for(int i=0;i<columVal.size();i++){
System.out.println("Content text is : " + columVal.get(i).getText());
// match the content here in the if loop
if(columVal.get(i).getText().equals(oneVal)){
// perform action
columVal.get(i).click();
}
}

You can use the text() method
String text = "someText";
driver.findElement(By.xpath("//div[contains(text(), '" + text + "')]")).click();
Or with cssSelector
String text = "someText";
driver.findElement(By.cssSelector("div:contains('" + text + "')")).click();

You can use the TableDriver extension to Selenium WebDriver for situations like this. (https://github.com/jkindwall/TableDriver.Java). It would be helpful if there was more detailed info about the table in question, however if we assume that the table has a column "link" containing the link you need to click, and we assume the id of the main table element is "tabelId" you could do it like this:
Table table = Table.create(driver.findElement(By.id("tableId"));
WebElement element = table.findCell("state=closed successful", "link").Element;
element.findElement(By.tagName("a")).Click();

Related

XPath to return value only elements containing the text

would like to return value of 'Earnings per share' (i.e. -7.3009, -7.1454, -19.6295, -1.6316)
from "http://www.aastocks.com/en/stocks/analysis/company-fundamental/earnings-summary?symbol=01801"
using below as a example for '-7.3009'
=importxml("http://www.aastocks.com/en/stocks/analysis/company-fundamental/earnings-summary?symbol=01801", "//tr/td[contains(text(),'Earnings')]/td[2]")
However, it returns #N/A.
Can someone help?
this xpath will return your specific data
id("cnhk-list")//tr[td[contains(., "Earnings Per Share")]]/td[starts-with(#class, "cfvalue")]//text()
xpath explanation in english is " you actually needs to select the td where row contains Earnings Per Share which is in table that has some specific ID

QTP UFT How to find a row number and column number? Angular JS

When I spy the table it shows just browser - Page - WebElement and also UI is developed in Angular JS.
I there any way to find row number and column number? By the way I am using UFT/QTP
From the source-code's image which you have attached, it is quite evident that the webElement corresponding to the rows have class = "ui-grid-row ng-scope". So, you can make use of descriptive programming. I assume that you already have added the object Browser(...).Page(...) to your OR.
Set rowDesc = Description.Create
rowDesc("Class Name").value = "WebElement"
rowDesc("Class").value = "ui-grid-row ng-scope"
Set objRows = Browser(...).Page(...).ChildObjects(rowDesc)
rowCount = objRows.Count 'This variable should now contain the total number of rows"
Now, this is just an idea which you can give a try. If it works for you, you can further enhance it to get the column count. If there is no way you can get the column count, then you can get the total cell count using the same method. In that case, you just need to change the value of property "class" to the one mentioned in the Object Spy Image("ui-grid-cell-contents ng-binding ng-scope"). Now you have Row Count and Cell Count. To get the column count, you can divide cell count by row count.(Again, this will give you correct answer ONLY IF there are same number of columns for each of tbe rows).

Clicking on specific row in a WebTable using UFT/QTP

I am having hard time clicking specific row in a Web Table. My code finding proper row, but when I use Child Item method it complains that Object is not found. Here is my code:
Desc = "Record to click"
If Browser("B").Page("P").WebTable("W").exist(10) Then
totalRows = Browser("B").Page("P").WebTable("W").RowCount()
For RowNum = 1 To totalRows
If aDesc = Browser("B").Page("P").WebTable("W").GetCellData(RowNum,2) Then
Browser("B").Page("P").WebTable("W").ChildItem(RowNum,2,"WebElement",0).click
End If
Next
End If
I spied on the value in the row it is Web Element, I tried to use Link- didn't work. Also I tried to Child Item(aDesc,2,"WebElement",0)- didn't work either. I used 0 for the index because there is only one element in the row - simple text. I keep getting this error in many other tests. On rare occasion this approach works in some tests, but most of the time it complains for absence of object.
Thank you very much for your help!
It happened with me as well. When I researched, I found in some of the old HP blogs that ChildItem method does not works correctly with WEBElement, but that was for QTP 9.0, and I was using 12.02.Anyhow, I can't figure out why its happening, and ended up using the following -
Set oExcptnDetail = Description.Create
oExcptnDetail("micclass").value = "WebElement"
oExcptnDetail("html tag").value = "TD"
Set chobj=Browser("").Page("").WebTable("Code").ChildObjects(oExcptnDetail)
chobj(0).Click
On a side note, in order to check a webelement/link exist in a certain row and column, use the following.
Browser("B").Page("P").WebTable("W").ChildItemCount(2,2,"WebElement")
Getcell data will return you anything that is in the desired row and column irrespective of what it is (link,webelement etc) and hence your assumption of if loop will go wrong.
Try this:
Browser("B").Page("P").WebTable("W").object.rows(rownum-1).cells(colnum-1).Click
I was trying to click the first link in my table and this code clicked the item
Set oDesc = Description.Create()
oDesc("html tag").Value = "A"
Set rc = Browser("B").Page("A").WebTable("html id:=gridTable").ChildObjects(oDesc)
rc(0).Click
'num = rc.Count() 'get the number of link in a page
'For i=0 to num-1
'ref = rc(0).GetROProperty("href") 'get the “href”propety of the i th link
'MsgBox ref
'Next
or
Browser("B").Page("A").WebTable("html id:=gridTable").ChildItem(2,8,"Link",0).click
successfully clicks the link I needed

How can I make automatic generated column in Adempiere ?

I have two columns called Quantity and Issued Quantity. I want that when I put value in Quantity column, for instance 3, the Issued Quantity will automatically generate 3. Also I want it to happen the other way around.
The example is on Purchase Order window, PO Line tab. in Quantity section. When I put 4 in Quantity field, the PO Quantity field automatically generate 4.
I try to imitate the column and field but it doesn't work.
This is accomplished in Adempiere by a Callout which is configured in what Adempiere calls the Application Dictionary
From the example you gave; updating the qty on the Purchase Order.
If you login to Adempiere using the System user, you can view and modify the Application Dictionary.
From the main menu select Application Dictionary->Table & Column.
In the Search box that opens enter C_OrderLine as the DB Table name.
Now Column tab and scroll down the list to locate the QtyEntered column. Switch to the Form view and near the end you will see were you can enter the field Callout.
You should see that the C_OrderLine.QtyEntered field already has a value "org.compiere.model.CalloutOrder.qty; org.compiere.model.CalloutOrder.amt" which indicates it should run the method qty in the class org.compiere.model.CalloutOrder followed by the method amt in the org.compiere.model.CalloutOrder class.
If you open those classes you can see how easily you can evaluate and modify values. Breaking some of of it down for you... if you open the CalloutOrder.java class you cab scroll down until you find the qty method.
public String qty (Properties ctx, int WindowNo, GridTab mTab, GridField mField, Object value)
{
You need use the signature as above for any new callout method you create. Follow that approach and Adempiere will look after passing the correct values for you
if (isCalloutActive() || value == null)
return "";
It's good practice to start the method with the above ensure you do not open a Callout from within a Callout - which would break the Adempiere rules.
int M_Product_ID = Env.getContextAsInt(ctx, WindowNo, "M_Product_ID");
Is an example of how you could extract values from the existing window... the syntax would remain the same regardless of Column/Field the you just need to enter the "M_Product_ID" which is the Field name you wish to extract to use.
Now this Callout is called by more than one Column/Field so it is littered with a big if...then...else to executed the logic needed for the relevant field. It's not pretty, but this is aimed at business developers who will concentrate more on business logic than coding principals.
The code you are interested in is
else if (mField.getColumnName().equals("QtyEntered"))
{
int C_UOM_To_ID = Env.getContextAsInt(ctx, WindowNo, "C_UOM_ID");
QtyEntered = (BigDecimal)value;
BigDecimal QtyEntered1 = QtyEntered.setScale(MUOM.getPrecision(ctx, C_UOM_To_ID), BigDecimal.ROUND_HALF_UP);
if (QtyEntered.compareTo(QtyEntered1) != 0)
{
log.fine("Corrected QtyEntered Scale UOM=" + C_UOM_To_ID
+ "; QtyEntered=" + QtyEntered + "->" + QtyEntered1);
QtyEntered = QtyEntered1;
mTab.setValue("QtyEntered", QtyEntered);
}
QtyOrdered = MUOMConversion.convertProductFrom (ctx, M_Product_ID,
C_UOM_To_ID, QtyEntered);
if (QtyOrdered == null)
QtyOrdered = QtyEntered;
boolean conversion = QtyEntered.compareTo(QtyOrdered) != 0;
log.fine("UOM=" + C_UOM_To_ID
+ ", QtyEntered=" + QtyEntered
+ " -> " + conversion
+ " QtyOrdered=" + QtyOrdered);
Env.setContext(ctx, WindowNo, "UOMConversion", conversion ? "Y" : "N");
mTab.setValue("QtyOrdered", QtyOrdered);
}
The code
mTab.setValue("QtyOrdered", QtyOrdered);
Is where it sets the value of the other quantity field Qty Ordered.
Also, the call must not be Java. It is possible to link any [JSR223 script][3]. I never actually tried this approach myself, but it is even possible to implement Callouts using the Drools Rules Engine!

How to mitigate users adding blank rows to a Google Spreadsheet and thus breaking ListFeed API?

We use Google Spreadsheets to collect research data and allow users to directly enter data into spreadsheets that have been pragmatically generated. This has been working fairly well until a user enters a blank line in between data rows! They may do this for readability or they may have deleted a row, anyway...
Google's documentation is clear on this:
https://developers.google.com/google-apps/spreadsheets/#retrieving_a_list-based_feed
The list feed contains all rows after the first row up to the first blank row.
So the problem is that I have 'harvester' scripts that rip through these spreadsheets, collecting up data for archival / local databasing. These scripts use the ListFeed, so they stop when reach a blank row and miss data!
The documentation suggests:
If expected data isn't appearing in a feed, check the worksheet manually to see whether there's an unexpected blank row in the middle of the data.
Manually! Gasp, I have hundreds of sheets :) Do you have suggestions for mitigating this situation other than yelling at users whenever I see this happen! Thank you
This is the only way that I think we can even get close with the spreadsheet API. This is NOT complete code, it's within a function I wrote but you get the drift... it's in C#:
Working with example:
--row 1 = header row
--row 2 = data
--row 3 = data
--row 4 = totally blank
--row 5 = data
--row 6-100 = totally blank
In English:
Get the worksheet's ListFeed.Entries.Count. ListFeeds ignore header row so in this example count would be "2".
Get the worksheet's CellFeed in order to cycle through the cells. CellFeeds DO include the header row as row 1, so in the example, from the perspective of a CellFeed, the first blank row must be row 4 (header=1, then 2 data rows, then first blank line which terminates the ListFeed set), therefore we should begin looking through cells at row 5 and beyond for any cell that is NOT empty:
foreach (WorksheetEntry entry in wsFeed.Entries)
{
//Get the worksheet CellFeed:
CellQuery cellQuery = new CellQuery(entry.CellFeedLink);
CellFeed cellFeed = service.Query(cellQuery);
//Get the worksheet ListFeed to compare with the CellFeed:
AtomLink listFeedLink = entry.Links.FindService(
GDataSpreadsheetsNameTable.ListRel, null
);
ListQuery listQuery = new ListQuery(listFeedLink.HRef.ToString());
//need to have service object already created for this... see API docs
ListFeed listFeed = service.Query(listQuery);
//Now to check if there is data after the ListFeed
//set which would indicate a blank line in the data set (not allowed)
foreach (CellEntry cell in cellFeed.Entries)
{
//start looking in cells in the row after what would be the first blank row
if (cell.Row > listFeed.Entries.Count + 2)
{
if (cell.Value != "")
{
MessageBox.Show("ERROR: There appears to be a blank row +
in the middle of the data set in worksheet: " +
entry.Title.Text + ". Completely blank rows " +
"are not allowed in between data rows. Each row " +
"within the data set must have at least one " +
"value in at least one cell. There CAN and " +
"should be blank rows after the data set at " +
"the bottom of the worksheet.");
return false;
}
}
}
}

Resources