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?
Related
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?
I am still learning xpath and I am trying to skip the first row of a table because the first row has no values. I am not having success i searched through stack over flow and could not find anyone with a similar issue. My code is below:
int reqIndex = 0;
do {
List<WebElement> payDates = driver.findElements(By.xpath("//tr[starts-with(#id,'changeStartWeekGrid_row_')]/td[contains(#id,'_cell_4')/span]"));
System.out.println(payDates);
for(WebElement pd:payDates) {
LocalDate localDate = LocalDate.now();
java.util.Date d = new SimpleDateFormat("yyyy-MM-dd").parse(localDate.toString());
String str = pd.getText();
if ( str >= (d) ){ // still figuring this out
driver.findElement(By.xpath( "//tr[#id='changeStartWeekGrid_row_'" + reqIndex +"])/TBODY[#id='changeStartWeekGrid_rows_tbody']/TR[7]/TD[1]/DIV[1]/DIV[1]/DIV[1]")).click();
break;
}else{
reqIndex++;
}
}
}while(reqIndex < 7 ); /// do this 7 times
Thread.sleep(10000);
This is the part i am trouble shooting right now
List<WebElement> payDates = driver.findElements(By.xpath("//tr[starts-with(#id,'changeStartWeekGrid_row_')]/td[contains(#id,'_cell_4')/span]"));
System.out.println(payDates);
Thread.sleep(10000);
The xpath works the problem is that it is selecting the first row and it has no values row zero does so it needs to skip the first row and go to row zero.
when i run the code i get this message:
( //tr[starts-with(#id,'changeStartWeekGrid_row_')]/td[contains(#id,'_cell_4')/span])
ilter expression must evaluate to nodeset.
the row highlighted is the row i am trying to skip
cell 4 image
-----radio button html code below
<div id="changeStartWeekGrid.store.rows[5].cells[0]_widget" tabindex="0" class="revitRadioButton dijitRadio" onfocus="var registry = require('dijit/registry'); registry.byId('changeStartWeekGrid').changeActiveCell('changeStartWeekGrid_row_5_cell_0', event);" onblur="var registry = require('dijit/registry'); registry.byId('changeStartWeekGrid').blurActiveCell('changeStartWeekGrid.store.rows[5]', 'changeStartWeekGrid.store.rows[5].cells[0]');"><div class="revitRadioButtonIcon"></div></div>
---radio button picture
Try to use XPath
//table[#id='changeStartWeekGrid_rows_table']//tr[preceding-sibling::tr]
to select all tr nodes except the firts one
You can also use position() as below:
//table[#id='changeStartWeekGrid_rows_table']//tr[position()>1]
Note that in XPath indexation starts from 1 and so [position()>1] predicate means return all sibling nodes skiping the first one
You may also use
//tr[starts-with(#id,'changeStartWeekGrid_row_') and not(starts-with(#id, 'changeStartWeekGrid_row_column'))]/td[5]/span
This is the code to click on 'Update icon' of a specific record from grid view using selenium webdriver (JAVA)
I have written the following code to click on 'Update icon' :-(This code is working fine)
//This will count total no of rows in the grid:-
List<WebElement> count=driver.findElements(By.xpath(".//*[#id='ctl00_ContentPlaceHolder1_gvCustomerDetails']/tbody/tr"));
int totalNoRecords=count.size();
System.out.println("total no of rows are "+totalNoRecords);
//This will find a particular record in a webtable like po name as selenium_testing11selenium_testing12
WebElement record=driver.findElement(By.xpath(".//*[#id='ctl00_ContentPlaceHolder1_gvCustomerDetails']/tbody/tr/following::td[text()='selenium_testing11']"));
String poName= record.getText();
//This will verify that if po name is equals to this then only click on Update icon else print the else message
if(poName.equals("selenium_testing11")){
WebElement updateBtn=driver.findElement(By.xpath(".//*[#id='ctl00_ContentPlaceHolder1_gvCustomerDetails']/tbody/tr/following::td[text()='selenium_testing11']/preceding-sibling::td//a[contains (#id,'btnUpdate')]"));
updateBtn.click();
}
else
System.out.println("Po does not exists");
String xpath = "//table[contains(#id, 'gvCustomerDetails')]" +
"//tr[#class][td[5][text()='%s']]/td[2]/a";
public void clickEditIcon(String po) {
driver.findElement(By.xpath(String.format(xpath, po))).click();
}
xpath explain:
//table[contains(#id, 'gvCustomerDetails')] to locator the table
//tr[#class] means only find rows which is not the header column row
[td[5][text()='%s']] is a qualifier on tr to say only find tr whose 5th
cell's text equal to the argument:po, td[5] means the column: PO
/td[2] means to find the 2th cell(update icon inside it) from matched row
/a means to find the update icon link inside matched cell
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