Selenium Java Interview Questions and Answers Part-12
1) Can we test APIs or web services using Selenium WebDriver?
Selenium directly cannot test APIs or web services but we can use Java libraries like Rest Assured to do the same.
2) How can we locate an element by only partially matching its attributes value in XPath?
We can use contains() method in Xpath to partially match attribute value.
WebElement element = driver.findElement(By.xpath("//*[contains(@id,'sub')]"));
3) How can we locate elements using their text in XPath?
We can use the text() method in XPath to locate elements.
WebElement element = driver.findElement(By.xpath("//*[text(),'Selenium']));
4) How can we move to parent of an element using XPath?
We can use ancestor in Xpath to move to parent node of an element.
WebElement element = driver.findElement(By.xpath("//*[@id='login']/ancestor::div[@class='button']));
5) How can we move to nth child element using XPath?
We can use child axis in Selenium to find all the children of the current node and
then use index method to move to nth child element.
WebElement element = driver.findElements(By.xpath("//*[@id='login']/child::*")[2]);
6) What is the syntax of finding elements by class using CSS Selectors?
In Css Selector . represents a class identifier.
WebElement element = driver.findElement(By.CssSelector(".button"));
For a detailed view on all different locators in Selenium refer the following page – https://qascript.com/selenium-locators/
7) What is the syntax of finding elements by id using CSS Selectors?
In Css Selector # represents a class identifier.
WebElement element = driver.findElement(By.CssSelector("#login"));
For a detailed view on all different locators in Selenium refer the following page – https://qascript.com/selenium-locators/
8) How can we select elements by their attribute value using CSS Selector?
We need to provide the identifier type followed by the value in Css Selector to select an element.
9) How can we move to nth child element using CSS Selector?
driver.findElement(By.cssSelector("ul > li:nth-child(1)"));
10) How can we submit a form in Selenium?
We can submit a form using the submit() method in selenium.
driver.findElement(By.id("Login")).submit();
11) How can we fetch a text written over an element?
We can fetch text using the getText() method.
String text = driver.findElement(By.id("Login")).getText();
12) What are some expected conditions that can be used in Explicit Waits?
– elementToBeClickable()
– elementToBeSelected()
– presenceOfElementLocated()
– visiblityOfElementLocated()
13) How can we fetch the title of the page in Selenium?
Using getTitle() method.
String title = driver.getTitle();
For a detailed view on all Selenium methods refer the following page – https://qascript.com/selenium-webdriver-commands-cheat-sheet/
14) How can we fetch the page source in Selenium?
Using getPageSource() method.
String source = driver.getPageSource();
15) What are some randomly encountered exceptions in Selenium ?
– NoSuchElementException
– NoSuchWindowException
– NoSuchFrameException
– NoAlertPresentException
– InvalidSelectorException
– ElementNotVisibleException
– ElementNotSelectableException
– TimeoutException
– NoSuchSessionException
– StaleElementReferenceException
16) How to check which option in the drop-down is selected?
Using getFirstSelectedOption() method.
Select select = new Select(driver.findElement(By.xpath("//select"))); WebElement option = select.getFirstSelectedOption(); String defaultItem = option.getText(); System.out.println(defaultItem );
17) How to handle HTTPS websites in Selenium? Does Selenium support them?
You can handle HTTPS websites by handling SSL certificates in each browser using DesiredCapabilities.
18) How to accept the SSL untrusted connection?
For handling SSL error in Chrome, we need to use desired capabilities of Selenium Webdriver.
The below code will help to accept all the SSL certificate in chrome, and the user will not
receive any SSL certificate related error using this code.
// Create object of DesiredCapabilities class DesiredCapabilities cap=DesiredCapabilities.chrome(); // Set ACCEPT_SSL_CERTS variable to true cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true); // Set the driver path System.setProperty("webdriver.chrome.driver","Chrome driver path"); // Open browser with capability WebDriver driver=new ChromeDriver(cap);
19) What is HtmlUnitDriver?
HTML UnitDriver is the most light weight and fastest implementation headless browser for of WebDriver.
It is based on HtmlUnit. It is known as Headless Browser Driver. It is same as Chrome, IE, or FireFox driver,
but it does not have GUI so one cannot see the test execution on screen.
20) What is the use of @Factory annotation in TestNG?
A factory will execute all the test methods present inside a test class using separate instances of the class.
It is used to create instances of test classes dynamically. This is useful if you want to run the test class any number of times.
21) What are some common assertions provided by TestNG?
– AssertTrue
– AssertEqual
– AssertFalse
22) Name an API used for logging in Java?
Log4J api is used for logging in Java.
23) What is the use of logging in Automation?
Logging helps in debugging errors and reporting purposes.
24) Can Selenium Test an application on Android Browser?
We have to use Appium to automate an application on Android browser.
25) How to select a radio button in Selenium WebDriver?
We can select a radio button using click() method.
driver.findElement(By.id("checkbox")).click();
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.