How to fetch last row data from table using selenium webdriver - selenium-webdriver

I have used this below code to get all row from table but I wanted only last row data
WebElement webtable=driver.findElement(By.id("requisitionsTable"));
List <WebElement> rowCollection=webtable.findElements(By.xpath("//*[#id='requisitionsTable']/tbody/tr"));
System.out.println("No. of Rows in the WebTable: "+rowCollection.size());
for(WebElement rowElement:rowCollection)
{
System.out.println("rowElement"+rowElement.getText());
List<WebElement> colCollection=rowElement.findElements(By.xpath("td"));
for(WebElement colElement:colCollection)
{
System.out.print(colElement.getText());
System.out.print(" ");
}
System.out.println("");
}

Use below cssSelector to get the last row data.
By lastRow=By.cssSelector("table#requisitionsTable tr:last-child");
driver.findElement(lastRow).getText();

Related

Selenium Webdriver - How to select record from grid view

How can I select specific record from grid view using selenium webdriver (JAVA)
Suppose I want to select the highlighted record in this snapshot. How I can do that:
You should systematically break down the grid into sections, loop through each row's cell to find a match using a unique key and then select the row. In this case, the unique key is the Employee Number column.
In Java:
public void selectRow(String expEmpNo) {
// Get the grid
WebElement grid = driver.findElement(By.id("gpUsers"));
// Get all the rows
By locAllRows = By.xpath(".//*[contains(#class,'x-grid3-body')]//*[contains(#class,'x-grid3-row')]");
List<WebElement> allRows = grid.findElements(locAllRows);
// Loop through each row and compare actual emp. no. with expected emp. no.
for(WebElement row : allRows) {
// Emp No. is 4th column
By locEmpNo = By.xpath(".//*[#class='x-grid3-cell-inner x-grid3-col-4']");
// Get the Emp. No.
String actEmpNo = row.findElement(locEmpNo).getText();
// Compare actual vs expected
if(actEmpNo.equals(expEmpNo)) {
row.click(); // Select row
System.out.println("Selected row " + (allRows.indexOf(row) + 1) + " having Emp. No. " + expEmpNo)
break; // exit the for loop
}
}
}
I believe that you need something like this
WebElement element = driver.findElement(By.xpath("use the xpath here"));
Select oSelect = new Select(element);
oSelect.selectByVisibleText("enter the visible text you want to select");

how to Print a list of unique icons used on article title and how many times was it used

http://slashdot.org/
Print how many articles are on the page, since it is dynamically changed everyday.
Print a list of unique icons used on articles.
hi find the answer below
public class latestNews {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://slashdot.org/");
// take everything inside the list for article
List<WebElement> latestNews = driver.findElements(By.className("story"));
// take everything for icon inside the list
List<WebElement> releventicons = driver.findElements(By.xpath("//*[#class='topic']/a/img"));
// print total number of latest articles for today
System.out.println("Total article for today is : " + latestNews.size());
// also plz note that size of icons will also be same as articles
// print headings and icons of the total number of articles for today
for(int i =0;i<latestNews.size();i++){
System.out.println("Article for today is : "+ latestNews.get(i).getText() + "=="+ releventicons.get(i).getAttribute("alt"));
}
}
}
just use this:
List<WebElement> totalarticale = driver.findElements(By
.cssSelector(" #firehoselist>article"));
System.out.println("size is "+totalarticale.size());
article icon number is same as articale number.

It's not entering in to the loop, although the element matched was present in the table

