Selenium Interview Questions

FREE Online Courses: Knowledge Awaits – Click for Free Access!

In this article, we will see some of the top and frequently asked Selenium Interview Questions with answers. Let’s start!!!

Selenium Interview Questions

1. What is the output of the following code?

from selenium import webdriver


driver = webdriver.Chrome()
driver.get("https://www.google.com/")
print(driver.title)
driver.quit()

Ans: The code opens the Google homepage in a Chrome browser window and prints the title of the page. The output should be “Google”.

2. What is the output of the following code?

from selenium import webdriver


driver = webdriver.Firefox()
driver.get("https://www.python.org/")
print(driver.current_url)
driver.quit()

Ans: The code opens the Python.org homepage in a Firefox browser window and prints the current URL of the page. The output should be “https://www.python.org/”.

3. What is the output of the following code?

from selenium import webdriver


driver = webdriver.Safari()
driver.get("https://www.wikipedia.org/")
print(driver.page_source)
driver.quit()

Ans: The code opens the Wikipedia homepage in a Safari browser window and prints the HTML source code of the page.

4. What is the output of the following code?

from selenium import webdriver


driver = webdriver.Chrome()
driver.get("https://www.youtube.com/")
elem = driver.find_element_by_name("search_query")
print(elem.tag_name)
driver.quit()

Ans: The code opens the YouTube homepage in a Chrome browser window and finds the search box element by its name attribute. It then prints the tag name of the element, which should be “input”.

5. What is the output of the following code?

from selenium import webdriver


driver = webdriver.Firefox()
driver.get("https://www.amazon.com/")
elem = driver.find_element_by_id("nav-orders")
print(elem.text)
driver.quit()

Ans: The code opens the Amazon homepage in a Firefox browser window and finds the “Orders” link element by its ID attribute. It then prints the text of the element, which should be “Orders”.

6. What is the output of the following code?

from selenium import webdriver


driver = webdriver.Chrome()
driver.get("https://www.google.com/")
elem = driver.find_element_by_name("q")
elem.send_keys("selenium")
elem.submit()
print(driver.title)
driver.quit()

Ans: The code opens the Google homepage in a Chrome browser window, finds the search box element by its name attribute, enters the text “selenium” into the box, submits the form, and prints the title of the resulting search results page. The output should be “selenium – Google Search”.

7. What is the output of the following code?

from selenium import webdriver


driver = webdriver.Firefox()
driver.get("https://www.python.org/")
elems = driver.find_elements_by_tag_name("h2")
for elem in elems:
print(elem.text)
driver.quit()

Ans: The code opens the Python.org homepage in a Firefox browser window and finds all of the h2 elements on the page. It then iterates through the list of elements and prints the text of each one.

8. What is the output of the following code?

from selenium import webdriver


driver = webdriver.Safari()
driver.get("https://www.wikipedia.org/")
elem = driver.find_element_by_link_text("English")
elem.click()
print(driver.current_url)
driver.quit()

Ans: The code opens the Wikipedia homepage in a Safari browser window and finds the “English” link element by its visible text. It then clicks the link and prints the current URL of the resulting page. The output should be “https://en.wikipedia.org/wiki/Main_Page”.

9. What is the output of the following code?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys


driver = webdriver.Chrome()
driver.get("https://www.google.com/")
elem = driver.find_element_by_name("q")
elem.send_keys("selenium")
elem.send_keys(Keys.RETURN)
elems = driver.find_elements_by_css_selector("h3 > a")
for elem in elems:
print(elem.get_attribute("href"))
driver.quit()

Ans: The code opens the Google homepage in a Chrome browser window, finds the search box element by its name attribute, enters the text “selenium” into the box, submits the form, finds all of the h3 elements with anchor tags on the search results page, iterates through the list of elements and prints the href attribute of each one.

10. What is the output of the following code?

from selenium import webdriver


driver = webdriver.Firefox()
driver.get("https://www.python.org/")
elem = driver.find_element_by_link_text("Documentation")
print(elem.get_attribute("class"))
driver.quit()

Ans: The code opens the Python.org homepage in a Firefox browser window and finds the “Documentation” link element by its visible text. It then prints the class attribute of the element.

11. What is the output of the following code?

from selenium import webdriver


driver = webdriver.Safari()
driver.get("https://www.wikipedia.org/")
elem = driver.find_element_by_partial_link_text("edit")
print(elem.get_attribute("href"))
driver.quit()

Ans: The code opens the Wikipedia homepage in a Safari browser window and finds the link element containing the word “edit” in its visible text. It then prints the href attribute of the element.

12. What is the output of the following code?

from selenium import webdriver


driver = webdriver.Chrome()
driver.get("https://www.google.com/")
elem = driver.find_element_by_name("q")
print(elem.is_displayed())
driver.quit()

