A: This exception is thrown when the element is either deleted or no longer attached to the DOM (Document Object Model)
A: Page Object Model is a design pattern for creating an Object Repository for web UI elements. Each web page in the application is required to have its own corresponding page class. The page class is thus responsible for finding the WebElements in the page, and then perform operations on those WebElements.
A: Page Factory is used to initialize the elements of the page object or instantiate the page objects. Instead of using “driver.findElement”, we use annotations like @FindBy to find WebElements, and initElements method to initialize web elements from the page factory class.
A: Implicit wait instructs the WebDriver to wait for some time by polling the DOM. Once you have declared implicit wait, it will be available for the entire life of the WebDriver instance. Explicit wait instructs the execution to wait for some time until some condition is achieved such as element to be clickable or present.
WebDriverWait wait=new WebDriverWait(driver, 20);
Element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath( “<xpath”)));
A: We can scroll down to a particular element using the function scrollIntoView().
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", element);
A: Use select tag to identify a dropdown element, and then select an option from that dropdown element
Cars
Bikes
Trains
WebElement mySelectElement = driver.findElement(By.id("mySelect"));
Select dropdown = new Select(mySelectElement);
Now to select an option from that dropdown, we can do it in either of the three ways:
1. dropdown.selectByVisibleText(“Bikes”); → Selecting an option by the text that is visible
2. dropdown.selectByIndex(“1”); → Selecting, by choosing the Index number of that option
3. dropdown.selectByValue(“option2”); → Selecting, by choosing the value of that option
A: find and click the menu WebElement, then find the list of elements, and go through the list
driver.findElement(By.xpath(“//*[id=’menu1’]”)).click();
List dropDown = driver.findElements(By.xpath(“//ul[@class=’dropdown-menu’]//li/a”));
for(WebElement e:dropDown){
String str = e.getAttribute(“innerHTML”);
If( str.contentEquals(“JavaScript”) ){
e.click();
break;
}
}
(ul = undordered list)
A: Use FindAll annotation to search for all elements that match any of the FindBy criteria. @FindAll(@FindBy(how = How.XPATH, using = "//*[contains(@class,'x-grid-tree-node-leaf')]")) List allElements;
A: Use the anchor tags to find a list of WebElements. For each tag, we can use the attribute href to obtain the hyperlink, and then analyze the response received via driver.get() method.
A: @Test (enabled = false)
A: Implicit wait is used to wait for a certain amount of time before throwing an exception that selenium cannot find the element on the page. The implicit wait is set for entire session that the browser is open.
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://url_that_delays_loading");
WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));
Explicit wait is used to wait for a longer ExpectedConditions such as an element to become clickable, visible.
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
Fluent Wait is used to wait for a condition with the maximum amount of time.
// Waiting 30 seconds for an element to be present on the page, checking
// for its presence once every 5 seconds.
Wait wait = new FluentWait(driver)
.withTimeout(30, SECONDS)
.pollingEvery(5, SECONDS)
.ignoring(NoSuchElementException.class);
WebElement foo = wait.until(new Function() {
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id("foo"));
}
});
FileInputStream fis = new FileInputStream(“path of excel file”);
Workbook wb = WorkbookFactory.create(fis);
Sheet s = wb.getSheet(“sheetName”);
String value = s.getRow(rowNum).getCell(cellNum).getStringCellValue();
Relative XPath with starting text : //button[starts-with(@id, ’Navyug-’)]
Relative XPath with Following or Preceding Node
//button [contains(@class, ‘Navyug-Class’)] /following:: input[contains(@id,’Navyug-’)]
//input [contains(@id,’Navyug-’)] /preceding:: button[contains(@class, ‘Navyug-Class’)]
Relative XPath with text contains : //button[contains(@class, ‘Navyug’)]
Relative XPath with Multiple Attribute
//button[contains(@id,’Navyug-’)] [contains(@class, ‘Navyug-Class-text’)]
Element with Index
Driver.findElements(By.tag(‘button’))[4]