diff --git a/amazon_search_script.py b/amazon_search_script.py new file mode 100644 index 000000000..841c75390 --- /dev/null +++ b/amazon_search_script.py @@ -0,0 +1,22 @@ +from selenium import webdriver +from selenium.webdriver.common.by import By +from time import sleep +from selenium.webdriver.chrome.service import Service + +# driver = webdriver.Chrome(executable_path='/Users/svetlanalevinsohn/JobEasy/13-python-selenium-automation/chromedriver') +service = Service('/Users/svetlanalevinsohn/JobEasy/13-python-selenium-automation/chromedriver') +driver = webdriver.Chrome(service=service) + +driver.get('https://www.amazon.com/') + +driver.find_element(By.ID, 'twotabsearchtextbox').send_keys('table') +driver.find_element(By.ID, 'nav-search-submit-button').click() + +expected_result = '"table"' +actual_result = driver.find_element(By.XPATH, "//span[@class='a-color-state a-text-bold']").text +print(actual_result) + +assert expected_result == actual_result, f'Expected {expected_result} but got actual {actual_result}' +print('Test case passed') + +driver.quit() diff --git a/chromedriver b/chromedriver new file mode 100755 index 000000000..f7e9dc7f4 Binary files /dev/null and b/chromedriver differ diff --git a/locators.py b/locators.py new file mode 100644 index 000000000..31c20fbfc --- /dev/null +++ b/locators.py @@ -0,0 +1,39 @@ +from selenium import webdriver +from selenium.webdriver.common.by import By +from selenium.webdriver.chrome.service import Service + +driver = webdriver.Chrome(executable_path='/Users/svetlanalevinsohn/JobEasy/13-python-selenium-automation/chromedriver') + +# By ID +driver.find_element(By.ID, 'twotabsearchtextbox') + +# By Xpath, tag and attribute +driver.find_element(By.XPATH, "//input[@placeholder='Search Amazon']") +driver.find_element(By.XPATH, "//input[@aria-label='Search Amazon']") +driver.find_element(By.XPATH, "//img[@alt='PAVOI Jewelry']") + +# By Xpath, multiple attr +driver.find_element(By.XPATH, "//a[@aria-label='Amazon' and @href='/ref=nav_logo']") +driver.find_element(By.XPATH, "//a[@href='/gp/bestsellers/?ref_=nav_cs_bestsellers' and @data-csa-c-content-id='nav_cs_bestsellers' and @data-csa-c-type='link']") + +# By Xpath, contains: +driver.find_element(By.XPATH, "//a[contains(@href, 'nav_cs_bestsellers')]") +# contains AND attr +driver.find_element(By.XPATH, "//a[contains(@href, 'bestsellers') and @data-csa-c-type='link']") + +# By Xpath, without a tag +driver.find_element(By.XPATH, "//*[contains(@href, 'bestsellers') and @data-csa-c-type='link']") +driver.find_element(By.XPATH, "//*[@aria-label='Search Amazon']") + +# By xpath, attr starts with certain value: +driver.find_element(By.XPATH, '//a[starts-with(@href, "/gp/bestsellers/?")]') + +# By xpath, text +driver.find_element(By.XPATH, "//h2[text()='The warm-weather edit']") +# Contains text: +driver.find_element(By.XPATH, "//h2[contains(text(), 'The warm-weather')]") +driver.find_element(By.XPATH, "//a[text()='Best Sellers' and @class='nav-a ']") + +# By xpath, going from parent node ==> child +driver.find_element(By.XPATH, "//div[@id='nav-xshop']//a[text()='Best Sellers']") + diff --git a/sample_script.py b/sample_script.py index 5d9332048..7ea10c096 100755 --- a/sample_script.py +++ b/sample_script.py @@ -3,7 +3,7 @@ from selenium.webdriver.common.by import By # init driver -driver = webdriver.Chrome() +driver = webdriver.Chrome(executable_path='/Users/svetlanalevinsohn/JobEasy/13-python-selenium-automation/chromedriver') driver.maximize_window() # open the url