2017-01-23 46 views
1

我試過xpath,name,id但不知道爲什麼它不起作用。每次我嘗試,它只是說它無法找到元素。硒不能找到元素? (自動化的網絡應用程序)

from selenium import webdriver 
from selenium.common.exceptions import TimeoutException 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 



def page_is_loaded(driver): 
    return driver.find_element_by_tag_name("body") != None 
driver = webdriver.Chrome('/Users/mattcasey/Desktop/SPLASHFORCE/chromedriver') 
driver.get("https://www.adidas.co.uk/on/demandware.store/Sites-adidas-GB-Site/en_GB/MyAccount-Register") 
wait = WebDriverWait(driver, 10) 
wait.until(page_is_loaded) 
first_name_field = driver.find_element_by_name("profile_customer_firstname") 
first_name_field.send_keys("Test") 
+0

好,複製並粘貼您的網址返回'URL不found'網站,所以請檢查是否有正確的地址。 –

+0

它的這種網站格式化的空間,「https://www.adidas.co.uk/on/demandware.store/Sites-adidas-GB-Site/en_GB/MyAccount-Register」是網站和硒加載現場罰款。 –

回答

1

您搜索的元素有id屬性與profile_customer_firstname值,但不name,所以你應該使用

first_name_field = driver.find_element_by_id("profile_customer_firstname") 

此外,它位於一個iframe內。嘗試:

driver.switch_to_frame(driver.find_element_by_xpath("//iframe[@class='sso-iframe']")) 
first_name_field = driver.find_element_by_id("profile_customer_firstname") 
+0

說沒有這樣的元素 –

+0

檢查更新的答案 – Andersson

+0

就是這樣!謝謝 ! –

0

這種嘗試:

first_name_field = driver.find_element_by_xpath("//input[@id='profile_customer_firstname']") 
+0

還在告訴我 - 消息:沒有這樣的元素:無法找到元素:{「method」:「xpath」,「selector」:「// input [@ id ='profile_customer_firstname']」} –

+0

這是因爲web isnt加載,你page_is_loaded必須找到另一個元素,如driver.find_element_by_tag_xpath(「// div [@ class'registration']」) – Wonka