forked from careerist-qa/python-selenium-automation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathamazon_locators_L02.py
More file actions
38 lines (33 loc) · 1.69 KB
/
amazon_locators_L02.py
File metadata and controls
38 lines (33 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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()
driver.maximize_window()
# open the url
driver.get('https://www.amazon.com/')
# By ID
driver.find_element(By.ID, 'twotabsearchtextbox') #search text box
driver.find_element(By.ID, 'nav-search-submit-button') #magnify glass
# By XPath
driver.find_element(By.XPATH, "//input[@aria-label='Search Amazon']")
driver.find_element(By.XPATH,"//input[@role='searchbox']")
# By XPath, multiple attributes (shorter, the better)
driver.find_element(By.XPATH, "//input[@tabindex='0' and @name='field-keywords']")
# By XPATH, any tag
driver.find_element(By.XPATH, "//*[@aria-label='Search Amazon']") #[* = wild card/all tag]
# By XPATH, using text (function, not attribute)
driver.find_element(By.XPATH, "//a[text()='Best Sellers' and @class='nav-a ']")
driver.find_element(By.XPATH, "//h2[text()='Luxury bestsellers']")
driver.find_element(By.XPATH, "//a[@class='nav-a ' and text()='Best Sellers']")
driver.find_element(By.XPATH, "//h2[contains(text(), 'Luxury bestsellers')]")
# Partial Text Match (only works if run automation in one language, not in multiple languages)
driver.find_element(By.XPATH, "//h2[contains(text(), 'Luxury')]")
# Partial Attribute Match
driver.find_element(By.XPATH, "//select[@aria-describedby='searchDropdownDescription']")
driver.find_element(By.XPATH, "//select[contains(@class, 'nav-search-dropdown')]")