2017-06-20 36 views
0

所以我想登錄到https://www.adidas.com/us/myaccount-create-or-login我試過xpath,名稱和ID。所以我問幾個朋友,一個能夠使用ID登錄,但我仍然無法登錄。試圖用硒python登錄到adidas.com

這是我的代碼:

from selenium import webdriver 
import time 
import colorama 
from colorama import Fore, Back, Style 
import datetime 

def getCurrentTime(): 
    return time.strftime("[%H:%M:%S]") 



def ACO(getCurrentTime): 
    print("{} Configuration Loaded".format(getCurrentTime())) 
    driver = webdriver.Chrome() 
    driver.get("https://www.adidas.com/us/myaccount-create-or-login") 
    time.sleep(5) 
    driver.find_element_by_id("username").send_keys("test123") 
    driver.find_element_by_id("password").send_keys("test123") 
    driver.find_element_by_xpath("""//*[@id="signinSubmit"]""").click() 


    time.sleep(500) 
ACO(getCurrentTime) 

回答

1

登錄表單位於一個iframe內。爲了找到這個表格上的元素,你將不得不切換到iframe的背景:

driver.switch_to.frame("loginaccountframe") 

driver.find_element_by_id("username").send_keys("test123") 
driver.find_element_by_id("password").send_keys("test123") 
driver.find_element_by_id("signinSubmit").click() 

作爲一個側面說明,儘量避免使用硬編碼time.sleep()超時 - 他們是相當不可靠的 - 相反,是明確的有Explicit Waits via WebDriverWait