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
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?
Hi I just want to get the index number of TH column using unique Xpath. Like A loop will run and check on which TH column Xpath match and return the index number. Is there any way i can do this in selenium ? Till now im able to get the tag index and run the the loop but now I have check the on each TH with xpath rather its matched or not and give me the index number. Please let me know is there anything with Im able to achieve this with this logic or any other any Xpath techniques.
Table = driver.findElement(By.xpath("//table/thead[#class='ui-datatable-thead']"))
List<WebElement> rows_head = Table.findElements(By.tagName('th'))
int head_size= rows_head.size()
System.out.println(head_size);
for (int c = 1; c <= head_size; c++) {
driver.findElement(By.xpath(
"//th[4][#class='ui-state-default ui-unselectable-text ui-sortable-column']")
)
// Here is something I want loop will check the each TH with above given
// Xapth and return the TH index in the table on match TH xpath index.
}
Selenium doesn't supply an API to return the xpath of found element. So you can't archive your goal by comparing xpath.
But Selenium supply API to get the attribute value of found element, you can compare with it to see it's your desired element or not.
WebElement thead = driver.findElement(By.xpath("//table/thead[#class='ui-datatable-thead']"));
List<WebElement> heads = thead.findElements(By.tagName('th'));
int head_size= heads.size()
System.out.println(head_size);
String expectedClass = "ui-state-default ui-unselectable-text ui-sortable-column"
int i = 1;
for (; i <= head_size; i++) {
if(heads[i].getAttribute("class").equal(expectedClass))
// check attribute class value is as expect
// you can change to other attribute or check more than one attribute
break;
)
}
System.out.println("Matched th index: " + 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
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 manipulate this blocks of codes:
List<WebElement> more = driver.findElements(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[4]/button"));
if(more.size()!=0){
driver.findElement(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[4]/button")).click();
}else {
WebElement present = driver.findElement(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div/table/tbody"));
List<WebElement> list = present.findElements(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div/table/tbody/tr"));
System.out.println("Total Number of TR: " + list.size());
}
What Im trying to do hopefully is that before I execute what is in the IF statement I want to loop that everytime it sees the element /html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[4]/button it will be click and if it is not available then ill execute this:
WebElement present = driver.findElement(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div/table/tbody"));
List<WebElement> list = present.findElements(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div/table/tbody/tr"));
System.out.println("Total Number of TR: " + list.size());
For further info what I am trying to do here is this.
I am on a listview for a specific module, and then at the button there is a "Click here for more records" -> its XPath is /html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[4]/button
I want that if i am in the listview and that button is present as mentioned above -I want to click it. And if in the listview there is no button "Click here for more records" (for example the records is composed of 5records only so there is no pagination clearly) I want to execute a blocks of code.
What I understood from your query is that you want to loop your IF condition statement multiple times until your else condition satisfies. For this, you can try following code:
(1) try{
(2) String xpathVAL="/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[4]/button";
(3) int i=1;
(4) while(i!=0){
(5) if(driver.findElements(By.xpath(xpathVAL)).size() != 0)
(6) {
(7) driver.findElement(By.xpath(xpathVAL).click();
(8) Thread.sleep(2000);
(9) }
(10) else{
(11) Thread.sleep(2000);
(12) WebElement present = driver.findElement(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div/table/tbody"));
(13) List<WebElement> list = present.findElements(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div/table/tbody/tr"));
(14) LOGGER.debug("Total Number of TR: " + list.size());
(15) i=0;
(16) }
(17) }
(18) }catch(Exception e){
(19) LOGGER.debug("Exception Caught");
(20) WebElement present = driver.findElement(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div/table/tbody"));
List<WebElement> list = present.findElements(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div/table/tbody/tr"));
}
I think your requirement would be to keep click a button until some window appears and the button disappears. What I suggest for this is, apply "Thread.sleep(//some 3-4 seconds//)". However, it is poor style of coding and using forceful thread is not suggested by many standards. You can put some other implicit wait there. Also, if your element is not found after certain period of time, you can apply break condition after certain time interval, else it will go to infinite loop. If you are having trouble writing the code, let me know and I will help you in that.
EDIT
Try following points to remove element not present in cache:-
Use Explicit wait(Thread.sleep) at line 8 and line 11
Change your xpath from position based to some id/name/class type. Changing it to NAME based gets more clearer picture of the element even if its position is changed.
Again re-initialize the element if exception is caught at line 20.