Selenium Practice Examples by GPT 4o
Certainly! Here are some practice examples for Selenium testing using Python, specifically designed to utilize the capabilities of a GPT model like GPT-4.
Example 1: Basic Web Page Interaction
Task:
Open a webpage, locate a search bar, enter a search term, and submit the search.
Steps:
-
Open the browser and navigate to a search engine (e.g., Google).
-
Find the search bar element.
-
Input a search term (e.g., "Selenium testing with Python").
-
Submit the search form.
-
Print the title of the resulting page.
Code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
Set up the WebDriver (assuming Chrome in this example)
driver = webdriver.Chrome()
Step 1: Open Google
driver.get("https://www.google.com")
Step 2: Find the search bar element
search_bar = driver.find_element(By.NAME, "q")
Step 3: Enter a search term
search_bar.send_keys("Selenium testing with Python")
Step 4: Submit the search form
search_bar.send_keys(Keys.RETURN)
Step 5: Print the title of the resulting page
print(driver.title)
Close the browser
driver.quit()
Example 2: Form Filling and Submission
Task:
Navigate to a web form, fill in the required fields, and submit the form.
Steps:
-
Open the browser and navigate to a form page (e.g., a sign-up page).
-
Find and fill out the form fields (e.g., name, email, password).
-
Submit the form.
-
Print a confirmation message or the title of the resulting page.
Code:
from selenium import webdriver
from selenium.webdriver.common.by import By
Set up the WebDriver (assuming Chrome in this example)
driver = webdriver.Chrome()
Step 1: Navigate to the sign-up form page
driver.get("https://www.example.com/signup")
Step 2: Find and fill out the form fields
driver.find_element(By.NAME, "name").send_keys("John Doe")
driver.find_element(By.NAME, "email").send_keys("[email protected]")
driver.find_element(By.NAME, "password").send_keys("securepassword123")
Step 3: Submit the form
driver.find_element(By.NAME, "submit").click()
Step 4: Print a confirmation message or the title of the resulting page
print("Form submitted. Current page title: " + driver.title)
Close the browser
driver.quit()
Example 3: Handling Alerts
Task:
Open a webpage that triggers a JavaScript alert, accept the alert, and print a confirmation message.
Steps:
-
Open the browser and navigate to a page with a JavaScript alert.
-
Trigger the alert.
-
Switch to the alert and accept it.
-
Print a confirmation message.
Code:
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
Set up the WebDriver (assuming Chrome in this example)
driver = webdriver.Chrome()
Step 1: Navigate to a page with a JavaScript alert
driver.get("https://www.example.com/alert")
Step 2: Trigger the alert (assuming there's a button that triggers the alert)
driver.find_element(By.ID, "alertButton").click()
Step 3: Switch to the alert and accept it
alert = driver.switch_to.alert
alert.accept()
Step 4: Print a confirmation message
print("Alert accepted.")
Close the browser
driver.quit()
Example 4: Taking a Screenshot
Task:
Open a webpage, take a screenshot of the page, and save it to a file.
Steps:
-
Open the browser and navigate to a webpage.
-
Take a screenshot of the page.
-
Save the screenshot to a file.
Code:
from selenium import webdriver
Set up the WebDriver (assuming Chrome in this example)
driver = webdriver.Chrome()
Step 1: Navigate to a webpage
driver.get("https://www.example.com")
Step 2: Take a screenshot of the page
screenshot_path = "screenshot.png"
driver.save_screenshot(screenshot_path)
Step 3: Print a confirmation message
print(f"Screenshot saved to {screenshot_path}")
Close the browser
driver.quit()
Example 5: Extracting Text from a Web Page
Task:
Open a webpage, locate a specific element, extract its text content, and print it.
Steps:
-
Open the browser and navigate to a webpage.
-
Find the element containing the text.
-
Extract and print the text content of the element.
Code:
from selenium import webdriver
from selenium.webdriver.common.by import By
Set up the WebDriver (assuming Chrome in this example)
driver = webdriver.Chrome()
Step 1: Navigate to a webpage
driver.get("https://www.example.com")
Step 2: Find the element containing the text
element = driver.find_element(By.ID, "textElementId")
Step 3: Extract and print the text content of the element
print(element.text)
Close the browser
driver.quit()
These examples cover a range of basic Selenium tasks, from interacting with forms and alerts to taking screenshots and extracting text. They provide a solid foundation for practicing Selenium testing with Python.
Csharp Versions
Sure! Below are the equivalent C# examples for Selenium testing based on the provided Python scripts.
Example 1: Basic Web Page Interaction
Task:
Open a webpage, locate a search bar, enter a search term, and submit the search.
Code:
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
class BasicWebPageInteraction
{
static void Main()
{
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://www.google.com");
IWebElement searchBar = driver.FindElement(By.Name("q"));
searchBar.SendKeys("Selenium testing with C#");
searchBar.SendKeys(Keys.Enter);
System.Console.WriteLine(driver.Title);
driver.Quit();
}
}
Example 2: Form Filling and Submission
Task:
Navigate to a web form, fill in the required fields, and submit the form.
Code:
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
class FormFillingAndSubmission
{
static void Main()
{
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://www.example.com/signup");
driver.FindElement(By.Name("name")).SendKeys("John Doe");
driver.FindElement(By.Name("email")).SendKeys("[email protected]");
driver.FindElement(By.Name("password")).SendKeys("securepassword123");
driver.FindElement(By.Name("submit")).Click();
System.Console.WriteLine("Form submitted. Current page title: " + driver.Title);
driver.Quit();
}
}
Example 3: Handling Alerts
Task:
Open a webpage that triggers a JavaScript alert, accept the alert, and print a confirmation message.
Code:
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
class HandlingAlerts
{
static void Main()
{
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://www.example.com/alert");
driver.FindElement(By.Id("alertButton")).Click();
IAlert alert = driver.SwitchTo().Alert();
alert.Accept();
System.Console.WriteLine("Alert accepted.");
driver.Quit();
}
}
Example 4: Taking a Screenshot
Task:
Open a webpage, take a screenshot of the page, and save it to a file.
Code:
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
class TakingScreenshot
{
static void Main()
{
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://www.example.com");
Screenshot screenshot = ((ITakesScreenshot)driver).GetScreenshot();
string screenshotPath = "screenshot.png";
screenshot.SaveAsFile(screenshotPath, ScreenshotImageFormat.Png);
System.Console.WriteLine($"Screenshot saved to {screenshotPath}");
driver.Quit();
}
}
Example 5: Extracting Text from a Web Page
Task:
Open a webpage, locate a specific element, extract its text content, and print it.
Code:
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
class ExtractingText
{
static void Main()
{
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://www.example.com");
IWebElement element = driver.FindElement(By.Id("textElementId"));
System.Console.WriteLine(element.Text);
driver.Quit();
}
}
These C# examples mirror the functionality of the original Python examples, allowing you to practice Selenium testing with a different programming language.
Imported from rifaterdemsahin.com · 2024