{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9cb6e86a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "from selenium.webdriver.common.keys import Keys\n",
    "from selenium.webdriver.common.action_chains import ActionChains\n",
    "from selenium.webdriver.support.ui import WebDriverWait, Select\n",
    "from selenium.webdriver.support import expected_conditions as EC"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "902d7ea7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Google\n"
     ]
    }
   ],
   "source": [
    "#TASK1: Open a Website and Verify the Title\n",
    "driver = webdriver.Chrome()\n",
    "driver.implicitly_wait(10)\n",
    "driver.get(\"https://www.google.com\")\n",
    "print(driver.title)\n",
    "assert driver.title == \"Google\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "43c58955",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Daamia - Google Search\n"
     ]
    }
   ],
   "source": [
    "#TASK2: Perform a Search on Google\n",
    "driver.get(\"https://www.google.com\")\n",
    "driver.find_element(By.NAME, \"q\").send_keys(\"Daamia\", Keys.ENTER)\n",
    "WebDriverWait(driver, 10).until(EC.title_contains(\"Daamia\"))\n",
    "print(driver.title)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "40455e1b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "You logged into a secure area!\n",
      "×\n"
     ]
    }
   ],
   "source": [
    "#TASK3: Fill Out a Login Form\n",
    "driver.get(\"https://the-internet.herokuapp.com/login\")\n",
    "driver.find_element(By.ID, \"username\").send_keys(\"tomsmith\")\n",
    "driver.find_element(By.ID, \"password\").send_keys(\"SuperSecretPassword!\")\n",
    "driver.find_element(By.CSS_SELECTOR, \"button[type='submit']\").click()\n",
    "print(WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, \"flash\"))).text)\n",
    "assert \"You logged into a secure area!\" in driver.find_element(By.ID, \"flash\").text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2f80bdf0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I am a JS Alert\n",
      "You clicked: Cancel\n",
      "You entered: Selenium\n"
     ]
    }
   ],
   "source": [
    "#TASK4: Handle Alerts\n",
    "driver.get(\"https://the-internet.herokuapp.com/javascript_alerts\")\n",
    "driver.find_element(By.XPATH, \"//button[text()='Click for JS Alert']\").click()\n",
    "print(WebDriverWait(driver, 10).until(EC.alert_is_present()).text)\n",
    "driver.switch_to.alert.accept()\n",
    "driver.find_element(By.XPATH, \"//button[text()='Click for JS Confirm']\").click()\n",
    "WebDriverWait(driver, 10).until(EC.alert_is_present()).dismiss()\n",
    "print(driver.find_element(By.ID, \"result\").text)\n",
    "driver.find_element(By.XPATH, \"//button[text()='Click for JS Prompt']\").click()\n",
    "WebDriverWait(driver, 10).until(EC.alert_is_present()).send_keys(\"Shoaib\")\n",
    "driver.switch_to.alert.accept()\n",
    "print(driver.find_element(By.ID, \"result\").text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "task05",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Purple\n"
     ]
    }
   ],
   "source": [
    "#TASK5: Automate a Dropdown\n",
    "driver.get(\"https://demoqa.com/select-menu\")\n",
    "Select(driver.find_element(By.ID, \"oldSelectMenu\")).select_by_index(2)\n",
    "Select(driver.find_element(By.ID, \"oldSelectMenu\")).select_by_value(\"4\")\n",
    "Select(driver.find_element(By.ID, \"oldSelectMenu\")).select_by_visible_text(\"Purple\")\n",
    "print(Select(driver.find_element(By.ID, \"oldSelectMenu\")).first_selected_option.text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "task06",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SP23-BSE-101.ipynb\n"
     ]
    }
   ],
   "source": [
    "#TASK6: Automate File Upload\n",
    "driver.get(\"https://the-internet.herokuapp.com/upload\")\n",
    "driver.find_element(By.ID, \"file-upload\").send_keys(os.path.abspath(\"SP23-BSE-101.ipynb\"))\n",
    "driver.find_element(By.ID, \"file-submit\").click()\n",
    "print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.ID, \"uploaded-files\"))).text)\n",
    "assert driver.find_element(By.ID, \"uploaded-files\").text == \"SP23-BSE-101.ipynb\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "task07",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Smith John jsmith@gmail.com $50.00 http://www.jsmith.com edit delete\n",
      "Bach Frank fbach@yahoo.com $51.00 http://www.frank.com edit delete\n",
      "Doe Jason jdoe@hotmail.com $100.00 http://www.jdoe.com edit delete\n",
      "Conway Tim tconway@earthlink.net $50.00 http://www.timconway.com edit delete\n"
     ]
    }
   ],
   "source": [
    "#TASK7: Extract Data from a Table\n",
    "driver.get(\"https://the-internet.herokuapp.com/tables\")\n",
    "for row in driver.find_elements(By.CSS_SELECTOR, \"#table1 tbody tr\"):\n",
    "    print(row.text)\n",
    "assert \"jsmith@gmail.com\" in driver.find_element(By.ID, \"table1\").text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "task08",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Your content goes here.\n",
      "An iFrame containing the TinyMCE WYSIWYG Editor\n"
     ]
    }
   ],
   "source": [
    "#TASK8: Handle Frames\n",
    "driver.get(\"https://the-internet.herokuapp.com/iframe\")\n",
    "driver.switch_to.frame(\"mce_0_ifr\")\n",
    "driver.find_element(By.ID, \"tinymce\").send_keys(\" Hello from Selenium\")\n",
    "print(driver.find_element(By.ID, \"tinymce\").text)\n",
    "driver.switch_to.default_content()\n",
    "print(driver.find_element(By.TAG_NAME, \"h3\").text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "task09",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello World!\n"
     ]
    }
   ],
   "source": [
    "#TASK9: Handle Dynamic Elements\n",
    "driver.get(\"https://the-internet.herokuapp.com/dynamic_loading/1\")\n",
    "driver.find_element(By.CSS_SELECTOR, \"#start button\").click()\n",
    "print(WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, \"finish\"))).text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "task10",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "71\n"
     ]
    }
   ],
   "source": [
    "#TASK10: Automate a Slider\n",
    "driver.get(\"https://demoqa.com/slider\")\n",
    "ActionChains(driver).drag_and_drop_by_offset(driver.find_element(By.CSS_SELECTOR, \"input[type='range']\"), 100, 0).perform()\n",
    "print(driver.find_element(By.ID, \"sliderValue\").get_attribute(\"value\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "task11",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "You hovered over the Button\n"
     ]
    }
   ],
   "source": [
    "#TASK11: Automate Hover Actions\n",
    "driver.get(\"https://demoqa.com/tool-tips\")\n",
    "ActionChains(driver).move_to_element(driver.find_element(By.ID, \"toolTipButton\")).perform()\n",
    "print(WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CLASS_NAME, \"tooltip-inner\"))).text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "task12",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Dropped!\n"
     ]
    }
   ],
   "source": [
    "#TASK12: Automate Drag and Drop\n",
    "driver.get(\"https://demoqa.com/droppable\")\n",
    "ActionChains(driver).drag_and_drop(WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, \"#draggable.ui-draggable\"))), driver.find_element(By.ID, \"droppable\")).perform()\n",
    "print(driver.find_element(By.ID, \"droppable\").text)\n",
    "assert driver.find_element(By.ID, \"droppable\").text == \"Dropped!\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "task13",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A Light in the Attic - £51.77\n",
      "Tipping the Velvet - £53.74\n",
      "Soumission - £50.10\n",
      "Sharp Objects - £47.82\n",
      "Sapiens: A Brief History of Humankind - £54.23\n",
      "The Requiem Red - £22.65\n",
      "The Dirty Little Secrets of Getting Your Dream Job - £33.34\n",
      "The Coming Woman: A Novel Based on the Life of the Infamous Feminist, Victoria Woodhull - £17.93\n",
      "The Boys in the Boat: Nine Americans and Their Epic Quest for Gold at the 1936 Berlin Olympics - £22.60\n",
      "The Black Maria - £52.15\n",
      "Starving Hearts (Triangular Trade Trilogy, #1) - £13.99\n",
      "Shakespeare's Sonnets - £20.66\n",
      "Set Me Free - £17.46\n",
      "Scott Pilgrim's Precious Little Life (Scott Pilgrim #1) - £52.29\n",
      "Rip it Up and Start Again - £35.02\n",
      "Our Band Could Be Your Life: Scenes from the American Indie Underground, 1981-1991 - £57.25\n",
      "Olio - £23.88\n",
      "Mesaerion: The Best Science Fiction Stories 1800-1849 - £37.59\n",
      "Libertarianism for Beginners - £51.33\n",
      "It's Only the Himalayas - £45.17\n"
     ]
    }
   ],
   "source": [
    "#TASK13: Automate Web Scraping with Selenium\n",
    "driver.get(\"https://books.toscrape.com\")\n",
    "for product in driver.find_elements(By.CLASS_NAME, \"product_pod\"):\n",
    "    print(product.find_element(By.CSS_SELECTOR, \"h3 a\").get_attribute(\"title\"), \"-\", product.find_element(By.CLASS_NAME, \"price_color\").text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "task14",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "Verification Success... Hooray!\n"
     ]
    }
   ],
   "source": [
    "#TASK14: Automate CAPTCHA\n",
    "driver.get(\"https://www.google.com/recaptcha/api2/demo\")\n",
    "print(driver.find_element(By.CSS_SELECTOR, \"iframe[title='reCAPTCHA']\").is_displayed())\n",
    "input(\"Solve the CAPTCHA in the browser, then press Enter\")\n",
    "driver.find_element(By.ID, \"recaptcha-demo-submit\").click()\n",
    "print(driver.find_element(By.CLASS_NAME, \"recaptcha-success\").text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "task15",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "HP 2026 Everyday Laptop, Intel CPU, 128GB Storage, Copilot AI, Office 365\n",
      "Lenovo 15.6\" FHD Essential Laptop, Intel Processor, 8GB DDR5, 128GB Storage\n",
      "HP 15.6\" FHD Touchscreen Laptop, AMD Ryzen 7 7730U, 32GB RAM 1TB SSD\n",
      "HP 14\" Laptop Computer 2026, Intel N150 CPU, Office 365, EXT 500GB HDD\n",
      "Acer Nitro V 16S AI Gaming Laptop | AMD Ryzen 7 260 Processor | NVIDIA GeForce RTX 5060 Laptop GPU (572 AI Tops) | 16\" WUXGA IPS 180Hz Display | 32GB DDR5 | 1TB Gen 4 SSD | Wi-Fi 6 | ANV16S-41-R2AJ\n",
      "HP OmniBook 3 16 inch Laptop PC, 2K Touchscreen, AMD Ryzen 5 40, 8 GB RAM, 512 GB SSD, AMD Radeon 610M GPU, Windows 11 Home, Glacier Silver, 16-by0499nr\n",
      "Lenovo Business 15.6\" FHD Laptop, Intel Processor, 8GB DDR5, 128GB Storage\n",
      "HP OmniBook 3 16 inch Laptop PC, 2K Touchscreen, AMD Ryzen 3 30, 8 GB RAM, 512 GB SSD, AMD Radeon 610M GPU, Windows 11 Home, Mica Silver, 16-by0299nr\n",
      "HP OmniBook Touchscreen Laptop, 32GB DDR5 RAM 1TB SSD, AMD Ryzen AI 7 445\n",
      "HP 2026 Everyday Laptop, Intel CPU, 128GB Storage, Copilot AI, Office 365\n",
      "Dell 15.6 Laptop, FHD, Intel Core Ultra 5 225U, 16GB RAM, Windows 11 Home\n",
      "\n",
      "\n",
      "Lenovo Business 15.6\" FHD Laptop, Intel Processor, 8GB DDR5, 128GB Storage\n",
      "HP 17.3\" Touchscreen Laptop Computer, Intel Core Ultra 7 Processor, 16GB RAM 1TB Storage (512GB SSD+512GB External Docking Set), Win11 Pro, FP Reader, Backlit KB, PLUSERA Earphones, Moonlight Blue\n",
      "HP OmniBook 3 17.3 inch Laptop PC, FHD Display, Intel Core Ultra 5 225U, 8 GB RAM, 512 GB SSD, Intel Graphics, Windows 11 Home, Glacier Silver, 17-dg0099nr\n",
      "Acer Chromebook 15.6\" FHD 1080p Laptop, 2026 Edition, Dual-Core Intel Celeron Processor, 64GB Storage, Sleeve, WiFi 6, 10-Hour Battery, Glare-Reducing Webcam, for Education, K12 & Everyday Use\n",
      "HP Ultrabook 14 Laptop Computer Business Study & Home 2026, MS Office for The Web + Windows 11 Home, Quad-Core Intel CPU, 128GB SSD, WiFi 6, Rose Gold\n"
     ]
    }
   ],
   "source": [
    "#TASK15: Test a Search Feature\n",
    "driver.get(\"https://www.amazon.com\")\n",
    "WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, \"twotabsearchtextbox\"))).send_keys(\"laptops\", Keys.ENTER)\n",
    "for title in WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, \"[data-component-type='s-search-result'] h2\"))):\n",
    "    print(title.text)\n",
    "assert any(\"laptop\" in title.text.lower() for title in driver.find_elements(By.CSS_SELECTOR, \"[data-component-type='s-search-result'] h2\"))\n",
    "driver.quit()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python (Selenium Task)",
   "language": "python",
   "name": "selenium-task"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