String bknum=d.findElement(By.xpath(".//*[#id='error-back']/a[#style]")).getText();
List<WebElement> cols=d.findElements(By.xpath("//tbody/tr/td[2][text()]"));
System.out.println("Coloumn was selected");
for(WebElement col:cols){
if(col.equals(bknum)){
System.out.println("Bookng exists");
d.findElement(By.xpath("//tbody/tr/td[2][contains(text(),"+bknum+")]/following-sibling::*[7]/a[2]/img")).click();
}

how to Click and check the hyper Links in the table columns of webpage using Selenium web driver(Java code)

My question is simple- Using Selenium.Can anyone help me on this.I am using selenium in java.The webpage contain table with 5 columns.In the second column Hyperlinks lists are there.I would like to click each and every link in the second column one by one and check some criteria.
Please do the needful.Thanks in Advance.
=================================================================================
This is my Code Part:
WebElement table = d1.findElement(By.xpath("//*[#id='search_results']/table"));
// Now get all the TR elements from the table
List<WebElement> allRows = table.findElements(By.tagName("tr"));
//WebElement cell1 =allRows.get(1);
// System.out.println("Table"+ allRows.get(1).getText());
//System.out.println(allRows.size());
// And iterate over them, getting the cells
for (WebElement row : allRows) {
List<WebElement> cells = row.findElements(By.tagName("td"));
for (WebElement cell : cells) {
String s = cell.getText();
==================================================================
This code is retrieving all table data's.I need to click each and everylink in the second column values(Hyperlinks) one by one and check some criteria.
=================================================================
It is showing "WebElements" cannot be a resolved type.This is my full code
public class Reading {
public static void main(String[] args) throws InvalidFormatException, IOException, InterruptedException
{
WebDriver d1= new FirefoxDriver();
d1.get("https://fusion.paypal.com/fusionportal/");
WebElement ds = d1.findElement(By.xpath("//*[#id='ext-gen176']/ul/li/a"));
ds.click();
WebElement dt = d1.findElement(By.xpath("//*[#id='ext-gen178_startDate']"));
dt.sendKeys("20/11/2013");
WebElement sc = d1.findElement(By.xpath("//*[#id='searchDeploys']/tbody/tr[2]/td[2]"));
sc.click();
Thread.sleep(2000);
//WebElement table_element = d1.findElement(By.xpath("//*[#id='ext-gen236']"));
//List<WebElement> tr=table_element.findElements(By.xpath("//*[#id='ext-gen236']"));
WebElements links = d1.findElement(By.xpath("//*[#id='ext-gen238']/div[1]/table/tbody/tr/td[2]/div"));
for (WebElement link : links) {
link.click();
}
}
}
Hard to know without seeing the HTML as well, but assuming it's a regular table from the rest of your code, try something like
List<WebElement> links = d1.findElement(By.xpath("//*[#id='search_results']/table/tr/td[2]/a"));
for (WebElement link: links) {
link.click();
}

How do I check all checkboxes on a page using webdriver?

I want to check/uncheck all checkboxes present in page.
Code snippet is as following:-
#Test
public void checkBoxAll() {
List<WebElement> checkBoxList=driver.findElements(By.cssSelector("input [type='checkbox']"));
for(WebElement checkBox:checkBoxList)
{
checkBox.click();
}
List<WebElement> unCheckedBoxList=driver.findElements(By.cssSelector("input:not(:checked)[type='checkbox']"));
if(!CollectionUtils.isEmpty(unCheckedBoxList))
Assert.fail();
}
First I find out all check boxes using ("input [type='checkbox']").Then click them in loop and then find all check boxes which are checked(for test case to be executed successfully there should be none).
I tried some approaches from google,but ain't working for me.Please tell me what I am doing wrong being a novice
Don't know what was problem in what I was doing earlier or the solution proposed above,but both didn't work.I tried out different things and below is the working solution.id='first' is id of div under which all checkboxes are placed.
#Test
public void CheckBoxAll() {
String cssSelectorForNotCheck=("[id='first'] input[type='checkbox']:not(:checked)");
List<WebElement> checkBoxList=driver.findElements(By.cssSelector(cssSelectorForNotCheck));
for(WebElement checkBox:checkBoxList)
{
checkBox.click();
}
checkBoxList=driver.findElements(By.cssSelector(cssSelectorForNotCheck));
if(!checkBoxList.isEmpty()) {
Assert.fail();
}
A solution is:
// Find all checked checkboxes with xpath
List<WebElement> checkBoxList=driver.findElements(By.xpath("//input[#type='checkbox' and #checked='checked']"));
for(WebElement checkBox:checkBoxList)
{
checkBox.click();
}
// Assert if any checkbox left checked
List<WebElement> allCheckedBoxList=driver.findElements(By.xpath("//input[#type='checkbox' and #checked='checked']"));
if(!allCheckedBoxList.isEmpty()) {
Assert.fail();
}
Update
If you need to look for unchecked checkboxes use following xpath instead:
//input[#type='checkbox' and not(#checked='checked')]

Resources