Selenium : Dynamic Table - java

driver.get("https://www.leafground.com/dynamicgrid.xhtml");
//count column
List<WebElement> column = driver.findElements(By.tagName("th"));
System.out.println(column.size());
//row
List<WebElement> row = driver.findElements(By.tagName("tr"));
System.out.println(row.size()/2);
//return value of a customer
String text = driver.findElement(By.xpath("//td[normalize-space()='Costa Jennifer']//td[3]")).getText();
System.out.println(text);
What I'm trying to do is to get the activity value for Costa Jennifer value. But I'm getting:
Unable to locate the element.

You need to improve your locators.
This will give you the table rows:
List<WebElement> rows = driver.findElements(By.xpath("//tbody//tr[#role='row']"));
System.out.println(rows.size());
To get activity value of some user you can locate the row by user name and then locate proper td cell value. As following:
String activity = driver.findElement(By.xpath("//tr[contains(.,'Munro Leon')]//td[4]")).getText();
System.out.println(activity);

Related

how to find sub tagname by using By.tagName in selenium?

i am struggling with find sub tag name
i want to find "ankit patel" name from using below code
enter image description here
i am writing my code for as below
List<WebElement> rows= driver.findElements(By.id("ui-id-2") ); //Printing the size of the rows
List<WebElement> lirowsx = rows.findElements(By.tagName("li"));
System.out.print(lirowsx .size() +"size ");
int s=lirowsx.size();
when i am trying to access "ankit patel" using "li" tag then it returns and error message
like
Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: element not interactable
(Session info: chrome=80.0.3987.132)
i have also tried with xpath but didn't get result.
can you please guide me how can i solve this ?
You can find the element by first finding it by xpath and then fetching its text.
You can do it like:
WebElement element = driver.findElement(By.xpath("//li[#class='ui-menu-item']//strong"));
// Fetching the text
String text = element.getText();
If the li element id is unique then
WebElement element = driver.findElement(By.xpath("//li[#id='ui-id-11']//strong"));
or
WebElement element = driver.findElement(By.xpath("//li[#id='ui-id-11']/div/div/strong"));
and to get the text
String text = element.getText();
Feel free to post any more errors you see...

Iterating on the table using Selenium

I don't understand why x-path doesn't working in table. I'm trying to iter table from the website I found xpath but Eclipse told me:
"Unable to find element with xpath".
Should I find xpath in tables another way?
Website URL: https://www.oferty.net/mieszkania/szukaj?ps%5Blocation%5D%5Btype%5D=1&ps%5Btype%5D=1&ps%5Btransaction%5D=1&ps%5Blocation%5D%5Btext%5D=dolno%C5%9Bl%C4%85skie
I try to read one offer for example:
String urlwyniki ="https://www.oferty.net/mieszkania/szukaj?ps%5Blocation%5D%5Btype%5D=1&ps%5Btype%5D=1&ps%5Btransaction%5D=1&ps%5Blocation%5D%5Btext%5D=dolno%C5%9Bl%C4%85skie";
driver.get(urlwyniki);
String xpathResult = "//html/body/div[2]/div[4]/div[2]/div[2]/div/div[2]/div[5]/table/tbody/tr[5]/td";
String sCellValue = driver.findElement(By.xpath(xpathResult)).getText();
System.out.print(sCellValue);
What did I do wrong?
Find the below code to iterate all cell value in the table. It may help you.
String urlwyniki ="https://www.oferty.net/mieszkania/szukaj?ps%5Blocation%5D%5Btype%5D=1&ps%5Btype%5D=1&ps%5Btransaction%5D=1&ps%5Blocation%5D%5Btext%5D=dolno%C5%9Bl%C4%85skie";
driver.get(urlwyniki);
String xpathResult = "//table/tbody/tr/td";
List<WebElement> rows = driver.findElements(By.xpath(xpathResult));
List<WebElement> cols=new ArrayList<WebElement>();
for(int i=0;i<rows.size();i++){
System.out.print("Row value: "+i);
cols=rows.get(i).findElements(By.tagName("td"));
for(WebElement col:cols)
System.out.print("cell value "+col.getText());
}

