Skip to main content

Posts

Showing posts from April, 2017

Display a Hidden Button(Component) in Selenium using Java

In some cases if a button is hidden on a web page. We can enable or display that button using following method: Here is the HTML code represents the button is hidden on web page. input type="submit" name="HiddenButton" value="Download" onclick="return checknextpage();" language="javascript" id="HiddenButton" class="button" style="width:100px;display:none;" Here is the java code to display or enable that button:             JavascriptExecutor js = (JavascriptExecutor) driver;    WebElement element = driver.findElement(By.id("HiddenButton"));    js.executeScript("arguments[0].setAttribute('style', 'width:100px')",element);

Switch to window in Selenium using Java

If you want to close the current active window and pass the control to another window using selenium. Below is the java code that will work for you. String currentWindow = driver.getWindowHandle();         // switch to first window that is not equal to the current window         String newWindow = null;         for ( String handle : driver.getWindowHandles()) {             if (!currentWindow.equals(handle)) {                 newWindow = handle;                 break;             }         }         // if there's another window found...         if (newWindow !=...

Read a PDF file and write the content of PDF into text file

Program to read a PDF file and write the content of PDF into text file using itext 5.3.5 library. Here Each page of a PDF is written to a separate text file. Ex. First page of PDF is written to first txt file. Second page of a PDF is written to second text file and so on. First you need to download the library then import it into your project. Here is the source code. Enjoy programming!!! import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.pdf.parser.PdfTextExtractor; /**  * This class is used to read an existing  *  pdf file using iText jar.  * @author javawithease  */ public class PDFReadExample {     public static void main(String args[]){                 BufferedWriter bw = null;         FileWriter fw = null;     ...

Create PDF reports in Selenium and Java

Sometimes we need to create PDF reports as we do crystal reports in .Net. Same thing can be achieved through iText library. Following program creates a PDF file and adds a text into it. In order to achieve this you need to download the iText library and import it's jar file into your program. Here is the link to download: iText JAR import java.io.FileOutputStream; import com.itextpdf.text.Document; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; public class iTextPDFDemo { public void iTextPDF() throws Exception{ String FILE = "D:/MyiText.pdf"; Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(FILE)); document.open(); document.add(new Paragraph("Now I can be able to write into the world of PDF ")); document.close(); } public static void main(String args[]){ iTextPDFDemo get = new iTextPDFDemo(); try { get.iTextPDF(); } catch (Exce...