每次刷新都找不到项目。循环混乱

时间:2018-09-27 03:12:33

标签: python selenium

所以我有这个程序,每次刷新时我都需要它来寻找一个项目,直到该项目出现为止。现在,我所测试的循环无法正常工作,我不确定该如何编码。

driver.get("http://www.supremenewyork.com/shop/all/accessories")
items = driver.find_elements_by_xpath("//article[.//a[contains(.,'Bike')]] 
[.//a[.='Red']]//a")
while True:
time.sleep(1.2)
if items:
   items[0].click()
   break
else:
   driver.get("http://www.supremenewyork.com/shop/all/accessories")

1 个答案:

答案 0 :(得分:0)

也许这将为您解决问题:

links_visited = []

while True:

driver.get("http://www.supremenewyork.com/shop/all/accessories")

items = driver.find_elements_by_xpath("""//article[.//a[contains(.,'Bike')]] 
[.//a[.='Red']]//a""")

links = [item.get_attribute("href") for item in items]


for link in links:
    if link in links_visited:
        print "link already visited! skipping..."
        continue

    driver.get(link)
    time.sleep(5)
    links_visited.append(link)
    #do what you have to do here...

time.sleep(3)