Dynamically Generate CSS Selector From Table Value

We have a website that is built upon a ton of tables. Each cell within the rows is clickable. I am working on a way to dynamically build the cssSelector info by providing the table name and value I want to click on. I am getting close (I think).
Using the practice table at ToolsQA, say I want to build the cssSelector for the value "Taiwan".
It's cssSelector is: .tsc_table_s13 > tbody:nth-child(4) > tr:nth-child(3) > td:nth-child(2)
I am iterating through the table and have successfully been able to enter the cell using the value ("Taiwan") I specified, however, I'm not sure how to get the value of the row and column it is currently on.
Here is the code I am using so far:
driver.get("http://toolsqa.com/automation-practice-table/");
String table = ".tsc_table_s13 > tbody:nth-child(4)";
String cellValue = "Taiwan";
getCell(table, cellValue);
// Get the cell of a particular value
public static void getCell(String table, String value) throws IOException{
// Grab the table
WebElement tableName = driver.findElement(By.cssSelector(table));
// Now get all the TR elements from the table
List<WebElement> allRows = tableName.findElements(By.tagName("tr"));
// And iterate over them, getting the cells
for (WebElement row : allRows) {
List<WebElement> cells = row.findElements(By.tagName("td"));
// Print the contents of each cell
for (WebElement cell : cells) {
// System.out.println(cell.getText());
if (cell.getText().equals(value))
{
String cellValue = table + " > tr:nth-child(" + row. + ") > td:nth-child(" + cell + ")";
System.out.println(cellValue);
} // end if text equals
} // end for loop for cells
} // end for loop for all rows
} // end getCell function
So here's a quick example of how you can use XPath to find text in a table and get a reference to the element. You provide the text to search for and it gets inserted into an XPath. That XPath below, searches for a TD that contains that text. This is just a simple case. If you have a lot of repetitive text in your table, you'll have to post some examples so that I can update the code to take that into account.
String searchText = "China";
driver.get("http://toolsqa.com/automation-practice-table/");
WebElement e = driver.findElement(By.xpath("//td[text()='" + searchText + "']"));
System.out.println(e.getText()); // you can get the text in the cell
System.out.println(e.getAttribute("outerHTML")); // you can get the HTML of the TD
e.click(); // you can click the element also but in this case it won't do anything since it's just a TD with text

Retrieve data from a field using selenium

