DWQA Ask QuestionCategory: QuestionsAssignment-Level_Medium: Web Table Handling. Search rows with cell text.
admin Staff asked 3 years ago
  1. Navigate to URL: http://w2ui.com/web/demo/grid
  2. Search for Text ‘Ben’  and Print the rows number of each row where it has been found.
  3. Code should work for any text
6 Answers
Rahul Staff answered 3 years ago
Start your code here

public class Search_Text
{
static WebDriver driver;

public static void main(String[] args)
{
System.setProperty("webdriver.chrome.driver", "D:\\Seleniumm\\All_Drivers\\chromedriver.exe");
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://w2ui.com/web/demo/grid");
search_by_name("Ben");

}

public static void search_by_name(String name)
{
String lstnm1="";
String lstnm="";
WebElement nm1 = driver.findElement(By.xpath("//*[text()='"+name+"']"));

lstnm1 = nm1.getText();
//System.out.println("The list of names are: " +lstnm1);
List<WebElement> nm = driver.findElements(By.xpath("//*[text()='"+name+"']//preceding::div[2]"));
for(int j=0;j<nm.size();j++)
{
lstnm = nm.get(j).getText();

System.out.println("The list of id and names are: "+lstnm+" " +lstnm1);
}


}


}
admin Staff replied 3 years ago

Good. Write another logic, with out using XPATH. i.e. iterate through each cell of web table and print the row number.

Divyesh Kota Staff answered 3 years ago
Start your code here

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class Assignment2 {

public static void main(String[]args) {


System.setProperty("webdriver.chrome.driver", "G:\\Selenium\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("http://w2ui.com/web/demo/grid");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);


List<WebElement> Table_Name=driver.findElements(By.xpath("//div[@title='John'][contains(text(),'John')] "));

List<WebElement> Table_Data=driver.findElements(By.xpath("//div[@title='John'][contains(text(),'John')] //preceding::div[2] "));

for(WebElement TD:Table_Data) {

System.out.println(TD.getText());
}


for(WebElement TN: Table_Name)
{
System.out.println(TN.getText());
}
}
}

admin Staff replied 3 years ago

I checked ur xpath; they are not giving any results. Wrong solution. Check again and correct it.

Divyesh Kota Staff answered 3 years ago
Start your code here

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class Assignment2 {

public static void main(String[]args) {


System.setProperty("webdriver.chrome.driver", "G:\\Selenium\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("http://w2ui.com/web/demo/grid");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

String F_Name="John";


List<WebElement> Table_Name=driver.findElements(By.xpath("//div[@title='"+F_Name+"'] "));

List<WebElement> Table_Data=driver.findElements(By.xpath("//div[@title='"+F_Name+"'] //preceding::div[2] "));

for(WebElement TD:Table_Data) {

System.out.println(TD.getText());
}


for(WebElement TN: Table_Name)
{
System.out.println(TN.getText());
}
}
}

Nikita dumbhare Staff answered 3 years ago
package SeleniumPck;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;

public class tableRowPrint {

static WebDriver driver;

@BeforeMethod

public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://w2ui.com/web/demo/grid");
}


@Test
public void fetchTableData() {
WebElement firstname = driver.findElement(By.xpath("//div[@title='John']")); //for John
String name =firstname.getText();
System.out.println("Name:"+name);

String col_data =null; //Local Variable
String cell_record =null;


//********************************Fetch 3rd column(First name) ******************************************//
List<WebElement> Data =driver.findElements(By.xpath("//div[@id='grid_grid_records']//table//tbody//tr[@class='w2ui-odd' or @class='w2ui-even' ]//td[@col='2']"));
System.out.println("Total No of Records are : " + Data.size());

for(int j=0;j<Data.size();j++)
{
col_data =Data.get(j).getText();
System.out.println("First Name : "+col_data);
}
System.out.println("");


//****************************Print Particular Data set*******************************//


List<WebElement> Record =driver.findElements(By.xpath("//div[@id='grid_grid_records']//table//tbody//tr[@class='w2ui-odd' or @class='w2ui-even' ]"));

System.out.println("Cell Record"+Record.size());
for(int i=0;i<Record.size();i++)
{
cell_record =Record.get(i).getText();
//System.out.println("Records:"+cell_record); //For Printing Total RecordsSet

if (cell_record.contains(name)){

System.out.println("Cell Record:"+cell_record);
}
else{
System.out.println("John is not Present in RecordSet");
}




}
System.out.println("");




}



@AfterMethod
public void afterMethod() {
}

}
Bindiya Patil Staff answered 3 years ago

public class Search_Text {
static WebDriver driver;
public static void main(String[] args) throws InterruptedException {
System.setProperty(“webdriver.chrome.driver”, “E:\\chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(“http://w2ui.com/web/demo/grid&#8221;);
Search_by_name(“Susan”);
}
public static void Search_by_name(String name) throws InterruptedException
{
String name1;
String name2;
WebElement FstName=driver.findElement(By.xpath(“//div[text()='”+name+”‘]”));
name1=FstName.getText();
Thread.sleep(3000);
List<WebElement>FstName1=driver.findElements(By.xpath(“//div[text()='”+name+”‘]//preceding :: div[2]”));
for(int i=0;i<FstName1.size();i++)
{
name2=FstName1.get(i).getText();
System.out.println(“the name and id is:” +name1+” ” +name2);
}
}
}

Nitu A Staff answered 3 years ago

public class GridTable {
static String firstname= “john”;
public static void main(String[] args)throws Exception{
System.setProperty(“webdriver.chrome.driver”, “C:\\Users\\HP\\Downloads\\selenium folder\\chromedriver.exe”);
WebDriver driver=new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
driver.get(“http://w2ui.com/web/demo/grid&#8221;);
List<WebElement> data=driver.findElements(By.xpath(“//tr[@onclick] “));
System.out.println(“data=”+ data.size());
WebElement firstname = driver.findElement(By.xpath(“//div[@title=’John’]”)); String name =firstname.getText();
System.out.println(“Name:”+name);
for( int i=0;i<=data.size();i++)
{

String temp =data.get(i).getText();

if(temp.contains(name))
{

System.out.println(“temp” + temp);
}
else {
System.out.println(“john is not present”);
}

}

}

}