top of page

learner'sBlog

How to manage Selenium dropdowns without utilizing the Select class?

# Import the 'time' module for sleep functionality
import time

# Import necessary Selenium 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)

# Click on the 'Dropdown' link using XPath
driver.find_element(By.XPATH, "//a[normalize-space()='Dropdown']").click()

# Select 'Option 2' from the dropdown using its ID
driver.find_element(By.XPATH, "//select[@id='dropdown']").send_keys("Option 2")

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

3 views0 comments

How can one capture a screenshot using Selenium in Python?

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

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

# Open the specified website
driver.get("https://www.saucedemo.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)

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

# Locate the username field by ID and enter "standard_user"
driver.find_element(By.ID, "user-name").send_keys("standard_user")

# Locate the password field by ID and enter "secret_sauce"
driver.find_element(By.ID, "password").send_keys("secret_sauce")

# Click the login button using XPath
driver.find_element(By.XPATH, "//input[@id='login-button']").click()

# Pause the script execution for 5 seconds (waiting for the page to load or perform actions)
time.sleep(5)

# Locate the specific element to capture by CSS selector
element_to_capture = driver.find_element(By.CSS_SELECTOR, ".inventory_item:nth-child(6)")

# Take a screenshot of the identified element and save it as screenshot.png"
element_to_capture.screenshot("screenshot.png")

# Alternatively, take a screenshot of the entire page and save it as "screenshot.png"
driver.save_screenshot("screenshot.png")

# Close the browser window
driver.quit()

8 views0 comments

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

8 views0 comments
bottom of page