Ans: The code opens the Google homepage in a Chrome browser window and finds the search box element by its name attribute. It then checks whether the element is currently displayed on the page and prints a boolean value.

13. What is the output of the following code?

from selenium import webdriver


driver = webdriver.Firefox()
driver.get("https://www.python.org/")
elem = driver.find_element_by_name("q")
print(elem.get_attribute("placeholder"))
driver.quit()

Ans: The code opens the Python.org homepage in a Firefox browser window and finds the search box element by its name attribute. It then prints the value of the placeholder attribute of the element.

14. What is the output of the following code?

from selenium import webdriver


driver = webdriver.Safari()
driver.get("https://www.wikipedia.org/")
elem = driver.find_element_by_name("search")
elem.send_keys("selenium")
elem.clear()
print(elem.get_attribute("value"))
driver.quit()

Ans: The code opens the Wikipedia homepage in a Safari browser window and finds the search box element by its name attribute. It then enters the text “selenium” into the box, clears the contents of the box, and prints the current value of the element (which should be an empty string).

15. What is the output of the following code?

from selenium import webdriver


driver = webdriver.Chrome()
driver.get("https://www.google.com/")
elem = driver.find_element_by_name("q")
print(elem.location)
driver.quit()

Ans: The code opens the Google homepage in a Chrome browser window and finds the search box element by its name attribute. It then prints a dictionary containing the location of the element on the page in pixels from the top and left edges

16. What is the output of the following code?

from selenium import webdriver


driver = webdriver.Firefox()
driver.get("https://www.python.org/")
elem = driver.find_element_by_tag_name("h1")
print(elem.text)
driver.quit()

Ans: The code opens the Python.org homepage in a Firefox browser window and finds the first h1 element on the page by its tag name. It then prints the visible text content of the element.

17. What is the output of the following code?

from selenium import webdriver


driver = webdriver.Safari()
driver.get("https://www.wikipedia.org/")
elem = driver.find_element_by_id("searchLanguage")
options = elem.find_elements_by_tag_name("option")
for option in options:
print(option.text)
driver.quit()

Ans: The code opens the Wikipedia homepage in a Safari browser window and finds the select element with the ID “searchLanguage”. It then finds all of the option elements within the select element, iterates through them, and prints the visible text content of each one.

18. What is the output of the following code?

from selenium import webdriver


driver = webdriver.Chrome()
driver.get("https://www.google.com/")
elem = driver.find_element_by_name("q")
elem.send_keys("selenium")
elem.submit()
print(driver.title)
driver.quit()

Ans: The code opens the Google homepage in a Chrome browser window and finds the search box element by its name attribute. It then enters the text “selenium” into the box, submits the form, waits for the search results page to load, and prints the title of the resulting page.

19. What is the output of the following code?

from selenium import webdriver


driver = webdriver.Firefox()
driver.get("https://www.python.org/")
elem = driver.find_element_by_link_text("Downloads")
print(elem.location_once_scrolled_into_view)
driver.quit()

Ans: The code opens the Python.org homepage in a Firefox browser window and finds the “Downloads” link element by its visible text. It then scrolls the page so that the element is in view, waits for the page to finish scrolling, and prints a dictionary containing the location of the element in pixels from the top and left edges.

20. What is the output of the following code?

from selenium import webdriver


driver = webdriver.Safari()
driver.get("https://www.wikipedia.org/")
elem = driver.find_element_by_name("search")
print(elem.is_enabled())
driver.quit()

Ans: The code opens the Wikipedia homepage in a Safari browser window and finds the search box element by its name attribute. It then checks whether the element is currently enabled (i.e., can be interacted with) and prints a boolean value.

21. What is the output of the following code?

from selenium import webdriver


driver = webdriver.Chrome()
driver.get("https://www.google.com/")
elem = driver.find_element_by_name("q")
elem.clear()
print(elem.get_attribute("value"))
driver.quit()

Ans: The code opens the Google homepage in a Chrome browser window and finds the search box element by its name attribute. It then clears the contents of the box, gets the current value of the box (which should be an empty string), and prints it.

22. What is the output of the following code?

from selenium import webdriver


driver = webdriver.Firefox()
driver.get("https://www.python.org/")
elem = driver.find_element_by_id("submit")
print(elem.is_displayed())
driver.quit()

Ans: The code opens the Python.org homepage in a Firefox browser window and finds the submit button element by its ID attribute. It then checks whether the element is currently displayed (i.e., visible on the page) and prints a boolean value.

23. What is the output of the following code?

from selenium import webdriver


driver = webdriver.Safari()
driver.get("https://www.wikipedia.org/")
elem = driver.find_element_by_css_selector("input[type='search']")
elem.send_keys("selenium")
elem.submit()
print(driver.current_url)
driver.quit()

