Selenium Flashcards
(25 cards)
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
Which exception? How to handle?
password=driver.findElement(By.id(“password”))
password.sendKeys(“apwd”)
driver.navigate().refresh()
password.sendKeys(“anotherpwd”)
StaleElementReferenceException
put in try-except block and locate element again:
password=driver.findElement(By.id(“password”))
what are some http commands
GET: This command is used to retrieve data from a web server. For example, when you type a URL into a web browser, the browser sends a GET command to the server to retrieve the web page associated with that URL.
POST: This command is used to submit data to a web server, such as form data or a file upload. For example, when you submit a form on a web page, the data you entered is sent to the server using a POST command.
PUT: This command is used to update or replace an existing resource on the server.
DELETE: This command is used to delete a resource on the server.
HEAD: This command is similar to the GET command, but it only retrieves the headers of a resource, without downloading the full content.
OPTIONS: This command is used to retrieve the options supported by a resource on the server.
What does driver.close() do
used to close the current window that the WebDriver is currently controlling. If there is only one window open, calling this method will also exit the WebDriver instance. If there are multiple windows open, calling this method will close the current window and switch to the previous window in the WebDriver window handle list.
How to get current url
browser.current_url
Write import statement for WebDriverWait
from selenium.webdriver.support.ui import WebDriverWait
Write import statement for By
from selenium.webdriver.common.by import By
Write import statement for By
from selenium.webdriver.support import expected_conditions as EC
what is the difference between driver.close() and driver.quit()
if you have only one window open, calling driver.close() and driver.quit() will both have the same effect of closing the browser window. However, if you have multiple windows open, calling driver.close() will only close the current window, while driver.quit() will close all windows and release all resources used by the driver.
what are some of the expected conditions for explicit wait
resenceOfElementLocated
visibilityOfElementLocated
elementToBeClickable
textToBePresentInElement