Module17:Gmail open first Unread Mail: tr and tr[1] | Selenium Forum
M
Posted on 12/08/2016
Hi,

I was writing code to read the first Unread Mail from Gmail Account. I noticed something strange when using the tr with [] and without []
//table/tbody/tr[1]/td[6]/div/div/div[2]/span/b


As per my knowledge, if we don't use [1] after tr, it still considers as first tr. So, the above should give same output as with the below:
//table/tbody/tr/td[6]/div/div/div[2]/span/b

But using my code, when I do not use [1] with tr, code lists all 50 Subjects on the page.
When i use [1] with tr, code lists the first subject on the page.

Could you please explain me about this different results? Below is the complete code:

Note: I have commented out the lines without using []. Please uncomment.

package exercises;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class GmailUnread {

public static void main(String[] args) throws Exception {

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.navigate().to("http://www.gmail.com");

driver.findElement(By.xpath("//*[@id='Email']")).sendKeys("*********");
driver.findElement(By.xpath("//*[@id='next']")).click();

Thread.sleep(1000);

driver.findElement(By.xpath("//*[@id='Passwd']")).sendKeys("********");
driver.findElement(By.xpath("//*[@id='signIn']")).click();

Thread.sleep(1000);

driver.findElement(By.xpath("//input[@type='text'][@aria-label='Search']")).sendKeys("label:(unread inbox)");
//driver.findElement(By.xpath("//button[aria-label='Search Gmail']")).click();
driver.findElement(By.xpath("//input[@type='text'][@aria-label='Search']")).sendKeys(Keys.ENTER);


Thread.sleep(1000);


/*
//To get the Subject of all the Unread mails on the page. 50 are listed for each page in Gmail
//Notice the tr doesn't have any []
List<WebElement> unreadList = driver.findElements(By.xpath("//table/tbody/tr/td[6]/div/div/div[2]/span/b"));

System.out.println("Size is "+ unreadList.size());

for(int i=0;i<unreadList.size();i++)
System.out.println(unreadList.get(i).getText());

*/

//To click on the first Unread mail, insert [1] to tr which clicks on the first row

driver.findElement(By.xpath("//table/tbody/tr[1]/td[6]/div/div/div[2]/span/b")).click();

Thread.sleep(1000);

System.out.println("First Unread mail is Successfully opened");
}

}

M
Replied on 13/08/2016

When you find table elements (i.e. tr, td), and you don't specify the index, then selenium will fetch all corresponding elements from the table.
That said, when u don't specify tr index, then it will fetch records from all rows. If you don't specify td index then it will fetch records from all columns in a row. If you don't specify index for both tr and td, then it will fetch records for all rows and all columns.