I am a begineer in Automation Testing(using Selenium, JavaLang N Eclipse).
I am trying to fetch few values from a webpage and store them in an excel.
The input values are taken from excel and the output will be stored in an excel.
The problem currently i am facing is, let me tell you with an example.
Firstly, it takes account number say 123 from excel and inputs in to search field and then selects the appropriate account and fetches name and email.
Secondly the next account number say 234 is fetched from excel and inputs in to search field and then it gets the appropriate name and email of that account(234).
I am trying with just two accounts as if now.
So when i see the ouput or rather opens the excel file i only get the last account owner name and number in both the columns.
The result i needed is First account name and email in one column and second account name and email in another column.
I request all the folks to help me out with this!!.
Thanks for looking in to this..
int rc= ws.getRows();
Below is the code:
WebElement s1=driver.findElement(By.xpath("//table[#id='kInfo']/tbody/tr[10]/td"));
//WebElement s1=driver.findElement(By.xpath("//td[#class='tRight']/a"));
String s2=s1.getText();
System.out.println("data:"+s2);
String D1= driver.findElement(By.xpath("//div[#class='vcard clearfix']")).getText();
System.out.println(D1);
Thread.sleep(1000);
/*driver.findElement(By.xpath("//*[#id='homePage']")).click();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
Thread.sleep(1000);*/
WritableWorkbook wb1 = Workbook.createWorkbook( new File("C:\\Bhasker-DNT\\Excel\\ouput.xls"));
WritableSheet ws1 = wb1.createSheet("customsheet", 1);
{
for(int count=0;count<4;count++)
{
for(int i=0;i<rc-1;i++)
{
Label label = new Label(i, 0, s2);
ws1.addCell(label);
Label label1 = new Label(i, 1, D1);
ws1.addCell(label1);
}
}
//Label label = new Label(0, 0, s2);
//ws1.addCell(label);
}
wb1.write();
wb1.close();
To fetch element for a div or a selection or a p you need the xpath i suppose you have it.
Than
String name = ""
List<WebElement> your_list= driver.findElements(By.xpath("xpath_stuff or workbook.get(I)"));
for (int i=0; i<your_list.size(); i++) {
// System.out.println(i+ ". " + your_list.get(i).getText());
// here you can write the elements to excel.
//if you need first name you need to just
if(i==(you know which is the first row that conatins the name)
{
name = your_list.get(i).getText();
}
... the same way the email and the other
ws1.addCell(your_list.get(i).getText());
}
wb1.close();
I hope it helps, if not please provide us some example from the html and the from excel.

I can't click a link in a dynamic table cell in Selenium WebDriver using Java

My project has one dynamic table. I need to go to a particular cell and click on an available link. I had reached a particular cell but am unable to click on the link which appears in the table cell.
#Test(priority = 1)
public void projectDelete() throws Exception {
int rowCount = -1;
int columnCount = 0;
WebElement table = webdriver.findElement(By.id("projectList"));
List<WebElement> allRows = table.findElements(By.tagName("tr"));
for (WebElement row : allRows) {
rowCount++;
List<WebElement> rowCells = row.findElements(By.tagName("td"));
for (WebElement cell : rowCells) {
columnCount++;
String projectName = cell.getText();
if (projectName.equals("TEST1")) {
System.out.println("Table Data" + cell.getText());
System.out.println("Table Row " + rowCount);
System.out.println("TEST PROJECT LINE FOUND ..... "
+ rowCount);
webdriver.findElement(By.xpath("//*[#id='projectList']/tbody/tr[rowCount]/td[5]")).click();
webdriver.findElement(By.xpath("//*[#id='493']")).click();
}
}
columnCount = 0;
}
}
Output:
Table DataTEST1
Table Row 76
TEST PROJECT LINE FOUND ..... 76
FAILED: projectDelete
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//*[#id='projectList']/tbody/tr[rowCount]/td[5]"}
Command duration or timeout: 20.06 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
On a single pass through your code, I found the following issue
Change the following
webdriver.findElement(By.xpath("//*[#id='projectList']/tbody/tr[rowCount]/td[5]")).click();
to
webdriver.findElement(By.xpath("//*[#id='projectList']/tbody/tr["+rowCount+"]/td[5]")).click();
On second thoughts, I have some suggestions on your code :-
Instead of finding the table as a whole, you can find the tbody since your data under test lies here.
I believe, you know in which column the project name. So you can avoid the loop for iterating through the List rowCells. Instead you can directly use rowCells.get(index) to get the exact column(index starts from 0. If your project name is in column 2, then index =1).
The same is applicable for the column which contains the link to click(). Use rowCell.get(index) to get the column and then click on it.
So your code can be modified as follows :-
#Test(priority = 1)
public void projectDelete() throws Exception {
//find tbody
WebElement table = webdriver.findElement(By.xpath("/table[#id='projectList']/tbody"));
//get all rows
List<WebElement> allRows = table.findElements(By.tagName("tr"));
//iterate through the rows
for (WebElement row : allRows) {
//get the rowCells in each row
List<WebElement> rowCells = row.findElements(By.tagName("td"));
//get the column which contains the project name and get text
String projectName = rowCells.get(indexofColumnwhichhasProjectname).getText();
//Compare if the project name equals TEST1
if (projectName.equals("TEST1")) {
System.out.println("Table Data : " + projectName);
System.out.println("Table Row : " + rowCells.indexOf(projectName));
//get the column containing the link and click on it.
rowCells.get(4).click();
//webdriver.findElement(By.id("493")).click();
//Img is contained within the row containing the project Name
//So, find the Img in the row and click
row.findElements(By.cssSelector("img[alt='Delete Project']")).click(); }
}
}
Let me know if this helps you.

Categories