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
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 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
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
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
I am trying to find a particular "Incident Number" from the table that is visible in the data grid that is generated dynamically. How can I access this?
Attached is the table view of the page from where I'm trying to see if the Incident number is present in the table or not.
Here is the html code, where I'm trying to access verify if the highlighted Incident number is present in this page or not,
We have to iterate through the data grid rows to find out the Incident Number expected i.e INC000006300863. Once we find out the incident number, we will store the row number and click on the check-box present on the same row. Coding will look like something below:
String xpath_rows = "//div[#class='ngCanvas']//div[contains(#class, 'Row')]";
String expectedIncidentNumber = "INC000006300863";
int numberOfRows = driver.findElements(By.xpath(xpath_rows)).size();
int matchedRow;
// Find out row number for **INC000006300863** incident number
for(int i=1; i<=numberOfRows; i++)
{
if(driver.findElement(By.xpath(xpath_rows +"[" +i +"]//a[contains(text(), 'INC')]")).getText().equals(expectedIncidentNumber))
{
matchedRow = i;
break;
}
}
// Click on the checkbox present on the matched row
driver.findElement(By.xpath(xpath_rows +"[" +i +"]//input[#type='checkbox']")).click();
Hope it helps!