2016-05-18 14 views
3

ホバーメニューのオプションをクリックしようとしています。Selenium webdriverでホバーメニューのリンクをクリックするには?

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

あなたは、トップナビゲーションバー(いない側1)男性をクリックして、トップスやTシャツをクリックした場合、それは私がセレン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() 

しかし、これではなく、トップ1の左のナビゲーションバーのアウトオプションを選択しているようです。

セレンを使用してトップナビゲーションバーの「トップスと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("[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") 

答えて

2

私は、あなたが「男性」メニューのコンテキスト内で検索を行うために必要があると思います。ワーキングサンプル:

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

これは私を与える:はNoSuchElementException:メッセージ:要素が見つかりません:{「メソッド」:「部分リンクテキスト」、「セレクタ」:「シャツ」} – david

+0

の方法について「シャツ」の代わりに@デビッドを?.. – alecxe

+0

また、私はこれを実行すると、私は視覚的にメンズホバーメニューを開いて表示されません(私はこの視覚的に見えるはずですかわからない) – david