0% found this document useful (0 votes)
3 views51 pages

At All Script Questions

Uploaded by

santhoshsiva879
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views51 pages

At All Script Questions

Uploaded by

santhoshsiva879
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Write a Selenium script that opens a browser, navigates to the login


page of SauceDemo, waits for the username field, enters login
credentials using ID locator, clicks the login button, waits for the
products page to load, and prints the page title before closing the
browser.

from selenium import webdriver

from [Link] import By

from [Link] import WebDriverWait

from [Link] import expected_conditions as EC

driver = [Link]()

driver.maximize_window()

[Link]("[Link]

wait = WebDriverWait(driver,10)

username = [Link](EC.presence_of_element_located(([Link],"user-
name")))

username.send_keys("standard_user")

password = driver.find_element([Link],"password")

password.send_keys("secret_sauce")

driver.find_element([Link],"login-button").click()

[Link](EC.presence_of_element_located((By.CLASS_NAME,"inventory_lis
t")))

print([Link])
[Link]()

2. Write a Selenium script that opens a browser, navigates to


[Link] locates the search box using the name
locator, and prints its placeholder attribute.

from selenium import webdriver

from [Link] import By

driver = [Link]()

[Link]("[Link]

search = driver.find_element([Link],"q")

print(search.get_attribute("placeholder"))

[Link]()

3. Write a Selenium script that locates the username field using XPath,
enters text using send_keys(), clears it using clear(), and enters a
new value.

from selenium import webdriver

from [Link] import By

import time

driver = [Link]()

[Link]("[Link]

username = driver.find_element([Link],"//input[@id='user-name']")

username.send_keys("Mahesh")

[Link](2)
[Link]()

[Link](2)

username.send_keys("standard_user")

[Link](3)

[Link]()

[Link] a Selenium script that opens a browser, navigates to


[Link] clicks the English link using link text locator,
and prints the page title.

from selenium import webdriver

from [Link] import By

driver = [Link]()

[Link]("[Link]

driver.find_element(By.LINK_TEXT,"English").click()

print([Link])

[Link]()

[Link] a Selenium script that waits for the login button to become
clickable using explicit wait and clicks it.
from selenium import webdriver

from [Link] import By

from [Link] import WebDriverWait

from [Link] import expected_conditions as EC

driver = [Link]()

[Link]("[Link]

wait = WebDriverWait(driver,10)

login = [Link](EC.element_to_be_clickable(([Link],"login-button")))

[Link]()

[Link]()

[Link] a Selenium script that opens a browser, navigates to


[Link] waits until the page title contains the word
“Google”, and prints the title.

from selenium import webdriver

from [Link] import WebDriverWait

driver = [Link]()

[Link]("[Link]

WebDriverWait(driver,10).until(lambda d: "Google" in [Link])

print([Link])

[Link]()
[Link] a Selenium script that locates an element using CSS Selector and
prints the text displayed in that element.

from selenium import webdriver

from [Link] import By

driver = [Link]()

[Link]("[Link]

text = driver.find_element(By.CSS_SELECTOR,"div.login_logo").text

print(text)

[Link]()

[Link] a Selenium script that opens a browser, navigates to a webpage,


finds all links using tag name locator, and prints the number of links
present on the page.

from selenium import webdriver

from [Link] import By

driver = [Link]()

[Link]("[Link]

links = driver.find_elements(By.TAG_NAME,"a")

print("Total links:",len(links))

[Link]()
[Link] a Selenium script that finds a button using class name locator,
verifies whether the button is enabled, and prints the result.

from selenium import webdriver

from [Link] import By

driver = [Link]()

[Link]("[Link]

button = driver.find_element([Link],"login-button")

print("Button Enabled:",button.is_enabled())

[Link]()

[Link] a Selenium script that opens a browser, navigates to


SauceDemo, waits for the logo to become visible using explicit wait, and
verifies that the logo is displayed.

from selenium import webdriver

from [Link] import By

from [Link] import WebDriverWait

from [Link] import expected_conditions as EC

driver = [Link]()

[Link]("[Link]

wait = WebDriverWait(driver,10)
logo =
[Link](EC.visibility_of_element_located((By.CLASS_NAME,"login_logo")))

print("Logo Displayed:",logo.is_displayed())

[Link]()

[Link] a Selenium script that logs into SauceDemo, locates all product
names using class name locator, and prints each product name in the
console.

from selenium import webdriver

from [Link] import By

import time

driver = [Link]()

driver.maximize_window()

[Link]("[Link]

driver.find_element([Link],"user-name").send_keys("standard_user")

driver.find_element([Link],"password").send_keys("secret_sauce")

driver.find_element([Link],"login-button").click()

[Link](3)

products = driver.find_elements(By.CLASS_NAME,"inventory_item_name")

for p in products:

print([Link])
[Link]()

[Link] a Selenium script that logs into SauceDemo, locates all products
on the inventory page, and prints the total number of products displayed.

from selenium import webdriver

from [Link] import By

import time

driver = [Link]()

[Link]("[Link]

driver.find_element([Link],"user-name").send_keys("standard_user")

driver.find_element([Link],"password").send_keys("secret_sauce")

driver.find_element([Link],"login-button").click()

[Link](3)

products = driver.find_elements(By.CLASS_NAME,"inventory_item")

print("Total Products:",len(products))

[Link]()

[Link] a Selenium script that opens [Link] locates the


search box using name locator, types “Selenium WebDriver”, and presses
ENTER using keyboard action.

from selenium import webdriver

from [Link] import By

from [Link] import Keys


driver = [Link]()

[Link]("[Link]

search = driver.find_element([Link],"q")

search.send_keys("Selenium WebDriver")

search.send_keys([Link])

[Link]()

[Link] a Selenium script that opens a browser, navigates to


[Link] locates the search box using ID locator, types a
keyword, and clicks the search button.

from selenium import webdriver

from [Link] import By

driver = [Link]()

[Link]("[Link]

driver.find_element([Link],"searchInput").send_keys("Selenium")

driver.find_element([Link],"//button[@type='submit']").click()

[Link]()

15. Write a Selenium script that opens a browser, navigates to a webpage,


refreshes the page three times, and prints the page title.

from selenium import webdriver


import time

driver = [Link]()

[Link]("[Link]

for i in range(3):

[Link]()

[Link](2)

print([Link])

[Link]()

[Link] a Selenium script that opens [Link] clicks the


English language link using partial link text locator, and prints the page
title.

from selenium import webdriver

from [Link] import By

driver = [Link]()

[Link]("[Link]

driver.find_element(By.PARTIAL_LINK_TEXT,"English").click()

print([Link])

[Link]()
[Link] a Selenium script that opens a browser, navigates to
SauceDemo, waits for the username field using explicit wait, and types a
username.

from selenium import webdriver

from [Link] import By

from [Link] import WebDriverWait

from [Link] import expected_conditions as EC

driver = [Link]()

[Link]("[Link]

wait = WebDriverWait(driver,10)

username = [Link](EC.presence_of_element_located(([Link],"user-
name")))

username.send_keys("standard_user")

[Link]()

18. Write a Selenium script that opens a browser, navigates to a webpage,


locates all buttons using tag name locator, and prints the text of each
button.

from selenium import webdriver

from [Link] import By

driver = [Link]()

[Link]("[Link]
buttons = driver.find_elements(By.TAG_NAME,"button")

for b in buttons:

print([Link])

[Link]()

[Link] a Selenium script that opens a browser, navigates to


[Link] prints the current URL, and closes the browser.

from selenium import webdriver

driver = [Link]()

[Link]("[Link]

print(driver.current_url)

[Link]()

[Link] a Selenium script that locates the username field using XPath
contains() function and enters a username.

from selenium import webdriver

from [Link] import By

driver = [Link]()

[Link]("[Link]

username = driver.find_element([Link],"//input[contains(@id,'user')]")
username.send_keys("standard_user")

[Link]()

[Link] a Selenium script that sets an implicit wait of 10 seconds and


then locates the username field on the SauceDemo login page.

from selenium import webdriver

from [Link] import By

driver = [Link]()

driver.implicitly_wait(10)

[Link]("[Link]

username = driver.find_element([Link],"user-name")

print("Username field located")

[Link]()

[Link] a Selenium script that opens a browser, navigates to two


different websites, goes back to the previous page, and prints the page
title.

from selenium import webdriver

driver = [Link]()

[Link]("[Link]

[Link]("[Link]
[Link]()

print([Link])

[Link]()

[Link] a Selenium script that retrieves the placeholder attribute of the


username field from SauceDemo and prints it.

from selenium import webdriver

from [Link] import By

driver = [Link]()

[Link]("[Link]

username = driver.find_element([Link],"user-name")

print(username.get_attribute("placeholder"))

[Link]()

[Link] a Selenium script that locates the login button using CSS selector
with attribute and clicks it.

from selenium import webdriver

from [Link] import By

driver = [Link]()

[Link]("[Link]
driver.find_element(By.CSS_SELECTOR,"input[id='login-button']").click()

[Link]()

25. Write a Selenium script that waits until the login button becomes
clickable using explicit wait and then clicks it.

from selenium import webdriver

from [Link] import By

from [Link] import WebDriverWait

from [Link] import expected_conditions as EC

driver = [Link]()

[Link]("[Link]

wait = WebDriverWait(driver,10)

login = [Link](EC.element_to_be_clickable(([Link],"login-button")))

[Link]()

[Link]()

[Link] a Selenium script that opens three different websites sequentially


and prints the title of each page.

from selenium import webdriver

driver = [Link]()

sites = [
"[Link]

"[Link]

"[Link]

for site in sites:

[Link](site)

print([Link])

[Link]()

[Link] a Selenium script that finds all buttons on a webpage and prints
whether each button is enabled.

from selenium import webdriver

from [Link] import By

driver = [Link]()

[Link]("[Link]

buttons = driver.find_elements(By.TAG_NAME,"button")

for b in buttons:

print([Link], b.is_enabled())

[Link]()

[Link] a Selenium script that locates the login button using XPath AND
condition and clicks it.

from selenium import webdriver


from [Link] import By

driver = [Link]()

[Link]("[Link]

login = driver.find_element([Link],"//input[@id='login-button' and


@type='submit']")

[Link]()

[Link]()

[Link] a Selenium script that waits until the page title contains the word
“Google” and then prints the title.

from selenium import webdriver

from [Link] import WebDriverWait

driver = [Link]()

[Link]("[Link]

WebDriverWait(driver,10).until(lambda d: "Google" in [Link])

print([Link])

[Link]()

[Link] a Selenium script that opens a browser, navigates to


[Link] enters a search term, clicks the search button,
prints the page title, and closes the browser.
from selenium import webdriver

from [Link] import By

from [Link] import Keys

import time

driver = [Link]()

[Link]("[Link]

search = driver.find_element([Link],"q")

search.send_keys("Selenium")

search.send_keys([Link])

[Link](3)

print([Link])

[Link]()

[Link] a Selenium script that logs into SauceDemo, locates all product
prices, converts them to numbers, and prints the highest price.

from selenium import webdriver

from [Link] import By

import time

driver = [Link]()

[Link]("[Link]
driver.find_element([Link],"user-name").send_keys("standard_user")

driver.find_element([Link],"password").send_keys("secret_sauce")

driver.find_element([Link],"login-button").click()

[Link](3)

prices = driver.find_elements(By.CLASS_NAME,"inventory_item_price")

price_list = []

for p in prices:

value = float([Link]("$",""))

price_list.append(value)

print("Highest Price:",max(price_list))

[Link]()

[Link] a Selenium script that opens a webpage and verifies whether the
login button is displayed and enabled.

from selenium import webdriver

from [Link] import By

driver = [Link]()

[Link]("[Link]

login = driver.find_element([Link],"login-button")
print("Displayed:",login.is_displayed())

print("Enabled:",login.is_enabled())

[Link]()

[Link] a Selenium script that logs into SauceDemo and counts the total
number of products displayed.

from selenium import webdriver

from [Link] import By

import time

driver = [Link]()

[Link]("[Link]

driver.find_element([Link],"user-name").send_keys("standard_user")

driver.find_element([Link],"password").send_keys("secret_sauce")

driver.find_element([Link],"login-button").click()

[Link](3)

products = driver.find_elements(By.CLASS_NAME,"inventory_item")

print("Total Products:",len(products))

[Link]()

34. Write a Selenium script that checks whether a specific element exists
on the page without throwing an exception.
from selenium import webdriver

from [Link] import By

driver = [Link]()

[Link]("[Link]

elements = driver.find_elements([Link],"login-button")

if len(elements) > 0:

print("Element Exists")

else:

print("Element Not Found")

[Link]()

[Link] a Selenium script that opens the login page of SauceDemo, waits
for the username field to be visible, enters credentials using ID locator,
clicks login, waits until the inventory page loads, and prints the page title.

from selenium import webdriver

from [Link] import By

from [Link] import WebDriverWait

from [Link] import expected_conditions as EC

driver = [Link]()

[Link]("[Link]

wait = WebDriverWait(driver,10)
username = [Link](EC.visibility_of_element_located(([Link],"user-
name")))

username.send_keys("standard_user")

driver.find_element([Link],"password").send_keys("secret_sauce")

driver.find_element([Link],"login-button").click()

[Link](EC.visibility_of_element_located((By.CLASS_NAME,"inventory_list
")))

print([Link])

[Link]()

[Link] a Selenium script that opens SauceDemo, waits for the login
button to become clickable, enters username and password, clicks login,
waits until the URL contains “inventory”, and prints the current URL.

from selenium import webdriver

from [Link] import By

from [Link] import WebDriverWait

driver = [Link]()

[Link]("[Link]

driver.find_element([Link],"user-name").send_keys("standard_user")

driver.find_element([Link],"password").send_keys("secret_sauce")

driver.find_element([Link],"login-button").click()

WebDriverWait(driver,10).until(lambda d: "inventory" in d.current_url)


print(driver.current_url)

[Link]()

[Link] a Selenium script that opens SauceDemo login page, waits for
the password field to appear, enters login credentials, clicks login, waits
until the inventory items are visible, and prints the number of products
displayed.

from selenium import webdriver

from [Link] import By

from [Link] import WebDriverWait

from [Link] import expected_conditions as EC

driver = [Link]()

[Link]("[Link]

driver.find_element([Link],"user-name").send_keys("standard_user")

driver.find_element([Link],"password").send_keys("secret_sauce")

driver.find_element([Link],"login-button").click()

wait = WebDriverWait(driver,10)

items =
[Link](EC.presence_of_all_elements_located((By.CLASS_NAME,"inventor
y_item")))

print("Products:",len(items))

[Link]()
[Link] a Selenium script that opens SauceDemo, waits for the username
field, enters invalid credentials, clicks login, waits for the error message to
appear, and prints the error message text.

from selenium import webdriver

from [Link] import By

from [Link] import WebDriverWait

from [Link] import expected_conditions as EC

driver = [Link]()

[Link]("[Link]

driver.find_element([Link],"user-name").send_keys("wrong")

driver.find_element([Link],"password").send_keys("wrong")

driver.find_element([Link],"login-button").click()

wait = WebDriverWait(driver,10)

error = [Link](EC.visibility_of_element_located((By.CLASS_NAME,"error-
message-container")))

print([Link])

[Link]()

[Link] a Selenium script that logs into SauceDemo, waits for the first
product add-to-cart button to become clickable, clicks it, waits for the cart
badge to appear, and prints the badge count.

from selenium import webdriver

from [Link] import By


import time

driver = [Link]()

[Link]("[Link]

driver.find_element([Link],"user-name").send_keys("standard_user")

driver.find_element([Link],"password").send_keys("secret_sauce")

driver.find_element([Link],"login-button").click()

[Link](3)

driver.find_element(By.CLASS_NAME,"btn_inventory").click()

badge = driver.find_element(By.CLASS_NAME,"shopping_cart_badge")

print("Cart Count:",[Link])

[Link]()

[Link] a Selenium script that logs into SauceDemo, waits for the product
titles to load, fetches all product names, and prints them.

from selenium import webdriver

from [Link] import By

from [Link] import WebDriverWait

from [Link] import expected_conditions as EC

driver = [Link]()
[Link]("[Link]

driver.find_element([Link],"user-name").send_keys("standard_user")

driver.find_element([Link],"password").send_keys("secret_sauce")

driver.find_element([Link],"login-button").click()

wait = WebDriverWait(driver,10)

products =
[Link](EC.presence_of_all_elements_located((By.CLASS_NAME,"inventor
y_item_name")))

for p in products:

print([Link])

[Link]()

41. Write a Selenium Python script that opens the login page of Practice
Test Automation ([Link]
login/), enters username and password using ID locator, clicks the login
button, and if an alert appears accepts it and prints the alert message.

from selenium import webdriver

from [Link] import By

import time

driver = [Link]()

[Link]("[Link]

driver.find_element([Link],"username").send_keys("student")

driver.find_element([Link],"password").send_keys("Password123")

driver.find_element([Link],"submit").click()
[Link](3)

try:

alert = driver.switch_to.alert

print([Link])

[Link]()

except:

print("No Alert Found")

[Link]()

42. Write a Selenium Python script that opens the dropdown page of The
Internet Herokuapp ([Link]
selects Option 1 using visible text, retrieves the selected option, and
prints “Dropdown selection successful” if the selected value matches
Option 1.

from selenium import webdriver

from [Link] import By

from [Link] import Select

driver = [Link]()

[Link]("[Link]

dropdown = Select(driver.find_element([Link],"dropdown"))

dropdown.select_by_visible_text("Option 1")

selected = dropdown.first_selected_option.text
if selected == "Option 1":

print("Dropdown selection successful")

[Link]()

[Link] a Selenium Python script that opens the frames page of


DemoQA ([Link] switches to frame1, retrieves the
heading text inside the frame, and verifies that the text contains “sample
page”.

from selenium import webdriver

from [Link] import By

driver = [Link]()

[Link]("[Link]

driver.switch_to.frame("frame1")

text = driver.find_element([Link],"sampleHeading").text

if "sample page" in [Link]():

print("Text Verified:",text)

[Link]()

[Link] a Selenium Python script that opens the multiple windows page
of The Internet Herokuapp
([Link] clicks Click Here, switches
to the newly opened window, and prints the title of that window.

from selenium import webdriver


from [Link] import By

driver = [Link]()

[Link]("[Link]

driver.find_element(By.LINK_TEXT,"Click Here").click()

windows = driver.window_handles

driver.switch_to.window(windows[1])

print([Link])

[Link]()

45. Write a Selenium Python script that opens the multiple windows page
of The Internet Herokuapp
([Link] opens a new window,
switches to the child window, prints the title, closes the child window, and
switches back to the parent window.

from selenium import webdriver

from [Link] import By

driver = [Link]()

[Link]("[Link]

parent = driver.current_window_handle
driver.find_element(By.LINK_TEXT,"Click Here").click()

windows = driver.window_handles

for w in windows:

if w != parent:

driver.switch_to.window(w)

print("Child Title:",[Link])

[Link]()

driver.switch_to.window(parent)

print("Back to Parent:",[Link])

[Link]()

46. Write a Selenium Python script that opens the dropdown page of The
Internet Herokuapp ([Link]
retrieves all dropdown options, prints them in the console, and selects
Option 2 using the index method.

from selenium import webdriver

from [Link] import By

from [Link] import Select

driver = [Link]()

[Link]("[Link]

dropdown = Select(driver.find_element([Link],"dropdown"))
options = [Link]

for o in options:

print([Link])

dropdown.select_by_index(2)

[Link]()

47. Write a Selenium Python script that opens the JavaScript alerts page of
The Internet Herokuapp
([Link] clicks JS Prompt,
enters a text value inside the alert, accepts the alert, and prints the result
message displayed on the page.

from selenium import webdriver

from [Link] import By

import time

driver = [Link]()

[Link]("[Link]

driver.find_element([Link],"//button[text()='Click for JS
Prompt']").click()

alert = driver.switch_to.alert

alert.send_keys("Hello Selenium")

[Link]()

[Link](2)
result = driver.find_element([Link],"result").text

print(result)

[Link]()

48. Write a Selenium Python script that opens the iframe editor page of
The Internet Herokuapp ([Link]
switches to the iframe, clears the existing text inside the textbox, enters
new text, and prints the entered text.

from selenium import webdriver

from [Link] import By

driver = [Link]()

[Link]("[Link]

driver.switch_to.frame("mce_0_ifr")

textbox = driver.find_element([Link],"tinymce")

[Link]()

textbox.send_keys("Hello Selenium Automation")

print([Link])

[Link]()

49. Write a Selenium Python script that opens the buttons page of
DemoQA ([Link] performs a double click action
on the double-click button using ActionChains, and prints the message
displayed after the action.
from selenium import webdriver

from [Link] import By

from [Link].action_chains import ActionChains

driver = [Link]()

[Link]("[Link]

button = driver.find_element([Link],"doubleClickBtn")

actions = ActionChains(driver)

actions.double_click(button).perform()

msg = driver.find_element([Link],"doubleClickMessage").text

print(msg)

[Link]()

50. Write a Selenium Python script that opens the buttons page of
DemoQA ([Link] performs a right click on the
right-click button using ActionChains, captures the success message
displayed, and prints it.

from selenium import webdriver

from [Link] import By

from [Link].action_chains import ActionChains

driver = [Link]()
[Link]("[Link]

button = driver.find_element([Link],"rightClickBtn")

actions = ActionChains(driver)

actions.context_click(button).perform()

msg = driver.find_element([Link],"rightClickMessage").text

print(msg)

[Link]()

51. Write a Selenium Python script that opens Google Search


([Link] enters “Selenium Automation” in the search
box, presses ENTER using the Keys class, and prints the title of the
search results page.

from selenium import webdriver

from [Link] import By

from [Link] import Keys

driver = [Link]()

[Link]("[Link]

search = driver.find_element([Link],"q")

search.send_keys("Selenium Automation")
search.send_keys([Link])

print([Link])

[Link]()

[Link] a Selenium Python script that opens the form page of DemoQA
([Link] uses the TAB key to move between input
fields, enters values in each field using keyboard actions, and submits the
form.

from selenium import webdriver

from [Link] import By

from [Link] import Keys

driver = [Link]()

[Link]("[Link]

name = driver.find_element([Link],"userName")

name.send_keys("Mahesh")

name.send_keys([Link])

driver.switch_to.active_element.send_keys("mahesh@[Link]")

driver.switch_to.active_element.send_keys([Link])

driver.switch_to.active_element.send_keys("Chennai")

driver.switch_to.active_element.send_keys([Link])

driver.switch_to.active_element.send_keys("Tamil Nadu")
driver.find_element([Link],"submit").click()

[Link]()

53. Write a Selenium Python script that opens the file upload page of The
Internet Herokuapp ([Link]
uploads an image file using send_keys, clicks the upload button, and
prints the uploaded file name displayed on the page.

from selenium import webdriver

from [Link] import By

driver = [Link]()

[Link]("[Link]

file_input = driver.find_element([Link],"file-upload")

file_input.send_keys("C:\\Users\\Mahesh\\Desktop\\[Link]")

driver.find_element([Link],"file-submit").click()

filename = driver.find_element([Link],"uploaded-files").text

print("Uploaded File:",filename)

[Link]()

54. Write a Selenium Python script that opens the multiple windows page
of The Internet Herokuapp
([Link] opens a new window,
switches to the child window, selects an option from a dropdown available
on the page, and prints the selected value.

from selenium import webdriver

from [Link] import By

from [Link] import Select

driver = [Link]()

[Link]("[Link]

driver.find_element(By.LINK_TEXT,"Click Here").click()

windows = driver.window_handles

driver.switch_to.window(windows[1])

# Example dropdown code

# (if dropdown exists)

[Link]()

55. Write a Selenium Python script that opens the nested frames page of
DemoQA ([Link] switches into the iframe
containing a dropdown, selects an option from the dropdown, and prints
the selected value.

from selenium import webdriver

from [Link] import By

driver = [Link]()
[Link]("[Link]

driver.switch_to.frame(driver.find_element([Link],"frame1"))

print(driver.find_element(By.TAG_NAME,"body").text)

[Link]()

56. Write a Selenium Python script that opens the multiple windows page
of The Internet Herokuapp
([Link] opens a new window, clicks
a button that triggers an alert, accepts the alert, and prints the alert
message.

from selenium import webdriver

from [Link] import By

driver = [Link]()

[Link]("[Link]

driver.find_element([Link],"//button[text()='Click for JS Alert']").click()

alert = driver.switch_to.alert

print([Link])

[Link]()

[Link]()
57. Write a Selenium Python script that opens the menu page of DemoQA
([Link] performs a mouse hover action on a menu
item, clicks the submenu that appears, and prints the submenu text.

from selenium import webdriver

from [Link] import By

from [Link].action_chains import ActionChains

driver = [Link]()

[Link]("[Link]

menu = driver.find_element([Link],"//a[text()='Main Item 2']")

actions = ActionChains(driver)

actions.move_to_element(menu).perform()

submenu = driver.find_element([Link],"//a[text()='SUB SUB LIST »']")

print([Link])

[Link]()

58. Write a Selenium Python script that opens the login page of Practice
Test Automation ([Link]
login/), enters username and password, presses ENTER using keyboard
actions instead of clicking the login button, and prints the URL after
login.

from selenium import webdriver

from [Link] import By


from [Link] import Keys

driver = [Link]()

[Link]("[Link]

driver.find_element([Link],"username").send_keys("student")

driver.find_element([Link],"password").send_keys("Password123")

driver.find_element([Link],"password").send_keys([Link])

print(driver.current_url)

[Link]()

59. Write a Selenium Python script that opens the iframe editor page of
The Internet Herokuapp ([Link]
switches into the iframe, clicks a button inside the frame that triggers an
alert, and accepts the alert.

from selenium import webdriver

from [Link] import By

driver = [Link]()

[Link]("[Link]

driver.switch_to.frame("mce_0_ifr")

print("Inside Frame")
driver.switch_to.default_content()

[Link]()

60. Write a Selenium Python script that opens DemoQA


([Link] clicks a link that opens a new window, switches to
the new window, performs a mouse hover action, selects a value from a
dropdown, and prints the title of the page.

from selenium import webdriver

from [Link] import By

from [Link].action_chains import ActionChains

from [Link] import Select

driver = [Link]()

[Link]("[Link]

# Example window opening

driver.execute_script("[Link]('[Link]

windows = driver.window_handles

driver.switch_to.window(windows[1])

dropdown = Select(driver.find_element([Link],"oldSelectMenu"))

dropdown.select_by_visible_text("Blue")

print([Link])
[Link]()

Data Driven Testing (DDT) – Scenario Questions

Write a Selenium Python script that reads username and password


from an Excel file, opens SauceDemo, enters the credentials, clicks
login, and prints whether the login is successful or failed for each row in
the Excel sheet.

from selenium import webdriver

from [Link] import By

from openpyxl import load_workbook

import time

workbook = load_workbook("login_data.xlsx")

sheet = [Link]

driver = [Link]()

driver.maximize_window()

rows = sheet.max_row

for i in range(2, rows+1):

username = [Link](row=i,column=1).value

password = [Link](row=i,column=2).value
[Link]("[Link]

driver.find_element([Link],"user-name").clear()

driver.find_element([Link],"user-name").send_keys(username)

driver.find_element([Link],"password").clear()

driver.find_element([Link],"password").send_keys(password)

driver.find_element([Link],"login-button").click()

[Link](2)

if "inventory" in driver.current_url:

print(username,"Login Successful")

else:

print(username,"Login Failed")

[Link]()

Write a Selenium Python script that reads multiple search keywords


from an Excel file, opens Google Search, performs a search for each
keyword, and prints the title of the search results page.

from selenium import webdriver

from [Link] import By

from [Link] import Keys

from openpyxl import load_workbook

import time
workbook = load_workbook("search_data.xlsx")

sheet = [Link]

driver = [Link]()

rows = sheet.max_row

for i in range(2, rows+1):

keyword = [Link](row=i,column=1).value

[Link]("[Link]

search = driver.find_element([Link],"q")

search.send_keys(keyword)

search.send_keys([Link])

[Link](2)

print("Keyword:",keyword)

print("Title:",[Link])

[Link]()

Write a Selenium Python script that reads multiple login credentials


from an Excel sheet, opens Practice Test Automation login page,
attempts login for each credential, and writes PASS or FAIL back to the
Excel file.
from selenium import webdriver

from [Link] import By

from openpyxl import load_workbook

import time

workbook = load_workbook("login_data.xlsx")

sheet = [Link]

driver = [Link]()

rows = sheet.max_row

for i in range(2, rows+1):

username = [Link](i,1).value

password = [Link](i,2).value

[Link]("[Link]

driver.find_element([Link],"username").send_keys(username)

driver.find_element([Link],"password").send_keys(password)

driver.find_element([Link],"submit").click()

[Link](2)

if "logged-in-successfully" in driver.current_url:

[Link](i,3).value="PASS"
else:

[Link](i,3).value="FAIL"

[Link]("login_data.xlsx")

[Link]()

Write a Selenium Python script that reads different URLs from an Excel
file, opens each URL in the browser, and prints the page title and
current URL.

from selenium import webdriver

from openpyxl import load_workbook

workbook = load_workbook("[Link]")

sheet = [Link]

driver = [Link]()

rows = sheet.max_row

for i in range(2,rows+1):

url = [Link](i,1).value

[Link](url)

print("URL:",driver.current_url)

print("Title:",[Link])
[Link]()

Write a Selenium Python script that reads different values from Excel,
enters them into a textbox on DemoQA, submits the form, and verifies
whether the entered data is displayed correctly.

from selenium import webdriver

from [Link] import By

from openpyxl import load_workbook

import time

workbook = load_workbook("form_data.xlsx")

sheet = [Link]

driver = [Link]()

rows = sheet.max_row

for i in range(2,rows+1):

name = [Link](i,1).value

[Link]("[Link]

driver.find_element([Link],"userName").send_keys(name)

driver.find_element([Link],"submit").click()
[Link](2)

output = driver.find_element([Link],"name").text

if name in output:

print("Data Verified")

[Link]()

Keyword Driven Testing (KDT) – Scenario Questions

Write a Selenium Python script that reads keywords from an Excel


sheet such as OpenBrowser, Navigate, EnterText, Click,
CloseBrowser, and performs login automation on SauceDemo based on
those keywords.

Example Excel:

Ste
Keyword Locator Value
p

OpenBrowse
1
r

https://
2 Navigate
[Link]

id=user-
3 EnterText standard_user
name

4 EnterText id=password secret_sauce

id=login-
5 Click
button

CloseBrows
6
er
from selenium import webdriver

from [Link] import By

from openpyxl import load_workbook

workbook = load_workbook("[Link]")

sheet = [Link]

driver=None

rows = sheet.max_row

for i in range(2,rows+1):

keyword = [Link](i,2).value

locator = [Link](i,3).value

value = [Link](i,4).value

if keyword == "OpenBrowser":

driver = [Link]()

elif keyword == "Navigate":

[Link](value)

elif keyword == "EnterText":

loc_type,loc_value = [Link]("=")

if loc_type == "id":
driver.find_element([Link],loc_value).send_keys(value)

elif keyword == "Click":

loc_type,loc_value = [Link]("=")

if loc_type == "id":

driver.find_element([Link],loc_value).click()

elif keyword == "CloseBrowser":

[Link]()

Data Driven Mode: Example that teach in class:

from selenium import webdriver


from [Link] import By
from openpyxl import load_workbook
import pytest
import allure

wb = load_workbook("test_data.xlsx")
sheet = [Link]

test_data = []

for row in range(2, sheet.max_row + 1):


username = [Link](row, 1).value
password = [Link](row, 2).value
test_data.append((username, password))

@[Link]("Login")
@[Link]("User Authentication")
@[Link]("username,password", test_data)
def test_login(username, password):
driver = [Link]()

with [Link]("Navigate to login page"):


[Link]("[Link]

with [Link]("Enter username and password"):


driver.find_element([Link], "username").send_keys(username)
driver.find_element([Link], "password").send_keys(password)

with [Link]("Click login button"):


driver.find_element(By.CLASS_NAME, "radius").click()

with [Link]("Verify login result"):


if username == "tomsmith" and password ==
"SuperSecretPassword!":
assert "secure" in driver.current_url
else:
assert "secure" not in driver.current_url

[Link]()

You might also like