Selenium Interview Questions -2
Xpath Interview Questions
- What is Normalize space in Xpath?
Normalize space is used to remove the empty spaces at the ends of the string while using xpath.normalize-space(string) str = " cher cher tech " normalize-space(string) // outputs "cher cher tech"
With code example
html code <label> who cares about spaces </label> xpath //label[normalize-space(text())='who cares about spaces']
- How do you handle text in XPath which is dynamic?
Using the text function, we can find the elements using the text present in them.<button type="button">Blueberry</button> (Blueberry is text here) xpath with text : //button[text()='Blueberry']
But when the text is dynamic we have to use contains function along with text function. For this to work some part of the string must be static.
<button type="button">*****berry</button> xpath with text : //button[contains(text(),'berry')]
- How do you handle lower and upper case attributes in Xpath?
We can use @ method for an attribute, but if the attribute values change every time lower to upper case or mix case value when the page refreshes, in this @ method may not help us.
During such kinds of situations, we must ignore the case(UPPER/lower).Syntax :// tagname[text(), 'sourceCaseContent', 'targetCaseContent'), 'value']
- How do you execute Xpath in Jquery?
We can execute the xpath in jQuery using $x$x("//xpath")
- Why Xpath is slower compared with?
XPath and CSS work almost with the same speed; even on this website, we have performed the tests couple of times but there is no considerable difference in speed.
What are Axes in Xpath?
- XPath axes are used to identify elements that periodically change or refresh their attributes by their relationship like a parent, child, sibling, based on the independent element, whose properties do not change.
- When the position function is used?
Position function helps the user to get the match at a particular index, using position we can get elements that are less than the position or greater than the position as well. - Can you use Xpath inside a frame?
The frame is nothing but another web page but showcased a different web page. So once you go into the frame you can use the XPath as if it is a normal web page. - How do you find parent elements without using Axes in Xpath?
In UNIX we use two dots to navigate to parent similar to that in xpath we can use two dots to navigate to the parent//input/../
- Write XPath for a Button?
Here, some people ask without giving any instruction for the questions. You have to ask the question of whether you should consider the button source code on your own.
You should know to create a button; otherwise, the intention of the question will not be satisfied. You can form the button in two ways, either using the button tag or input tag.
Xpath for button// form button using input tag <input type='button' value='ABCD'> Xpath : //input[@type='button'] // form button using the button tag <button>XYZ</button> Xpath : //button
- Can you write xpath for aria labels?
You can write xpath for any attribute present in the HTML tag; aria-label has been used to help visually impaired people. We can write xpath to aria labels as if it is an ID or a class.
<button aria-label="Close" onclick="myDialog.close()">X</button>
Xpath : //button[@aria-label='Close']
When does Implicitly Wait is applicable for
findElements method.?
Implicit wait in selenium is applicable for all findElement statements, but the applicability of implicit wait to the findElements is Limited.
Implicitly wait is applicable on findElements only when there are no elements present, selenium webdriver moves to the next command the moment it finds a single element.If there is Zero matching elements selenium waits till the time it reaches provided implicitly wait
Scenario 1 : Consider a page having 100 checkboxes and applied implicit wait is 30 Seconds
When the page is loading, selenium tries to find all the matching elements, but at that moment, there no matching elements.
After 10 seconds of wait 1 checkbox is loaded, now selenium finds that one checkbox and concludes that there is only one element, so it ends the wait and moves to the next command.
Selenium does not wait till 100 checkboxes loaded into the page, as the webdriver does not know how many elements going to be present on the page.
Scenario 2 : Consider a page having 0 checkboxes and applied implicit wait is 30 Seconds
Selenium tries to find all the matching elements in DOM, but so far, no checkbox is loaded.
Now there are zero matches, so selenium waits till 30 seconds, and if does not find an element after 30 seconds selenium webdriver throws NoSuchElementEXception.How do you check an option is present in a dropdown or not ? *New
Answer 1 : Find the dropdown and Create an object to the
Select
class and Retrieve all options usinggetOptions()
method and Iterate all options and compare the text.
Answer 2 : Find the dropdown and create the object toSelect
class and try to select the option usingselectByVisibleText
, if the option is there, then it will be selected; otherwise, it will throw an exception. So based on this exception, we can decide whether the element is there or not.
Answer 3 : Write the XPath/CSS which matches all the options(//select/option
) inside the dropdown and get InnerHtml (why the hell I am not using getText ?, have a Guess). And compare the text and check.
Answer 4 : Write locator for the option directly and find the element and do nothing, if we can find an element then the result is positive otherwise negative
Doubt on 4 : How do you decide, it present only on dropdown not anywhere else on the page, because in both casesAnswer 4
results positive.
Answer 5 : Write an Xpath/Css locator based on the parent(dropdown) and then find a child (the target) like,//select[@id='some']/option[text()='cherchertech']
, if element found, then positive otherwise negative. We can have a try and catch block here.
Answer 6 : What if try and catch block is irritating/irrelevant to test, then use the same XPath asAnswer 4
but use findElements instead of findElement. Now check the number of elements, if 0 then negative otherwise positive All the above answers are correct, use it based on the requirement but answer in the interview based on your experience, I would go with answers 5 and 6.How do you check whether an element enabled or not ?, condition: there is no such attribute as "disabled" in the element?
Explanation : When there is no such attribute as disabled in the element, the isEnabled() method from selenium does not work.
Solution : There is some attribute that makes the element to be disabled, so we have to find the element and get the attribute using the getAttribute() method then compare what is the value for enabled and disabled. Based on this, we can conclude whether the element is enabled or not.Why do we write
Webdriver driver = new FirefoxDriver()
, why notSearchContext driver = new FirefoxDriver()
?Don't hurry to answer like, Webdriver is an interface and FirefoxDriver is Class. This is right, but you need to understand the question first.
He is asking about SearchContext, which is a parent interface of Webdriver so that the answer would be. Yes, we can write like that because SearchContext is the parent of all the Interfaces and classes present in selenium.Is it possible to automate captcha?
Yes, we can automate the captcha, but there is a limitation that we can automate our own captcha but not others'
For example, a company has a captcha on their website, so somebody has to check it, but at the same time, it is not possible for a manual tester to check all the captchas.
So we have to automate the captcha in the dev/QA environment by getting the captcha answer in some attribute of the element, so based on that attribute, we can enter the value to the text bar, which accepts the captcha value.
We should remove this attribute while pushing the code to the Live environment.Testcase failed saying "ElementNotVisible", but when analyzed manually, the element is visible ? How to Handle it?
There are a couple of things that may cause this issue.
- The element may not be visible in automation due to the speed of selenium.
- If you closed a hidden division pop-up and tried to perform the action, then there is a chance that the hidden division popup animation was not over, which could cause this issue.
- There could be another element that has the same XPath or locator on some other page
Example : Consider you have an element that has XPath as //button[@id='abc'] on page X; by clicking some tab on X page navigates the user to Y page, Now there is an element on Y page which have XPath same as //button[@id='abc'].
But when you launch your application, the application may be directly landed on page Y. So with this scenario, if you try to perform on the element on Y page, it could throw an Exception.
Step by step Solution :- First, verify whether it is really any of the above scenarios?
- Print number of elements present with that XPath using the findElements method
- If there is only one element, please follow program 1; if there is more than 1 element, then follow program 2.
Program 1 : Using Explicit wait (Wait for the element to be visible):
1. Open the URL (please change according to your application).
2. Create an object for WebdriverWait and set the wait time as 60 seconds.WebDriverWait wait = new WebDriverWait(driver, 60 /*seconds*/);
3. Use visibilityOfElementLocated method present in Expected conditions class
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(buttonXpath)));
Complete Program 1 with Explicit wait (Wait for the element to be visible)
// PLEASE DO WRITE IMPORT STATEMENTS // set the webdriver Wait as 60 seconds WebDriverWait wait = new WebDriverWait(driver, 60 /*seconds*/); String buttonXpath = "//button[@id='abc']"; // wait for the element to be visible, max wait time is 60 seconds wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(buttonXpath))); // clicks the button which has xpath = //button[@id='abc'] driver.findElement(By.xpath(buttonXpath)).click();
Program 2 : Using findElements And IsDisplayed :
1. Open the URL (please change according to your application).
2. Set implicitly wait for 1 minute
3. Find all the elements which match the XPath and store them 4. iterate over the element we stored 5. verify if the element is displayed or not
6. Click element if displayed or ignore
7. Once the element is clicked, break the loop.
Complete Program 2 : findElements And IsDisplayed:// PLEASE DO WRITE IMPORT STATEMENTS // open webpage driver.get("https://chercher.tech/practice/practice-dropdowns"); String buttonXpath = "//button[@id='abc']"; // find all the elements with that xpath match List elements = driver.findElements(By.xpath(buttonXpath)); // iterate over the elements for (WebElement webElement : elements) { // check whether element is displayed or not, if (webElement.isDisplayed()) { // if displayed click the element and break the loop. webElement.click(); break; } }
Do implicit wait has any impact on findElements ?
Most of the time, the implicit wait will not work with findElements, but only when there are no elements for the given locator, then only implicit wait works from findElements.
Comments
Post a Comment