top of page

Python Selenium Interview Questions

arrow.png
arrow_edited_edited.jpg

Open Browser and Launch the Application

# Import necessary libraries
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
import time

# Create a Service object for the Chrome browser
service_obj = Service()

# Initialize the Chrome WebDriver with the created Service 
driver = webdriver.Chrome(service=service_obj)

# Open the specified website
driver.get("https://www.saucedemo.com")

# Set an implicit wait of 3 seconds to allow elements to be found
driver.implicitly_wait(3)

# Maximize the browser window
driver.maximize_window()

# Pause the script execution for 5 seconds (using time.sleep)
time.sleep(5)

# Print the title of the current webpage to the console
print(driver.title)

# Close the browser window
driver.quit()

Another way

from selenium import webdriver

# Create a new instance of the Chrome WebDriver

driver = webdriver.Chrome()

# Open the first tab and navigate to a website

driver.get("https://demoqa.com/automation-practice-form")

# Set an implicit wait of 3 seconds to allow elements to be found
driver.implicitly_wait(3)

# Maximize the browser window
driver.maximize_window()

Provide a Selenium script demonstrating how to run a browser in headless mode, specifically using Chrome

# Import necessary libraries
from selenium import webdriver
from selenium.webdriver.chrome.service import Service

# Create ChromeOptions and add the "headless" argument to run Chrome in headless mode
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("headless")

# Create a Service object for the Chrome browser
service_obj = Service()

# Initialize the Chrome WebDriver with the created Service and options
driver = webdriver.Chrome(service=service_obj, options=chrome_options)

# Open the specified website
driver.get("https://www.saucedemo.com")

# Set an implicit wait of 3 seconds to allow elements to be found
driver.implicitly_wait(3)

# Maximize the browser window
driver.maximize_window()

# Print the title of the current webpage to the console
print(driver.title)

# Close the browser window
driver.quit()

 

"What is the role of the `getText` method and how this method is utilized to fetch and display the text 
content from webelement

# Import necessary libraries
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

# Create a Service object for the Chrome browser
service_obj = Service()

# Initialize the Chrome WebDriver with the created Service
driver = webdriver.Chrome(service=service_obj)

# Open the specified website
driver.get("https://the-internet.herokuapp.com/")

# Maximize the browser window
driver.maximize_window()

# Set an implicit wait of 3 seconds to allow elements to be found
driver.implicitly_wait(3)

# Find and print the text of the first <h1> element on the page
h1_element_text = driver.find_element(By.TAG_NAME, "h1").text
print(h1_element_text)

# Close the browser window
driver.quit()

 

What makes Python a commonly chosen language for Selenium automation?

What is the approach for dealing with multiple tabs in a web browser using Selenium in Python?

import time

from selenium import webdriver
# Create a new instance of the Chrome WebDriver
driver = webdriver.Chrome()

# Open the first tab and navigate to a website
driver.get("https://the-internet.herokuapp.com/")
time.sleep(3)
# Open a new tab using JavaScript (Ctrl+T shortcut)
driver.execute_script("window.open('', '_blank');")
time.sleep(3)
# Switch to the second tab
driver.switch_to.window(driver.window_handles[1])
driver.get("https://the-internet.herokuapp.com/windows")
print(driver.window_handles[1])

# Open another tab using JavaScript
driver.execute_script("window.open('', '_blank');")

# Switch to the third tab
driver.switch_to.window(driver.window_handles[2])
driver.get("https://the-internet.herokuapp.com/windows/new")
print(driver.window_handles[2])
# Close the third tab
driver.close()
# Switch back to the second tab
driver.switch_to.window(driver.window_handles[1])

# Perform actions in the second tab

# Close the remaining tabs
driver.close()
time.sleep(3)
driver.switch_to.window(driver.window_handles[0])
print(driver.window_handles[0])
driver.close()

# Quit the WebDriver
driver.quit()

-------------------------
Factor                  
-------------------------

  • Ease of Learning        

  • Rich Ecosystem          

  • Community Support      

  • Cross-Platform          

  • Integration Capabilities 

  • Versatility             

-------------------------

Reason                     
-------------------------

  • Simple syntax and readability

  • Vast libraries and frameworks

  • Large and active community  

  • Compatibility across OS   

  • Seamless integration with Selenium 

  • Applicability in various domains 

bottom of page