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")
New Selenium IDE A hyperlink on a page is identified with the anchor tag. To click a link, we can use the link text locator which matches the text enclosed within the anchor tag. We can also use the partial link text locator which matches the text enclosed within the anchor tag partially.
We can perform mouseover action on elements in Selenium with the help of Actions class. In order to perform the mouse movement we will use moveToElement () method of the Actions class. Finally use build(). perform() to execute all the steps.
A linkText is used to identify the hyperlinks on a web page. It can be determined with the help of an anchor tag (<a>). In order to create the hyperlinks on a web page, you can use anchor tags followed by the linkText.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With