如何使用Selenium webdriver点击悬停菜单中的链接?

时间:2016-05-18 18:22:45

标签: python selenium-webdriver

我正在尝试点击悬停菜单中的选项。

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

如果您单击顶部导航栏中的Men(而不是侧面导航栏),然后单击tops和T-shirt,那就是我正在尝试使用Selenium webdriver。

我目前正在这样做:

# 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()

但这似乎是从左侧导航栏而不是顶部导航栏中选择一个选项。

如何使用硒点击顶部导航栏中的“上衣和T恤”?

完整示例:

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("test@test.com")

# 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 个答案:

答案 0 :(得分:4)

我认为你需要在“男人”菜单的上下文中进行搜索。工作样本:

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()