Selenium Flashcards

1
Q

What is the same-origin policy and how is it handled?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Mention the types of Web locators.

A

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”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the types of waits supported by WebDriver?

A
  1. explicit: wait for certain conditions before throwing an “ElementNotVisibleException”.
  2. implicit: wait for a certain amount of time before throwing a “No such element” exception.
  3. 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”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Mention the types of navigation commands

A
  1. driver.navigate().to(“https://www.ebay.in/”);
  2. driver.navigate().refresh();
  3. driver.navigate().forward();
  4. driver.navigate().back();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the difference between “/” and “//” in XPath?

A

”/” 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to select a value from a dropdown in Selenium WebDriver?

A

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”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does the switchTo() command do?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to set browser window size in Selenium?

A

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)”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Which 2 exceptions?
driver.findElement(By.xpath(“…wrong locator
driver.findElement(By.xpath(“…invalid xpath syntax

A

NoSuchElementException
InvalidSelectorException

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Which 2 exceptions?
driver.switchTo().window(“abcd”)
driver.switchTo().frame(“abcd”)

A

NoSuchWindowException
NoSuchFrameException

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Which exceptions?
driver.switchTo.alert()

A

NoAlertPresentException

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Which exception when the button is disabled?
driver.findElement(By.id(“hiddn_bttn”)).click()

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Difference between ElementNotInteractableException and ElementNotVisibleException

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Which exception?
driver.quit()
driver.findElement(By.id(“valid_id”)).click()

A

NoSuchSessionException

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to handle StaleElementReferenceException?

A

exception msg: element not attached to page document
put it a catch block and run fin

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Which exception? How to handle?
password=driver.findElement(By.id(“password”))
password.sendKeys(“apwd”)
driver.navigate().refresh()
password.sendKeys(“anotherpwd”)

A

StaleElementReferenceException
put in try-except block and locate element again:
password=driver.findElement(By.id(“password”))

17
Q

what are some http commands

A

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.

18
Q

What does driver.close() do

A

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.

19
Q

How to get current url

A

browser.current_url

20
Q

Write import statement for WebDriverWait

A

from selenium.webdriver.support.ui import WebDriverWait

21
Q

Write import statement for By

A

from selenium.webdriver.common.by import By

22
Q

Write import statement for By

A

from selenium.webdriver.support import expected_conditions as EC

23
Q

what is the difference between driver.close() and driver.quit()

A

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.

24
Q

what are some of the expected conditions for explicit wait

A

resenceOfElementLocated
visibilityOfElementLocated
elementToBeClickable
textToBePresentInElement

25
Q

what is the default polling interval in fluent wait

A

500 ms