Selenium Java Interview Questions and Answers Part-19
1) Why do you get NoSuchElementException ?
NoSuchElementException is thrown when the element is not present in the page.
2) Write syntax for switching to default content after switching to any frame?
driver.switchTo().defaultContent();
3) Write syntax for actions class?
Actions actions = new Actions(driver); actions.moveToElement(element).click().build().perform();
4) Write syntax for drag & drop?
WebElement toElement = driver.findElement(by.id("area1")); WebElement fromElement = driver.findElement(by.id("area2")); Actions actions = new Actions(driver); actions.dragAndDrop(fromElement,toElement).build().perform();
5) Can we use implicitly wait() and explicitly wait() together in the test case?
Yes we can use implicitly and explicitly wait together in the same test.
6) What Is the syntax to get value from text box and store It In variable.?
String text = driver.findElement(by.id("textbox")).getAttribute("value");
7) How to shoot the snapshot using selenium?
TakeScreenshot screenshot = ((TakeScreenshot)driver); File src = screenshot.getScreenshotAs(OutputType.FILE); File dest = new File(file1); FileUtils.copyFile(src,dest);
8) How to handle Confirmation Pop-Up?
We can use different methods of Alert interface in WebDriver:
//To click on the cancel button driver.switchTo().alert().dismiss(); //To click on OK button driver.switchTo().alert().accept();
9) How do you handle untrusted SSL certificate in Selenium WebDriver?
DesiredCapabilities capability = DesiredCapabilities.chrome(); capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true); WebDriver driver = new ChromeDriver(capability);
10) What is Select Class in Selenium WebDriver and how to use it?
Select class is used to interact with select dropdowns on the webpage.
WebElement element = driver.findElement(by.id("country")); Select select = new Select(driver); select.selectByVisibleText(element);
11) What is Alert interface and how to use it?
Alert interface is used to handle alert dialog boxes in Selenium WebDriver.
It provides the folowing methods:
– accept(): To click on the OK button
– dismiss(): To click on the CANCEL button
– getText(): To capture alert message
– sendKeys(): To send data to alert box
12) What is click() command in Selenium WebDriver?
click() command is used to click on any web element of the page.
13) What is sendKeys() command in Selenium WebDriver?
sendKeys() command is used to type a key sequence in DOM element.
15) How to read data from properties file in Selenium?
FileReader file = new FileReader("app.properties"); Properties props = new Properties(); String url = props.load(file).getProperty("baseURL");
16) How to automate a scroll bar?
JavaScriptExecutor js = (JavaScriptExecutor)driver; js.executeScript("window.scrollBy(0,200)");
17) If an explicit wait is 10 sec and the condition is met in 5 sec, will
driver move to execute next statement after 5 sec or it will wait for complete 10 sec then move?
Driver will move to next statement if the condition is met within 5 seconds and will not wait
for 10 seconds.
18) If element is loaded by taking much time, how to handle this situation in selenium?
We can use explicit or fluent wait methods to explicitly wait for the element.
19) What is the problem with Thread.Sleep in code?
Thread.Sleep will always wait for the defined amount of time and increases the overall execution time.
20) How to verify whether the background color of a paragraph is green or not?
WebElement element = driver.findElement(By.id("para")); element.getCssValue("background-color");
21) How to change the URL on a webpage using selenium web driver?
We can use the navigate method to change the URL.
driver.navigateTo().url("https://qascript.com");
22) Write a program to return the number of rows and columns in a webtable?
List<WebElement> rows = driver.findElements(By.xpath("//*[@class='table']/tbody/tr")); System.out.println(rows.size()); List<WebElement> cols = driver.findElements(By.xpath("//*[@class='table']/tbody/tr[1]/td")); System.out.println(cols.size());
23) Write a program to return the row and column value like(3,4) for a given data in web table?
String val = driver.findElement(by.xpath("//*[@id='table']/tbody/tr[3]/td[4]")).getText();
24) Action is class or interface?
Action is an interface which represents a single user interaction action.
25) How do you handle exception handling in selenium?
We can handle exceptions using try catch blocks.
Next Steps:
> More interview questions and answers on Selenium Java, continue to the next post (Click on Next Post link below)
> Check complete Selenium Java interview questions and answers here (Click here)
Please leave your questions/comments/feedback below.
Happy Learning ?
Connect to me on Linked In (Click here)
On a mission to contribute to the Software Testing Community in all possible ways.