Selenium Flashcards
What is the same-origin policy and how is it handled?
a web browser allows scripts from one webpage to access the contents of another webpage provided both the pages have the same origin. The origin refers to a combination of the URL scheme, hostname, and port number.
Mention the types of Web locators.
driver.findElement(By.id(“user”));
driver.findElement(By.linkText(“Today’s deals”)).click(); # and link and partial link
driver.findElement(By.name(“books”).click());
driver.findElement(By.tagName(“button”).click());
driver.findElement(By.className(“inputtext”));
driver.findElement(By.xpath(“//span[contains(text(),’an account’)]”)).getText();
driver.findElement(By.cssSelector(“input#email”)).sendKeys(“myemail@email.com”);
What are the types of waits supported by WebDriver?
- explicit: wait for certain conditions before throwing an “ElementNotVisibleException”.
- implicit: wait for a certain amount of time before throwing a “No such element” exception.
- fluet wait: tell the web driver to wait for a condition, as well as the frequency with which we want to check the condition before throwing an “ElementNotVisibleException”
Mention the types of navigation commands
- driver.navigate().to(“https://www.ebay.in/”);
- driver.navigate().refresh();
- driver.navigate().forward();
- driver.navigate().back();
What is the difference between “/” and “//” in XPath?
”/” is used to select an element based on its absolute location, while “//” is used to select an element based on its relative location.
For example, if you want to select the first <p> element on a page, you would use “/p”. If you want to select all <p> elements on a page, regardless of their location, you would use “//p”.
How to select a value from a dropdown in Selenium WebDriver?
Select class in WebDriver is used for selecting and deselecting options in a dropdown.
The objects of Select type can be initialized by passing the dropdown webElement as a parameter to its constructor.
WebElement testDrop = driver.findElement(By.id(“testingDropdown”));
Select dropdown = new Select(testDrop);
WebDriver offers three ways to select from a dropdown:
selectByIndex: Selection based on index starting from 0
dropdown.selectByIndex(5);
selectByValue: Selection based on value
dropdown.selectByValue(“Books”);
selectByVisibleText: Selection of option that displays text matching the given argument
dropdown.selectByVisibleText(“The Alchemist”);
What does the switchTo() command do?
switchTo() command is used to switch between windows, frames or pop-ups within the application. Every window instantiated by the WebDriver is given a unique alphanumeric value called “Window Handle”.
Get the window handle of the window you wish to switch to
String handle= driver.getWindowHandle();
Switch to the desired window
driver.switchTo().window(handle);
Alternatively
for(String handle= driver.getWindowHandles())
{ driver.switchTo().window(handle); }
How to set browser window size in Selenium?
driver.manage().window().maximize();
Dimension d = new Dimension(400,600);
driver.manage().window().setSize(d);
Alternatively,
The window size can be reset using JavaScriptExecutor
((JavascriptExecutor)driver).executeScript(“window.resizeTo(1024, 768)”);
Which 2 exceptions?
driver.findElement(By.xpath(“…wrong locator
driver.findElement(By.xpath(“…invalid xpath syntax
NoSuchElementException
InvalidSelectorException
Which 2 exceptions?
driver.switchTo().window(“abcd”)
driver.switchTo().frame(“abcd”)
NoSuchWindowException
NoSuchFrameException
Which exceptions?
driver.switchTo.alert()
NoAlertPresentException
Which exception when the button is disabled?
driver.findElement(By.id(“hiddn_bttn”)).click()
ElementNotInteractableException
This exception is raised when an element is present in the HTML DOM (Document Object Model), but it is not in a state that can be interacted with. (disabled, hidden, not visible)
Difference between ElementNotInteractableException and ElementNotVisibleException
ElementNotInteractableException is thrown when an element is present but not in a state to be interacted with, while ElementNotVisibleException is thrown when an element is not currently visible on the page (enabled button is at the bottom –> need to scroll first).
Which exception?
driver.quit()
driver.findElement(By.id(“valid_id”)).click()
NoSuchSessionException
How to handle StaleElementReferenceException?
exception msg: element not attached to page document
put it a catch block and run fin