DWQA Ask Questionselenium
Kulkarni Arundhati asked 6 years ago

The link in the multiple mails inside one mail write the link in the excel and open in the browser how to get that link and write it in the excel and put in to the browser from excel?

Kulkarni Arundhati replied 6 years ago

package BatchExecution;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class Linkwrite {

WebDriver driver;

@Test
public void f_login() throws InterruptedException, IOException, IOException {
System.setProperty(“webdriver.chrome.driver”,”E:\\chromedriver\\chromedriver.exe”);
driver = new ChromeDriver();
//Driver.get(“http://temp.clidiem.com/”);
//driver.get(“http://gmail.com/”);
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
driver.get(“http://gmail.com/”);
Thread.sleep(3000);
driver.findElement(By.id(“identifierId”)).sendKeys(“email@gmail.com”);

driver.findElement(By.xpath(“//span[@class=’RveJvd snByac’]”)).click();;

driver.findElement(By.name(“password”)).sendKeys(“passwrd”);

Thread.sleep(3000);

// some optional actions for reaching gmail inbox
//driver.findElement(By.xpath(“//*[@title=’Google apps’]”)).click();
//driver.findElement(By.id(“gb23”)).click();

driver.findElement(By.xpath(“//span[@class=’RveJvd snByac’]”)).click();
Thread.sleep(5000);
//int index;
//driver.findElement(By.xpath(“//div[@class=’Cp’]//div[1]//table[1]//tbody[1]//tr[1]//td[5]//div[2]//span[@class=’yP’]”)).click();
List readmail = driver.findElements(By.xpath(“//*[@class=’yP’]”));
//readmail.get(0).click();

String MyMailer = “no-reply”;

for(int i=0;i<readmail.size();i++){

if(readmail.get(i).isDisplayed()==true){

if(readmail.get(i).getText().equals(MyMailer)){

System.out.println("yes got mail" + MyMailer);
readmail.get(i).click();
System.out.println("clicked on the mail mail opens");
break;
}else{
System.out.println("No Mail from " + MyMailer);
}
}
}

Thread.sleep(30000);

//FInd email with Subject as "TestingCascadeEmailLink" and click on it

List o_email_list = driver.findElements(By.xpath(“//span[@class=’bog’]”));

for (WebElement element:o_email_list){

if(element.getText().equalsIgnoreCase(“TestingCascadeEmailLink”)){

element.click();

}

}

Thread.sleep(5000);

//find all the containers with class as ‘gs’

List o_col = driver.findElements(By.xpath(“//div[@class=’gs’]”));

//Find the size of the collection

int i_size = o_col.size();

System.out.println(“Total elements in cascaded email: ” + i_size);

//or if your email is collapsed then click on three dotted line image first and then above line

driver.findElement(By.xpath(“//img[@class=’ajT’]”)).click();

o_col.get(i_size-1).findElement(By.tagName(“a”)).click();

try (FileOutputStream outputStream = new FileOutputStream(“WriteLink.xlsx”)) {

XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet(“WriteLink”);
XSSFRow row1 = sheet.createRow(0);
XSSFCell cell1 = row1.createCell(0);
String str = o_col.get(i_size-1).findElement(By.tagName(“a”)).getText();
System.out.println(“link = ” + o_col);

cell1.setCellValue(str);

workbook.write(outputStream);
outputStream.flush();
outputStream.close();
} catch (FileNotFoundException e) {
System.out.println(“File not there”);
} catch (IOException e) {
e.printStackTrace();
}

}
}