Selenium Java Interview Questions and Answers Part-9: By: Bijan Patel - in
Selenium Java Interview Questions and Answers Part-9: By: Bijan Patel - in
Selenium Java Interview Questions and Answers Part-9: By: Bijan Patel - in
try{
driver.findElement(by.id("button")).click();
}
catch(NoSuchElementException e){
System.out.println("Element not present");
}
2) There are four browser windows opened and you don’t have any idea where the required element
is present. What will be your approach to find that element?
– use getWindowHandles() method to get Window handles of all browser windows
– use switchTo() method to switch to each browser window using the handle id
– Find the element in each browser window and close the window if not present
- dismiss()
driver.switchTo().alert().dismiss();
- accept()
driver.switchTo().alert().accept();
8) Give an example for method overloading concept that you have used in Selenium?
Implicit Wait in Selenium use method overloading as we can provide different Timestamp or TimeUnit
like SECONDS, MINUTES, etc.
9) How do you select a value from a drop-down field and what are the different methods available?
We can select value from drop-down using methods of Select class. Following are the methods:
– selectByVisibleText
– selectByValue
– selectByIndex
10) When your XPath is matching more than one element, how do you handle it to locate the required
element?
We can use index of the element to locate it or we can use different Xpath axes methods to locate the
element like Following, Ancestor, Child, Preceding or Following-sibling
11) How do you capture screen-shots in Selenium and what is the best place to have the screen-shot
code?
//Convert web driver object to TakeScreenshot
TakesScreenshot scrShot =((TakesScreenshot)webdriver);
//Call getScreenshotAs method to create image file
File SrcFile=scrShot.getScreenshotAs(OutputType.FILE);
//Move image file to new destination
File DestFile=new File(fileWithPath);
//Copy file at destination
FileUtils.copyFile(SrcFile, DestFile);
12) Write the code for connecting to Excel files and other operations?
XSSFWorkbook srcBook = new XSSFWorkbook("Demo.xlsx");
XSSFSheet sourceSheet = srcBook.getSheetAt(0);
int rownum=rowcounter;
XSSFRow sourceRow = sourceSheet.getRow(rownum);
XSSFCell cell1=sourceRow.getCell(0);
13) How do you read and write into a PDF file?
BufferedInputStream file = new BufferedInputStream("Path of PDF file");
PDFParser pdf = new PDFParser(file);
pdf.parse();
String text = new PDFTestStripper().getText(pdf.getPDDocument());
15) How do you debug your automation code when it is not working as expected?
– Add breakpoints on the lines of code where it is not working
– Use different actions like F7(Step Into), F8(Step Over), F9(Step Out) to debug the problem
16) What are the end methods you use for verifying whether the end result is achieved by our
Selenium automation scripts?
We can use different assertion methods available in different test frameworks like TestNG or Junit.
17) How do you clear the cookies of a browser using Selenium, before starting the execution?
driver.manage().deleteAllCookies();