So, I included the main program without using JavaScript
; but, under the Main Program Reference, I included the method for using JavaScript
as well.
To achieve this solution without JS, I used xpath
to validate that the page loaded successfully. Then, after that, I found the xpath
for the search button
//nav[@class='is-navigation']//span[contains(@class, 'search-btn')]
Once I discovered the xpath
for this, I clicked the search button and then I created a method to search for a specific product. In my example, I used the "Converse" shoes as an example.
def search_for_text(driver : ChromeDriver, text : str):
driver.find_element(By.XPATH, "//form[@id='formSearch']//input[contains(@data-options, 'SearchBox')]").send_keys(text)
time.sleep(2)
if driver.find_element(By.XPATH, "//form[@id='formSearch']//input[contains(@data-options, 'SearchBox')]").get_attribute('value').__len__() != text.__len__():
raise Exception("Failed to populate our search textbox")
else:
driver.find_element(By.XPATH, "//form[@id='formSearch']//input[contains(@data-options, 'SearchBox')]").send_keys(Keys.RETURN)
time.sleep(2)
wait_displayed(driver, "//div[@class='is-pw__products']//div[contains(@class, 'products-list')]", 30)
print(f'Your search for {text} was successful')
In that method, you see that I used wait_displayed
to validate that my product list displays properly.
MAIN PROGRAM - FOR REFERENCE
from selenium import webdriver
from selenium.webdriver.chrome.webdriver import WebDriver as ChromeDriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as DriverWait
from selenium.webdriver.support import expected_conditions as DriverConditions
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.common.keys import Keys
import time
def get_chrome_driver():
"""This sets up our Chrome Driver and returns it as an object"""
path_to_chrome = "F:\Selenium_Drivers\Windows_Chrome87_Driver\chromedriver.exe"
chrome_options = webdriver.ChromeOptions()
# Browser is displayed in a custom window size
chrome_options.add_argument("window-size=1500,1000")
return webdriver.Chrome(executable_path = path_to_chrome,
options = chrome_options)
def wait_displayed(driver : ChromeDriver, xpath: str, int = 5):
try:
DriverWait(driver, int).until(
DriverConditions.presence_of_element_located(locator = (By.XPATH, xpath))
)
except:
raise WebDriverException(f'Timeout: Failed to find {xpath}')
def is_displayed(driver : ChromeDriver, xpath: str, int = 5):
try:
webElement = DriverWait(driver, int).until(
DriverConditions.presence_of_element_located(locator = (By.XPATH, xpath))
)
return True if webElement != None else False
except:
return False
def click_search(driver : ChromeDriver):
driver.find_element(By.XPATH, "//nav[@class='is-navigation']//span[contains(@class, 'search-btn')]").click()
time.sleep(2)
if is_displayed(driver, "//form[@id='formSearch']//input[contains(@data-options, 'SearchBox')]") == False:
raise Exception("Failed to click our search button")
else:
print('You clicked the Search Button Successfully')
def search_for_text(driver : ChromeDriver, text : str):
driver.find_element(By.XPATH, "//form[@id='formSearch']//input[contains(@data-options, 'SearchBox')]").send_keys(text)
time.sleep(2)
if driver.find_element(By.XPATH, "//form[@id='formSearch']//input[contains(@data-options, 'SearchBox')]").get_attribute('value').__len__() != text.__len__():
raise Exception("Failed to populate our search textbox")
else:
driver.find_element(By.XPATH, "//form[@id='formSearch']//input[contains(@data-options, 'SearchBox')]").send_keys(Keys.RETURN)
time.sleep(2)
wait_displayed(driver, "//div[@class='is-pw__products']//div[contains(@class, 'products-list')]", 30)
print(f'Your search for {text} was successful')
# Gets our chrome driver and opens our site
chrome_driver = get_chrome_driver()
chrome_driver.get("https://www.innvictus.com/")
wait_displayed(chrome_driver, "(//nav[@class='is-navigation']//div[contains(@class, 'desktop-menu')]//a[contains(@href, 'lanzamientos')])[1]")
wait_displayed(chrome_driver, "//div[@class='content-page']//div[@class='scrolling-wrapper-flexbox']")
wait_displayed(chrome_driver, "//nav[@class='is-navigation']//span[contains(@class, 'search-btn')]")
click_search(chrome_driver)
search_for_text(chrome_driver, "Converse")
chrome_driver.quit()
chrome_driver.service.stop()
COMMAND FOR CLICKING THE SEARCH BUTTON USING JAVASCRIPT
jsText = "document.querySelector('nav').querySelector('div .is-navigation__main').querySelector('div .is-navigation__main__right').querySelector('span').click()"
driver.execute_script(jsText)
METHOD
def click_search_using_javaScript(driver : ChromeDriver):
jsText = "document.querySelector('nav').querySelector('div .is-navigation__main').querySelector('div .is-navigation__main__right').querySelector('span').click()"
driver.execute_script(jsText)
time.sleep(2)
if is_displayed(driver, "//form[@id='formSearch']//input[contains(@data-options, 'SearchBox')]") == False:
raise Exception("Failed to click our search button")
else:
print('You clicked the Search Button Successfully')