I want to automate the webtable in angular. Here's the site- http://demo.automationtesting.in/WebTable.html
I want to capture all the records of all rows. Here what I tried-
List<WebElement> wbt=drv.findElements(By.cssSelector("div[role='rowgroup'][class='ui-grid-viewport ng-isolate-scope']"));
System.out.println("size"+wbt.size());
for(int i=0; i<wbt.size();i++){
System.out.println("values-"+wbt.get(i).getText());
}
But the size coming out is 1.
I also tried with div[class='ui-grid-contents-wrapper'], but with no success.
You selector is pointing to the table div and to the rows. To get table rows you can use .ui-grid-row selector, that will give you table body rows.
List<WebElement> rows = drv.findElements(By.cssSelector(".ui-grid-row"));
for (WebElement row : rows) {
System.out.println("values-" + row.getText());
}
To get each column element by row, you can use .ui-grid-cell-contents selector inside the loop:
List<WebElement> rows = drv.findElements(By.cssSelector(".ui-grid-row"));
for (WebElement row : rows) {
columns = row.findElements(By.cssSelector(".ui-grid-cell-contents"));
columns.forEach(column -> System.out.println(column.getText().trim()));
}
You can learn about selectors here.
Also you may need to wait until some conditions is met, information you can get from: When to use explicit wait vs implicit wait in Selenium Webdriver?
Related
on my project there is dynamically loaded table so each time when we scroll down, table is updating and rowcount starts from 0 or 1 i guess. and i want to iterate through whole table and get specific cell's text from every row. how can i do that? please, help.
what i have done so far is:
List<WebElement> currentGridTableRowsList = driver.findElements(
By.xpath("//table[#role='grid'] //tbody/tr"));
//print current rows count
int currentGridTableRows = currentGridTableRowsList.size();
System.out.println(currentGridTableRows);
for (WebElement row : currentGridTableRowsList){
List<WebElement> tdCollection = row.findElements(By.tagName("td"));
for (WebElement td : tdCollection){
String cellText = td.getText();
System.out.println(cellText);
}
}
currentGridTableRowsList.size() returns current grid number of rows - 10 for example. But attribute aria-rowcount returns 30 rows.
I know there is a snippet
WebElement element = driver.findElement(By.xpath(“//table[#id = ‘tableID’]/tbody//tr[last()]”));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(“arguments[0].scrollIntoView();”, element);
but i don't know how to apply that, how to make correct for loops in Java language, so that when it is end of table (current grid), webdriver will scroll and when it is end of table (aria-rowcount) driver will quit. can you please tell me how to write correct for loop for the table and how to get specific cell value, for example td[2] of each row?
I want to find specific row in a mat-table, and I want to click a specific mat-icon. However I don't know how to use xpath in the case of mat-icon. My code is find the table, and iterate over the rows to find specific row. After that it should click on mat-icon in the last column. Maybe can I find it by its name? Here is my code:
public void clickIconFromSelectedRow(String text, String icon) {
WebElement baseTable = driver.findElement(By.xpath("//mat-table[#role='table']"));
List<WebElement> tableRows = baseTable.findElements(By.xpath("//mat-row[#role='row']"));
for (WebElement row : tableRows){
List<WebElement> cols = row.findElements(By.xpath("//mat-cell[#role='cell']"));
for (WebElement col : cols) {
if (col.getText().equals(text)) {
driver.findElement(By.xpath("SOME XPATH AND + text + TEXT")).click();
}
}
}
}
Here is the HTML for your information
Note: in the last column, there must be several mat-icons.
You need to consider a couple of things here:
You are constructing the list tableRows with respect to the WebElement baseTable. So within the xpath instead of By.xpath("//mat-row[#role='row']") you need By.xpath("./mat-row[#role='row']")
The text Test is actually within the descendent of the element //mat-cell[#role='cell'] so you may like to go deeper as ./mat-cell[#role='cell']/div/div[text()].
Your effective code block will be:
public void clickIconFromSelectedRow(String text, String icon) {
WebElement baseTable = driver.findElement(By.xpath("//mat-table[#role='table']"));
List<WebElement> tableRows = baseTable.findElements(By.xpath("./mat-row[#role='row']"));
for (WebElement row : tableRows){
List<WebElement> cols = row.findElements(By.xpath("./mat-cell[#role='cell']/div/div[text()]"));
for (WebElement col : cols) {
if (col.getText().equals(text)) {
driver.findElement(By.xpath("SOME XPATH AND + text + TEXT")).click();
}
}
}
}
Really, I think you are over-complex a simple problem using xPath. I I imagine you has a mat-table with a column like
<ng-container matColumnDef="position">
<th mat-header-cell *matHeaderCellDef> Actions</th>
<td mat-cell *matCellDef="let element;let i=dataIndex">
<button mat-icon-button (click)="doSomething(element,i)">
<mat-icon>more_vert</mat-icon>
</button>
</td>
</ng-container>
Why not search the row asking the dataSource and use?
this.doSomething(element,i)
I am trying to implement some code using Selenium Webdriver using Java.
Basically, I have a website with a text box. Once user enter the first letter, based on that a value will be displayed(using AJAX). I need to select the particular value, which i mentioned in send keys .
WebElement fromCity = driver.findElement(By.id("pickUpLocation"));
fromCity.sendKeys("A Ma Temple / 媽閣");
Thread.sleep(2000);
WebElement ajaxContainer1 = driver.findElement(By.className("txt-box ng-touched ng-dirty ng-valid"));
WebElement ajaxHolder1 = ajaxContainer1.findElement(By.tagName("ul"));
List<WebElement> ajaxValues1 = ajaxHolder1.findElements(By.tagName("li"));
for (WebElement value1 : ajaxValues1) {
if (value1.getText().equals("A Ma Temple ")) {
((WebElement)ajaxValues1).click();
break;
}
}
After you send keys.your Ajax value should be retrieved in a box related to you keyword search.You need to get the complete box.and fetch each one as you have done in for loop .get the text and compare it with your expected text and click where this condition is true.
What is that line for before thread.sleep()
I think u can try selecting through index. It should be like this
List<WebElement> ajaxValues1 = ajaxHolder1.findElements(By.tagName("li"));
Select dropdown= new Select(ajaxValues1);
dropdown.selectByIndex(0);
dropdown.selectByIndex(1);
dropdown.selectByIndex(2);
0 represents the first element in the dropdown. Based on index number of that element, feed the corresponding number in selectByIndex(0)
Let me know if this helps. Thanks
I am using selenium web driver to count the rows of a web table. This is my code for that part:
List<WebElement> rows = driver.findElements(By.xpath("//div[#class='z-listbox-body']/table/tbody/tr")); totalrows = rows.size();
It doesn't output the expected result. Can anyone tell me why?
You should select the <tbody> first, store the element, find all <tr>s in the current element and then you should get your value by getting the length of the list obtained:
WebElement tbody = driver.findElement(By.xpath("//div[#class='z-listbox-body']/table/tbody"));
List<WebElement> rows = tbody.findElements(By.xpath("tr"));
totalrows = rows.size();
Sorry if I did something wrong, but I'm using Selenium with Python, never with Java.
This is the Python code that is working:
tbody = browser.find_element_by_xpath("//div[#class='z-listbox-
body']/table/tbody")
rows = tbody.find_elements_by_xpath("tr")
totalrows = len(rows)
Note that when I'm selecting the tbody I am doing find_element_by_xpath so will get only the first element and store it in a normal variable and when selecting the rows I am doing find_elments_by_xpath so will get a list of elements.
In my html page there's a table with 10 rows; those rows will display based on filter (dynamically). Let's say, for example, without any filters by default 10 row will be returned.
after applying the filter, less than 10 rows will be returned, depending on the type of filter, so i want to get that dynamic count of table rows (after filter) using selenium web driver.
i tried with driver.findElements(By.xpath("abcd")).size() but this is giving default count 10; however, after applying the filter only 2 rows are appearing.
Please suggest how to get dynamic count (count=2 as appearing 2 rows in UI) .
To find the total number of elements on dynamic webpage we need to use driver.findElements().size() method. But sometimes it's not useful at all.
First get the size of all element matching with the row count. Once we have it then you can use dynamic xpath ie replace row and column number run time to get the data.
{
List<WebElement> rows_table = mytable.findElements(By.tagName("tr"));
//To calculate no of rows In table.
int rows_count = rows_table.size();
//Loop will execute till the last row of table.
for (int row=0; row<rows_count; row++){
//To locate columns(cells) of that specific row.
List<WebElement> Columns_row = rows_table.get(row).findElements(By.tagName("td"));
//To calculate no of columns(cells) In that specific row.
int columns_count = Columns_row.size();
System.out.println("Number of cells In Row "+row+" are "+columns_count);
//Loop will execute till the last cell of that specific row.
for (int column=0; column<columns_count; column++){
//To retrieve text from that specific cell.
String celtext = Columns_row.get(column).getText();
System.out.println("Cell Value Of row number "+row+" and column number "+column+" Is "+celtext);
}
System.out.println("--------------------------------------------------");
}
I'll prefix this with the fact that I'm not a Java dev but I use a lot of webdriver in other languages.
The best way to setup tests is to wait for the outcome of the action that your testing. Sometimes you can get away without doing waits, or timed waits, but then you go and run your tests on a slower grid box and everything falls in a heap.
Things like "div exists", "div has class", whatever your outcome may be. In your case, it sounds like you may not be able to test for a div to be rendered but you can probably use your size test as the outcome to wait for.
Selenium can use any ExpectedCondition or you can specify a function
WebDriverWait wait = new WebDriverWait(getDriver(), 5);
wait.until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
int elementCount = driver.findElement(By.xpath("xxxx")).size();
if (elementCount == 2)
return true;
else
return false;
}
});
Code from https://sqa.stackexchange.com/a/8701