Skip to content

Lesson 2 #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions locators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from time import sleep

# get the path to the ChromeDriver executable
driver_path = ChromeDriverManager().install()

# create a new Chrome browser instance
service = Service(driver_path)
driver = webdriver.Chrome(service=service)
driver.maximize_window()

# IDs:
driver.find_element(By.ID, 'twotabsearchtextbox')
driver.find_element(By.ID, 'searchDropdownBox')

# Xpath
driver.find_element(By.XPATH, "//input[@aria-label='Search Amazon']")
driver.find_element(By.XPATH, "//input[@name='field-keywords']")

# Multiple attr
driver.find_element(By.XPATH, "//input[@tabindex='0' and @placeholder='Search Amazon']")
driver.find_element(By.XPATH, "//select[@class='nav-search-dropdown "
"searchSelect nav-progressive-attrubute nav-progressive-search-dropdown']")
# Partial attr => contains()
driver.find_element(By.XPATH, "//select[contains(@class,'search-dropdown')]")
driver.find_element(By.XPATH, "//select[contains(@class,'search-dropdown') and @ame='url']")

# text()
driver.find_element(By.XPATH, "//span[text()='Winter Sale deals for your home']")
# text() + attr
driver.find_element(By.XPATH, "//span[text()='Winter Sale deals for your home' and "
"@class='a-truncate-full a-offscreen']")

# text() + contains()
driver.find_element(By.XPATH, "//span[contains(text(), ' Sale deals for') and @class='a-truncate-full a-offscreen']")
30 changes: 30 additions & 0 deletions product_search_script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from time import sleep

# get the path to the ChromeDriver executable
driver_path = ChromeDriverManager().install()

# create a new Chrome browser instance
service = Service(driver_path)
driver = webdriver.Chrome(service=service)
driver.maximize_window()

driver.get('https://www.target.com/')
search_word = 'coffee'

# Input search word
driver.find_element(By.ID, 'search').send_keys(search_word)

# Click on search btn
driver.find_element(By.XPATH, "//button[@data-test='@web/Search/SearchButton']").click()
sleep(6)

# Verify search worked:
actual_text = driver.find_element(By.XPATH, "//div[@data-test='resultsHeading']").text
assert search_word in actual_text, f'Expected word {search_word} not in {actual_text}'

print('Test case passed')
driver.quit()