5

I am trying to click on an option in a hover menu.

http://www.nike.com/us/en_us/c/men

if you click Men in the top nav bar (not the side one), and click tops and t-shirts, that is what I'm trying to do with Selenium webdriver.

I currently am doing this:

# select shirt category
driver.find_element_by_css_selector("span.nsg-font-family--platform.gnav-bar--facet-label").click()
driver.find_element_by_partial_link_text("shirt").click()

But this seems to be choosing an option out of the left nav bar instead of the top one.

How can I click on "tops and t-shirts" in the top nav bar using selenium?

Full example:

driver = self.driver
driver.get(self.base_url)
driver.maximize_window()
wait = WebDriverWait(driver, 10)

# choose country/region
driver.find_element_by_xpath("(//button[@type='button'])[2]").click()
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "li.US a"))).click()

#verify swoosh logo is displayed
logo = driver.find_element(By.CSS_SELECTOR, 'span.nsg-glyph--swoosh.gnav-bar--home-logo')
logo.is_displayed()

# open men's hover menu in top nav bar
men_menu = driver.find_element_by_css_selector("li[data-nav-tracking=men]")

# click shirt
men_menu.find_element_by_partial_link_text("Shirt").click()

# select a shirt for sale
driver.find_element_by_xpath("//div[@id='exp-gridwall-wrapper']/div[2]/div[2]/div[2]/div/div/div/div/div/div[3]/div[2]/p").click()

# opening size dropdown
size_button = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".exp-pdp-size-and-quantity-container a.exp-pdp-size-dropdown")))
actions = ActionChains(driver)
actions.move_to_element(size_button).click().perform()

# selecting size
size = wait.until(EC.visibility_of_element_located((By.XPATH, "//li[contains(@class, 'nsg-form--drop-down--option') and normalize-space(.) = 'XL']")))
actions = ActionChains(driver)
actions.move_to_element(size).click().perform()

# adding to cart
driver.find_element_by_id("buyingtools-add-to-cart-button").click()

# open checkout
checkout_button = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".checkout-button")))
actions = ActionChains(driver)
actions.move_to_element(checkout_button).click().perform()

# select guest checkout
guestlogin_button = wait.until(EC.visibility_of_element_located((By.ID, "ch4_loginGuestBtn")))
actions = ActionChains(driver)
actions.move_to_element(guestlogin_button).click().perform()
# driver.find_element_by_id("ch4_loginGuestBtn").click()

# filling out order form
firstname = driver.find_element_by_id("fname")
firstname.click()
firstname.clear()
firstname.send_keys("testname")
firstname.send_keys(Keys.TAB)

lastname = driver.find_element_by_id("lname")
lastname.send_keys("testname")
lastname.send_keys(Keys.TAB)

address = driver.find_element_by_id("address1Field")
address.send_keys("test address")
address.send_keys(Keys.TAB)

city = driver.find_element_by_id("singleCity")
city.send_keys("testcity")
city.send_keys(Keys.TAB)

state = driver.find_element_by_id("singleState")
state.send_keys("Oregon")
state.send_keys(Keys.TAB)

postalcode = driver.find_element_by_id("postalCodeField")
postalcode.send_keys("97202")
postalcode.send_keys(Keys.TAB)

# click next button
driver.find_element_by_id("shippingSubmit").click()

# switch to billing frame
wait = WebDriverWait(driver, 10)
driver.switch_to.frame("billingFormFrame") 

# enter credit card number
cc = wait.until(EC.element_to_be_clickable((By.ID, "creditCardNumber")))
cc.click()
cc.send_keys("4111111111111111")

# enter expiration
selectmonth = Select(driver.find_element_by_id("expirationMonth"))
selectmonth.select_by_value("10")

# enter expiration year
selectyear = Select(driver.find_element_by_id("expirationYear"))
selectyear.select_by_value("2018")

# enter security code
ccs = driver.find_element_by_id("cvNumber")
ccs.send_keys("111")

# enter phone number
phonenum = driver.find_element_by_id("phoneNumber")
phonenum.send_keys("5035035033")

# enter email
email = driver.find_element_by_id("email")
email.send_keys("[email protected]")

# click submit button
driver.find_element_by_id("billingSubmit").click()

# click place order button
driver.find_element_by_css_selector("input.ch4_btnOrange").click()

time.sleep(9)
# confirm order was succesful
bodyText = driver.find_element_by_tag_name('body').text
self.assertIn("YOUR ORDER WAS PLACED SUCCESSFULLY.", bodyText)
#driver.find_element_by_id("ch4_confirmHeading")

1 Answer 1

10

I think you need to make your search in a context of the "men" menu. Working sample:

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Firefox()
driver.get("http://www.nike.com/us/en_us/c/men")
driver.maximize_window()

wait = WebDriverWait(driver, 10)
actions = ActionChains(driver)

# open men's hover menu in top nav bar
men_menu = driver.find_element_by_css_selector("li[data-nav-tracking=men]")
actions.move_to_element(men_menu).perform()

# click shirt
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "li[data-nav-tracking=men] a[data-subnav-label$=Shirts]"))).click()
Sign up to request clarification or add additional context in comments.

5 Comments

This gives me: NoSuchElementException: Message: Unable to locate element: {"method":"partial link text","selector":"shirt"}
Also when I run this, I do not visually see the mens hover menu open (I'm not sure if I am supposed to see this visually)
It only works sometimes, when it fails i get NoSuchElementException: Message: Unable to locate element: {"method":"partial link text","selector":"Shirt"}
@david okay, could you please do 2 things to help me help: 1. please provide a screenshot and highlight the link you want to click. 2. post the complete code you have so far. Thanks.
@david thanks for that, updated the answer with a working sample. Hope that helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.