我试图迭代一个表单来输入每个字段的值。 我使用JSON来保存值,使用for循环来迭代它们
当代码运行时,我收到一条错误消息!
错误消息
"<td title="" class="xdsoft_date xdsoft_day_of_week0 xdsoft_date xdsoft_other_month xdsoft_weekend" data-year="2017" data-month="11" data-date="31"><div>31</div></td><td title="" class="xdsoft_date xdsoft_day_of_week1 xdsoft_date" data-year="2018" data-month="0" data-date="1"><div>1</div></td><td title="" class="xdsoft_date xdsoft_day_of_week2 xdsoft_date" data-year="2018" data-month="0" data-date="2"><div>2</div></td><td title="" class="xdsoft_date xdsoft_day_of_week3 xdsoft_date" data-year="2018" data-month="0" data-date="3"><div>3</div></td><td title="" class="xdsoft_date xdsoft_day_of_week4 xdsoft_date" data-year="2018" data-month="0" data-date="4"><div>4</div></td><td title="" class="xdsoft_date xdsoft_day_of_week5 xdsoft_date" data-year="2018" data-month="0" data-date="5"><div>5</div></td><td title="" class="xdsoft_date xdsoft_day_of_week6 xdsoft_date xdsoft_weekend" data-year="2018" data-month="0" data-date="6"><div>6</div></td>"
JSON
Traceback (most recent call last):
File "./fl_bot.py", line 35, in <module>
ui.WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@id="+info+"]")))
File "/Library/Python/2.7/site-packages/selenium/webdriver/support/wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
码
{
"billFirstName":"rashawn",
"billLastName":"doyley",
"billAddress1":"11612 newburg st",
"billPostalCode":"11412",
"billCity":"jamaica",
"billState":"ny",
"billHomePhone":"7184139582",
"billEmailAddress":"rashawndoyley@gmail.com"
}
当我使用print语句打印出info变量中保存的元素时,我只得到4个元素而且没有特定的顺序
from selenium.webdriver.support import ui
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import json
from pprint import pprint
def get_page(model, sku):
url = "https://www.footlocker.com/product/model:"+str(model)+"/sku:"+ str(sku)+"/"
return url
data = json.load(open('info.json'))
browser = webdriver.Firefox()
page=browser.get(get_page(277097,"8448001"))
browser.find_element_by_xpath("//*[@id='pdp_size_select_mask']").click()
shoesize = ui.WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'a.grid_size:nth-child(8)')))
shoesize.click()
browser.find_element_by_xpath("//*[@id='pdp_addtocart_button']").click()
browser.get('https://www.footlocker.com/shoppingcart/default.cfm?sku=')
browser.find_element_by_css_selector('#cart_checkout_button').click()
for info , Value in data.items():
ui.WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@id="+info+"]")))
browser.find_element_by_xpath("//input[@id="+info+"]").click()
browser.find_element_by_xpath("//input[@id="+info+"]").clear()
#print(info,Value)
browser.find_element_by_xpath("//input[@id="+info+"]").send_keys(Value)
答案 0 :(得分:1)
根本原因是xpath不正确,需要在id值周围使用单引号或双引号。
V
应为"//input[@id="+info+"]"
另一个可能的风险是点击"//input[@id='"+info+"']"
按钮后页面需要很长时间才能加载,10秒是不够的。增加10至60秒。
CHECKOUT
只有第一个元素会等待很长时间,下一个元素将花费不到2秒的时间来查找甚至你给出的最大查找时间是60秒,因为页面已加载。