Google search info cpying to word document | Selenium Forum
M
Posted on 03/11/2016
Hi Ashish,
I am trying to search a word in google and copy that into the wordpad but while I am copying to word document it was replacing old data with new data.so how should I append the data without loosing the old one.
package Googlewords;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.concurrent.TimeUnit;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class googlewords {
static WebDriver driver;
public static void main(String[] args) throws AWTException, InterruptedException, IOException {
driver=new FirefoxDriver();
//System.setProperty("webdriver.chrome.driver","C:\\Users\\Joe\\Downloads\\chromedriver_win32\\chromedriver.exe");
//driver=new ChromeDriver();

googleSearch("Mastiff");
googleSearch("Oblivious");
googleSearch("Orchestra");
googleSearch("Partridge");
googleSearch("Paisley");
googleSearch("Peculiar");
googleSearch("Pensive");
googleSearch("Pauper");

}
public static void googleSearch(String meaning) throws AWTException, InterruptedException, IOException{
try{
String path="C:\\write\\meaning.txt";
driver.get("https://www.google.com/");
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
driver.manage().window().maximize();
WebElement ele=driver.findElement(By.xpath("//div[@id='sfdiv']/div"));
ele.sendKeys(meaning+" definition");
Thread.sleep(2000);
ele.click();
ele.sendKeys(Keys.ENTER);
/*Robot robot=new Robot();
robot.keyPress(KeyEvent.VK_DOWN);
robot.keyPress(KeyEvent.VK_ENTER);*/
isElementPresent("//div[@class='lr_container mod']");
String text=driver.findElement(By.xpath("//div[@class='lr_container mod']")).getText();

System.out.println(text);
worddocument(text);
System.out.println("******************************************************************************************");

Thread.sleep(2000);
}catch(Exception e){
e.printStackTrace();
}
//File f=new File(path);
/*FileWriter fw=new FileWriter(f,true);
BufferedWriter bw=new BufferedWriter(fw);
bw.write(text);
bw.flush();
Thread.sleep(2000);
bw.newLine();*/
/*PrintWriter out=new PrintWriter(f);
out.println(text);
out.close();*/




}

public static boolean isElementPresent(String locatorKey){
int s=driver.findElements(By.xpath(locatorKey)).size();
if(s==0)
return false;
else
return true;
}
public static void worddocument(String text){

try {
File f=new File("C:\\Users\\Joe\\Desktop\\sanjay meanings.doc");
XWPFDocument document=new XWPFDocument();
FileOutputStream fos=new FileOutputStream(f,true);
XWPFParagraph paragraph=document.createParagraph();
XWPFRun run=paragraph.createRun();

run.setFontSize(12);
run.setText(text);
run.addCarriageReturn();
document.write(fos);

fos.flush();
fos.close();

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//.out.println("document created");


}
}

M
Replied on 08/11/2016

are you still having the problem?


M
Replied on 08/11/2016

Yes,could you please check at your end once and let me know how to append data to file.


M
Replied on 09/11/2016

i working on this for hours. this is the best i could find

http://stackoverflow.com/questions/21869991/apache-poi-word-documents-doc-docx-updating


M
Replied on 09/11/2016

Thank you