Ans: The code opens the Wikipedia homepage in a Safari browser window and finds the search box element by a CSS selector that matches any input element with a type attribute of “search”. It then enters the text “selenium” into the box, submits the form, waits for the search results page to load, and prints the current URL of the resulting page.

24. What is the output of the following code?

from selenium import webdriver


driver = webdriver.Chrome()
driver.get("https://www.google.com/")
elem = driver.find_element_by_name("q")
elem.send_keys("selenium")
elem.submit()
elems = driver.find_elements_by_css_selector("h3.LC20lb")
for elem in elems:
print(elem.text)
driver.quit()

Ans: The code opens the Google homepage in a Chrome browser window and finds the search box element by its name attribute. It then enters the text “selenium” into the box, submits the form, waits for the search results page to load, and finds all of the h3 elements with a class name of “LC20lb”. It iterates through the elements and prints the visible text content of each one.

25. What is the output of the following code?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys


driver = webdriver.Firefox()
driver.get("https://www.python.org/")
elem = driver.find_element_by_tag_name("body")
elem.send_keys(Keys.CONTROL + "t")
print(driver.window_handles)
driver.quit()

Ans: The code opens the Python.org homepage in a Firefox browser window and finds the body element. It then simulates the user pressing the control key and the “t” key at the same time, which should open a new browser tab. Finally, it prints a list of the browser window handles (IDs), which should contain two handles for the original tab and the new tab.

26. What is the output of the following code?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys


driver = webdriver.Safari()
driver.get("https://www.wikipedia.org/")
elem = driver.find_element_by_name("search")
elem.send_keys("selenium")
elem.send_keys(Keys.RETURN)
print(driver.current_url)
driver.quit()

Ans: The code opens the Wikipedia homepage in a Safari browser window and finds the search box element by its name attribute. It then enters the text “selenium” into the box and simulates the user pressing the return key, which should submit the form. Finally, it prints the current URL of the resulting page.

27. What is the output of the following code?

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains


driver = webdriver.Chrome()
driver.get("https://www.python.org/")
elem = driver.find_element_by_link_text("Downloads")
hover = ActionChains(driver).move_to_element(elem)
hover.perform()
print(driver.current_url)
driver.quit()

Ans: The code opens the Python.org homepage in a Chrome browser window and finds the “Downloads” link element by its visible text content. It then creates an ActionChains object, which can be used to simulate mouse movements and clicks, and moves the mouse cursor to hover over the “Downloads” link. Finally, it prints the current URL of the page (which should still be the Python.org homepage).

28. What is the output of the following code?

from selenium import webdriver


driver = webdriver.Firefox()
driver.get("https://www.wikipedia.org/")
elem = driver.find_element_by_link_text("English")
elem.click()
print(driver.current_url)
driver.back()
print(driver.current_url)
driver.forward()
print(driver.current_url)
driver.quit()

Ans: The code opens the Wikipedia homepage in a Firefox browser window and finds the “English” link element by its visible text content. It then clicks the link, waits for the new page to load, and prints the current URL of the resulting page. It then simulates the user pressing the “back” button, waits for the previous page to load, and prints its URL. Finally, it simulates the user pressing the “forward” button, waits for the next page to load, and prints its URL.

29. What is the output of the following code?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys


driver = webdriver.Safari()
driver.get("https://www.google.com/")
elem = driver.find_element_by_name("q")
elem.send_keys("selenium")
elem.send_keys(Keys.ENTER)
print(driver.title)
driver.quit()

Ans: The code opens the Google homepage in a Safari browser window and finds the search box element by its name attribute. It then enters the text “selenium” into the box and simulates the user pressing the enter key, which should submit the form. Finally, it prints the title of the resulting page, which should be “selenium – Google Search”.

30. What is the output of the following code?

from selenium import webdriver


driver = webdriver.Chrome()
driver.get("https://www.wikipedia.org/")
elem = driver.find_element_by_css_selector("input[type='search']")
elem.send_keys("selenium")
elem.submit()
print(driver.find_element_by_partial_link_text("Selenium (software)").get_attribute("href"))
driver.quit()

Ans: The code opens the Wikipedia homepage in a Chrome browser window and finds the search box element by a CSS selector that matches any input element with a type attribute of “search”. It then enters the text “selenium” into the box, submits the form, waits for the search results page to load, and finds the first link element whose visible text content contains the string “Selenium (software)”. It gets the value of the “href” attribute of that link element and prints it. This should be the URL of the Wikipedia page for the Selenium software.

Summary

This was all about selenium interview questions and answers. These will definitely help you crack your interviews.

Did we exceed your expectations?
If Yes, share your valuable feedback on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *