硒找不到元素? (自动化网络应用)

时间:2017-01-23 12:28:49

标签: python python-3.x selenium xpath web

我试过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")

2 个答案:

答案 0 :(得分: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")

答案 1 :(得分:0)

试试这个:

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